要在 .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免费学习笔记(深入)”;
以上就是Python RSA 加密如何转换为 C# 代码?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号