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();
}
}