RSAParameters序列化的问题

挨踢啊挨踢 2006-07-06 06:16:03
static RSAParameters rsaParamsExcludePrivate1;
static RSAParameters rsaParamsIncludePrivate1;

产生RSA参数:
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//得到公钥
rsaParamsExcludePrivate=rsa.ExportParameters(false);
//得到私钥
rsaParamsIncludePrivate=rsa.ExportParameters(true);

ViewState["rsaParamsExcludePrivate"]=ClassSerializer.BinarySerializer(rsaParamsExcludePrivate);
ViewState["rsaParamsIncludePrivate"]=ClassSerializer.BinarySerializer(rsaParamsIncludePrivate);

加密:


RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsaParamsExcludePrivate1=(RSAParameters)ClassSerializer.BinaryDeserialize((string)ViewState["rsaParamsExcludePrivate"]);

rsa.ImportParameters(rsaParamsExcludePrivate1);
if(TB1.Text!="")
{ //要加密的数据
DataToEncrypt=ByteConverter.GetBytes(TB1.Text);
//加密
EncryptedData=rsa.Encrypt(DataToEncrypt,false);
TB2.Text=ByteConverter.GetString(EncryptedData);
DB.Enabled=true;
TB3.Text=DisplayByteArray(EncryptedData);
NewB.Enabled=false;
CB.Enabled=false;
TB1.ReadOnly=true;
TB1.Enabled=false;
}
else
{
TB2.Text="No data to encrypt!";
TB3.Text="No data to encrypt!";
TB4.Text="No data to encrypt and no data to decrypt!";
DB.Enabled=false;
}
byte [] TB2Byte=ByteConverter.GetBytes(TB2.Text);
if(TB2Byte.Length==128) //RSA参数为1024位时
SaveB.Enabled=true;
else
SaveB.Enabled=false;
解密:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

rsaParamsIncludePrivate1=(RSAParameters)ClassSerializer.BinaryDeserialize((string)ViewState["rsaParamsIncludePrivate"]);

rsa.ImportParameters(rsaParamsIncludePrivate1);
//解密
DecryptedData=rsa.Decrypt(EncryptedData,false);//此时出错
TB4.Text=ByteConverter.GetString(DecryptedData);
DB.Enabled=false;
CB.Enabled=true;
NewB.Enabled=true;
TB1.Enabled=true;
TB1.ReadOnly=false;
SaveB.Enabled=false;

序列化方法:

/// <summary>
/// Binary格式的序列化
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static string BinarySerializer(object o)
{
// To serialize the hashtable and its key/value pairs,
// you must first open a stream for writing.
// In this case, use a file stream.
//FileStream fs = new FileStream("DataFile.xml", FileMode.Create);
Stream ms=new MemoryStream();
// Construct a BinaryFormatter and use it to serialize the data to the stream.
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(ms, o);

byte[] b=new byte[ms.Length];
ms.Position=0;
ms.Read(b,0,b.Length);

string s=Convert.ToBase64String(b);
return s;
}
catch (SerializationException e)
{
//Log.Write("ServiceNode:Exception","Failed to serialize. Reason: " + e.Message);
throw e ;
}
finally
{
ms.Close();
}
}

/// <summary>
/// Binary格式的反序列化
/// </summary>
/// <param name="returnString"></param>
/// <returns></returns>
public static object BinaryDeserialize(string returnString)
{

// Open the file containing the data that you want to deserialize.
BinaryFormatter formatter;
MemoryStream ms=null;
try
{
formatter = new BinaryFormatter();

byte[] b=Convert.FromBase64String(returnString);

ms=new MemoryStream(b);

// Deserialize the hashtable from the file and
// assign the reference to the local variable.
object response = formatter.Deserialize(ms);
return response;
}
catch (SerializationException e)
{
//Log.Write("ServiceNode:Exception","Failed to deserialize. Reason: " + e.Message);
throw e;
}
finally
{
ms.Close();

}

}
...全文
315 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
littlefish9801 2006-08-16
  • 打赏
  • 举报
回复
用xmlserial
挨踢啊挨踢 2006-07-06
  • 打赏
  • 举报
回复
?rsaParamsIncludePrivate //序列化前
{System.Security.Cryptography.RSAParameters}
System.ValueType: {System.Security.Cryptography.RSAParameters}
D: {Length=128}
DP: {Length=64}
DQ: {Length=64}
Exponent: {Length=3}
InverseQ: {Length=64}
Modulus: {Length=128}
P: {Length=64}
Q: {Length=64}

//rsaParamsIncludePrivate1=(RSAParameters)ClassSerializer.BinaryDeserialize((string)ViewState["rsaParamsIncludePrivate"]);
?rsaParamsIncludePrivate1 //序列化后
{System.Security.Cryptography.RSAParameters}
System.ValueType: {System.Security.Cryptography.RSAParameters}
D: <undefined value>
DP: <undefined value>
DQ: <undefined value>
Exponent: {Length=3}
InverseQ: <undefined value>
Modulus: {Length=128}
P: <undefined value>
Q: <undefined value>

之所以这么做,我是想把公钥
rsaParamsExcludePrivate=rsa.ExportParameters(false);
和私钥
rsaParamsIncludePrivate=rsa.ExportParameters(true);
通过转存到ViewState中然后序列化,保存到数据库中,现在是解密时出错:

Bad Key.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Security.Cryptography.CryptographicException: Bad Key.

Source Error:


Line 150:
Line 151: rsa.ImportParameters(rsaParamsIncludePrivate1);
Line 152: DecryptedData=rsa.Decrypt(EncryptedData,false);
Line 153: TB4.Text=ByteConverter.GetString(DecryptedData);
Line 154: DB.Enabled=false;

谢谢大虾看看?!拜谢了!
牛人 2006-07-06
  • 打赏
  • 举报
回复
mark

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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