首页 新闻 搜索 专区 学院

Python RSA 加密转换 C#代码

-1
悬赏园豆:15 [已解决问题] 解决于 2021-04-16 13:19
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);
            }             
Ian_zw的主页 Ian_zw | 初学一级 | 园豆:8
提问于:2021-04-13 16:51
< >
分享
最佳答案
0
  //公钥大素数
            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);
            }             
Ian_zw | 初学一级 |园豆:8 | 2021-04-16 13:17
其他回答(2)
0

详情看http://www.bouncycastle.org/csharp/index.html 这个库的rsa加密部分

收获园豆:15
InCerry | 园豆:217 (菜鸟二级) | 2021-04-13 18:26
0

格式不一样,见我之前的回答。

花飘水流兮 | 园豆:12547 (专家六级) | 2021-04-13 22:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册