Python は、C# で記述された dll ライブラリ (python は .net を呼び出します) を呼び出します

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 をこの「all data merged and secondary processing」ディレクトリに置きます
.test.py スクリプトの最適なファイルもこの下にあります.
注:
clr.FindAssembly('MyTestDll') のパラメータには接尾辞( .dll )
c = DESUtil() DESUtil クラスをインスタンス化する
pw_sign = c.EncryptDES(pw_content,"iskeyiskey") クラス内の EncryptDES 関数を呼び出す

**MyTestDll インポートから *** MyTestDll は c# の名前空間に対応する名前空間です.
MyTestDll が赤くバーストするのは正常です
. 問題を解決する必要がある場合は、グループに参加してくださいここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/m0_38124502/article/details/109822440