1: Install pip install pythonnet
2: Dll library writing (or using someone else's)
(1)
How to create:
Then write the c# code (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;
}
}
}
This is an encryption and decryption of des cbc,
then we save it, and then find MyTestDll.dll in the resource file in the project.
3:
py script (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)
I put MyTestDll.dll in this "all data merged secondary processing" directory.
The best file of the test.py script is also under this.
Note:
The parameters in clr.FindAssembly('MyTestDll') do not have the suffix ( .dll )
c = DESUtil() instantiate a DESUtil class
pw_sign = c.EncryptDES(pw_content,"iskeyiskey") call the EncryptDES function inside the class
**from MyTestDll import *** MyTestDll is the namespace corresponding to the namespace in c#.
It is normal for MyTestDll to burst into red
. If you need to solve the problem, join the group