
要在 .net core 3.1 中使用 rsa 加密,可以利用 python 提供的代码来转换。将 python 中 rsa 的相关函数和库转换成 c# 代码如下:
获取 rsa 加密
public static string GetRsaEncrypt(string rsaKey, string txt)
{
// 公钥大素数
BigInteger biE = BigInteger.Parse(rsaKey, NumberStyles.HexNumber);
// 大整数 N
BigInteger biN = BigInteger.Parse("10001", NumberStyles.HexNumber);
byte[] publicKeyByte2 = biE.ToByteArray().Reverse().ToArray();
byte[] exponentByte2 = biN.ToByteArray().Reverse().ToArray();
using (var RSA = new RSACryptoServiceProvider())
{
var RSAKeyInfo = new RSAParameters
{
Modulus = publicKeyByte2,
Exponent = exponentByte2
};
RSA.ImportParameters(RSAKeyInfo);
byte[] passwordByte = Encoding.UTF8.GetBytes(txt);
var encryptResult = RSA.Encrypt(passwordByte, RSAEncryptionPadding.Pkcs1);
string encrypted = BitConverter.ToString(encryptResult).Replace("-", "");
return encrypted;
}
}使用该方法即可将 python 中 rsa 加密的代码转换为 c# 代码。
立即学习“Python免费学习笔记(深入)”;










