炫意html5
最早CSS3和HTML5移动技术网站之一

Python RSA 加密转换 C#代码

from rsa import key, common, transform, core, pkcs1
def get_rsa_encrypt(rsa_key, txt):
pub_key = key.PublicKey(int(rsa_key, 16), 65537)
key_length = common.byte_size(pub_key.n)
padded = pkcs1._pad_for_encryption(txt, key_length)
payload = transform.bytes2int(padded)
encrypted = core.encrypt_int(payload, pub_key.e, pub_key.n)
encode_str = hex(encrypted)[2:-1]
if len(encode_str) % 2 != 0:
encode_str = '0{}'.format(encode_str)
return encode_str

对接接口,客户给了一个python的代码, 这个一段代码想转换成C# 代码,在netcore3.1 下运行
哪个大佬,给看看怎么弄,查了几天没有找到对应的代码,感谢

回答

问题补充:

  //公钥大素数
            BigInteger biE = BigInteger.Parse(rsa_key, System.Globalization.NumberStyles.HexNumber);
            //大整数N
            BigInteger biN = BigInteger.Parse("10001", System.Globalization.NumberStyles.HexNumber);


            byte[] publicKeyByte2 = biE.ToByteArray();
            byte[] exponentByte2 = biN.ToByteArray();

            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {
                RSAParameters RSAKeyInfo = new RSAParameters();
                UTF8Encoding ByteConverter = new UTF8Encoding();
                RSAKeyInfo.Modulus = publicKeyByte2.Reverse().ToArray();
                RSAKeyInfo.Exponent = exponentByte2.Reverse().ToArray();
                RSA.ImportParameters(RSAKeyInfo);
                byte[] passwordByte = ByteConverter.GetBytes(txt);
                var encryptReuslt = RSA.Encrypt(passwordByte, RSAEncryptionPadding.Pkcs1);
                //var Ciphertext = Convert.ToBase64String(encryptReuslt);
                encrypted = BitConverter.ToString(encryptReuslt).Replace("-", "");
                //encrypted = GetHexFromChs(Ciphertext);
            }             

炫意HTML5 » Python RSA 加密转换 C#代码

Java基础教程Android基础教程