RSA非对称加密解密的问题,不能获得明文!!!!

大麦芽糖 大麦芽糖 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我用大数算法解密后得不到明文,请教大虾们!!!!

环境:VS2010,NET4.0
引用:using System.Security.Cryptography;
using System.Numerics;


public partial class FormRsaEncrypt : Form
{
public FormRsaEncrypt()
{
InitializeComponent();
this.Text = "RSA 加密解密";

textBoxEncrypt.ReadOnly = true;
textBoxDecrypt.ReadOnly = true;

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
this.txbPrivateKey.Text = RSA.ToXmlString(true);
this.txbPublicKey.Text = RSA.ToXmlString(false);
RSA.Clear();
}

private void buttonOK_Click(object sender, EventArgs e)
{
//int TextLength = 128;
byte[] encryptedData;
byte[] decryptedData;

try
{
RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider();
RSA1.FromXmlString(this.txbPrivateKey.Text);
RSAParameters RSAKeyInfo = RSA1.ExportParameters(true);

//byte[] dataToEncrypt = GenerateBytes(TextLength);
byte[] dataToEncrypt = Encoding.UTF8.GetBytes(this.textBoxInput.Text);

encryptedData = RSAEncrypt(dataToEncrypt, RSAKeyInfo.Exponent, RSAKeyInfo.Modulus);
int x = encryptedData.Length;
this.textBoxEncrypt.Text = Convert.ToBase64String(encryptedData);

decryptedData = RSADecrypt(encryptedData, RSAKeyInfo.D, RSAKeyInfo.Modulus);
int y = decryptedData.Length;
this.textBoxDecrypt.Text = Convert.ToBase64String(decryptedData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

//***********************************************************************
// RSA Encrypt
//***********************************************************************
static public byte[] RSAEncrypt(byte[] dataToEncrypt, byte[] Exponent, byte[] Modulus)
{
BigInteger original = new BigInteger(dataToEncrypt);
BigInteger e = new BigInteger(Exponent);
BigInteger n = new BigInteger(Modulus);

BigInteger encrypted = BigInteger.ModPow(original, e, n);
return encrypted.ToByteArray();
}

//***********************************************************************
// RSA Decrypt
//***********************************************************************
static public byte[] RSADecrypt(byte[] encryptedData, byte[] D, byte[] Modulus)
{
BigInteger encrypted = new BigInteger(encryptedData);
BigInteger d = new BigInteger(D);
BigInteger n = new BigInteger(Modulus);

BigInteger decrypted = BigInteger.ModPow(encrypted, d, n);

return decrypted.ToByteArray();
}
}
...全文

相关推荐

RSA非对称加密前端加密后端解密

基于.Net的MVC模拟登录,RSA非对称加密,web前端加密,C#后端解密。

风吹云东星不动

C#实现RSA非对称加密与解密

非对称加密是信息安全领域中的一种基础且重要的加密方法。与对称加密不同,非对称加密使用一对密钥:公钥和私钥。公钥用于加密数据,而私钥用于解密。这种特性使得非对称加密不仅可以用于加密和解密,还广泛应用于数字签名和身份验证等安全通信协议中。一个安全的密钥交换协议应该确保密钥在传输过程中即使被第三方截获也无法被破解。现有的密钥交换协议包括Diffie-Hellman、ECDH(椭圆曲线Diffie-Hellman)以及基于公钥基础设施(PKI)的密钥交换方法。

Ready-Player

前后端RSA非对称加密解密(代码示例).rar

登录时,用户的密码用明文传输,太不安全,应该加密传输,怎么做呢,对称加密一旦秘钥丢失则形同虚设,最好使用非对称加密的方式,由后端事先生成公钥和私钥,公钥发给前端页面,私钥后端自己保留,前端进行认证时,把密码原文用公钥加密再发给后端,后端用私钥解密而得到原文。非对称加密算法常用RSA算法,秘钥使用base64编码成字符串,后端使用jdk8的标准API,前端使用jsencrypt.js进行RSA的对应操作。经过测试,本例中的前后端代码的加密解密计算结果是一致的。

zexianc

c/c++实现的基于文件的RSA加解密

简单的c/c++实现的基于文件的RSA加解密

chyukn

C# RSA加密解密

C#做的RSA加密解密, 带winform界面, 测试通过, 代码完整。

lpb914
评论 4
收藏 已收藏
分享
返回顶部
大麦芽糖
C# 111,129 成员 642,538 内容