RSA加密解密

xinuaile2003 2006-12-28 07:26:51
我用RSA对字符串进行加密后,为什么解密后得不到原来的字符串?
...全文
269 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinuaile2003 2006-12-28
  • 打赏
  • 举报
回复
结贴
xinuaile2003 2006-12-28
  • 打赏
  • 举报
回复
谢谢fupingzi83(一叶浮萍)这位朋友。
fupingzi83 2006-12-28
  • 打赏
  • 举报
回复
以上是我做的一个例子,成功了,你看下.有问题在说.
fupingzi83 2006-12-28
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Security.Cryptography;

///
/// 说明:
///本方法是针对字符串进行加密和解密运算的,由于每次只能针对117个字节进行加密和解密,
///所以,对于较长的字符串要先拆分
///这里在初始化RSACryptoServiceProvider实例时,没有指定密钥的大小,如果要指定,需为1024的倍数
///默认为1024,这样加密的后的字符串的长度为172,如果指定为2048,则为334,如果为3096,则为516
///

namespace SRAMethod
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int iMaxLength = 117;//一次加密的最大字节数
byte[] encryptedData;//加密后的数据
byte[] decryptedData;//解密后的数据

try
{
//生成非对称加密和解密的公钥和私钥的XML,并存储在字符串sKey中
int iKeyLength = 3096;
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(iKeyLength);
string sKey = RSA.ToXmlString(true);
string str = "key: " + sKey + "\n";

RSA.FromXmlString(sKey);//初始化密钥信息

byte[] dataToEncrypt = GenerateBytes();//获取要加密的字符串的二进制信息

int iEncrypt = dataToEncrypt.Length / iMaxLength; //加密要循环的次数

string sEncrypted = "";
for (int i = 0; i < iEncrypt + 1; i++)
{

byte[] buff = new Byte[iMaxLength];
//获取待加密的字节信息buff
if (i == iEncrypt)
{
System.Buffer.BlockCopy(dataToEncrypt, i * iMaxLength, buff, 0, dataToEncrypt.Length - i * iMaxLength);
}
else
{
System.Buffer.BlockCopy(dataToEncrypt, i * iMaxLength, buff, 0, iMaxLength);
}
encryptedData = RSA.Encrypt(buff, false);//加密运算
sEncrypted += Convert.ToBase64String(encryptedData);//把加密后的数据转化为字符串
}
str += "Encrypted buff: " + sEncrypted + '\n';

int iDecryptLength = (iKeyLength / 1024) * 172; //每次需要解密的字节数
int iDecrypt = sEncrypted.Length / iDecryptLength; //解密次数
string sDecrypted = ""; //解密后的字符串
int iByte = (iDecrypt + 1) * iMaxLength;
byte[] bDecrypted = new Byte[iByte]; //解密后的字节信息
for (int i = 0; i < iDecrypt + 1; i++)
{
string sToDecrypted = "";
//把待解密的字符串分段
if (i == iDecrypt)
{
sToDecrypted = sEncrypted.Substring(i * iDecryptLength, sEncrypted.Length - i * iDecryptLength);
}
else
{
sToDecrypted = sEncrypted.Substring(i * iDecryptLength, iDecryptLength);
}

if (sToDecrypted != null && sToDecrypted != "")
{
byte[] bToDecrypt = Convert.FromBase64String(sToDecrypted); //得到待解密的字节信息
decryptedData = RSA.Decrypt(bToDecrypt, false);//解密
System.Buffer.BlockCopy(decryptedData, 0, bDecrypted, i * iMaxLength, decryptedData.Length);//得到解密后的字节信息
}
}
sDecrypted += System.Text.Encoding.Default.GetString(bDecrypted); //得到解密后的字符串
str += "Decrypted buff: " + sDecrypted + '\n';

this.richTextBox1.Text = str;
}
catch (Exception e1)
{
throw e1;
}
}

//***********************************************************************
// 得到加密数据的字节数组
//***********************************************************************
public static byte[] GenerateBytes()
{
//待解密的字符串
string s = "进行填3333###???..,NGCr..,该数组已使用密码增强的密码增强的随机字节进随机字节进行填充RNGCryptoServiceProvide使用密码增强的随机字节r rng = new RNGCryptoServiceProvider() ;该数组已使用密码增强的随机字节进行填充RNGCryptoServiceProvider rng = new随机字节 RNGCryptoServiceProvider() ;随机字节";
//转换为字节数组
byte[] buff = System.Text.Encoding.Default.GetBytes(s);
return buff;
}
}
}

111,097

社区成员

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

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

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