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

大麦芽糖 2012-09-03 10:13:57
我用大数算法解密后得不到明文,请教大虾们!!!!

环境: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();
}
}
...全文
223 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqtt789632147 2012-09-04
  • 打赏
  • 举报
回复

msdn上不是有现成了的么?为什么还要自己写大数算法?


//Pass the data to ENCRYPT, the public key information
//(using RSACryptoServiceProvider.ExportParameters(false),
//and a boolean flag specifying no OAEP padding.
encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);

//Pass the data to DECRYPT, the private key information
//(using RSACryptoServiceProvider.ExportParameters(true),
//and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
大麦芽糖 2012-09-04
  • 打赏
  • 举报
回复
再定!!!
大麦芽糖 2012-09-03
  • 打赏
  • 举报
回复
就差这一点了,帮帮忙啊!!!
【课程介绍】     课程目标:             - 有状态登录和无状态登录的区别             - 常见的非对称加密算法和非对称的加密方式             - 老版本只使用jwt进行加密的弊端             - 授权中心的授权流程             - 如何整合网关组件实现jwt安全验证             - 理解什么是公钥什么是私钥      - 深刻理解授权流程什么是有状态? 有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,典型的设计如tomcat中的session。例如登录:用户登录后,我们把登录者的信息保存在服务端session中,并且给用户一个cookie值,记录对应的session。然后下次请求,用户携带cookie值来,我们就能识别到对应session,从而找到用户的信息。缺点是什么?- 服务端保存大量数据,增加服务端压力- 服务端保存用户状态,无法进行水平扩展- 客户端请求依赖服务端,多次请求必须访问同一台服务器。什么是无状态? 微服务集群中的每个服务,对外提供的都是Rest风格的接口。而Rest风格的一个最重要的规范就是:服务的无状态性,即:- 服务端不保存任何客户端请求者信息- 客户端的每次请求必须具备自描述信息,通过这些信息识别客户端身份带来的好处是什么呢?- 客户端请求不依赖服务端的信息,任何多次请求不需要必须访问到同一台服务- 服务端的集群和状态对客户端透明- 服务端可以任意的迁移和伸缩- 减小服务端存储压力

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧