如何将 Python RSA 加密代码转换为 C# 代码?

如何将 python rsa 加密代码转换为 c# 代码?

python rsa 加密转换 c# 代码

本文旨在提供一段 python rsa 加密代码到 c# 的转换示例。

python 代码如下:

from rsa import key, common, transform, core, pkcs1def 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

c# 代码转换

立即学习“Python免费学习笔记(深入)”;

以下为 python 代码转换为 c# 的示例:

//公钥大素数            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);            }

此 c# 代码将生成与 python 代码相同格式的十六进制加密结果。

以上就是如何将 Python RSA 加密代码转换为 C# 代码?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1351947.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月13日 16:24:27
下一篇 2025年12月8日 00:35:16

相关推荐

发表回复

登录后才能评论
关注微信