python调用dll库(python调用.net) c#编写

1:安装pip install pythonnet

2:dll库编写(或者使用别人写好的)
(1)
如何创建:

在这里插入图片描述
在这里插入图片描述
然后写上c#代码(MyTestDll.dll):

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace MyTestDll
{
    
    
    public class DESUtil
    {
    
    
            public static string EncryptDES(string encryptString, string encryptKey)
            {
    
    
                string text = null;
                try
                {
    
    
                    using (DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider())
                    {
    
    
                        byte[] bytes = Encoding.UTF8.GetBytes(encryptString);
                        descryptoServiceProvider.Key = Encoding.ASCII.GetBytes(encryptKey);
                        descryptoServiceProvider.IV = new byte[]
					{
    
    1,2,3,4,5,6,7,8};
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
    
    
                            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write))
                            {
    
    
                                cryptoStream.Write(bytes, 0, bytes.Length);
                                cryptoStream.FlushFinalBlock();
                            }
                            text = Convert.ToBase64String(memoryStream.ToArray());
                            text = text.Replace('/', '_');
                            text = text.Replace('+', '.');
                        }
                    }
                }
                catch (Exception arg)
                {
    
    
                    Console.WriteLine("{0} Exception caught.", arg);
                }
                return text;
            }
            public static string DecryptDES(string pToDecrypt, string sKey)
            {
    
    
                string result = null;
                try
                {
    
    
                    byte[] array = Convert.FromBase64String(pToDecrypt);
                    using (DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider())
                    {
    
    
                        descryptoServiceProvider.Key = Encoding.ASCII.GetBytes(sKey);
                        descryptoServiceProvider.IV = new byte[]
					{
    
    1,2,3,4,5,6,7,8};
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
    
    
                            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write))
                            {
    
    
                                cryptoStream.Write(array, 0, array.Length);
                                cryptoStream.FlushFinalBlock();
                                cryptoStream.Close();
                            }
                            result = Encoding.UTF8.GetString(memoryStream.ToArray());
                        }
                    }
                }
                catch (Exception arg)
                {
    
    
                    Console.WriteLine("{0} Exception caught.", arg);
                }
                return result;
            }
        }   
}

这是一个des cbc 的加密和解密
然后我们保存,再工程里面,资源文件里面找到MyTestDll.dll 即可
3:
py脚本(test.py)

import clr
import sys
import System
sys.path.append("F:\所有数据归并二次处理\")
clr.FindAssembly('F:\所有数据归并二次处理\MyTestDll.dll')
print(System.Environment.Version)
d = clr.FindAssembly('MyTestDll')
from MyTestDll import *
c = DESUtil()
pw_content = 'testoneoenoen'
pw_sign = c.EncryptDES(pw_content,"iskeyiskey")
print(pw_sign)

我把MyTestDll.dll放在了这个"所有数据归并二次处理"目录下
test.py脚本最好文件也在这个下面
注意:
clr.FindAssembly(‘MyTestDll’) 中参数不要带后缀( .dll )
c = DESUtil() 实例化一个DESUtil类
pw_sign = c.EncryptDES(pw_content,“iskeyiskey”)调用类里面的EncryptDES函数

**from MyTestDll import *** 中MyTestDll 是c#中命名空间对应的 namespace MyTestDll
爆红色很正常
需要解决问题的话,进群在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_38124502/article/details/109822440