我正在尝试从密钥容器访问私钥,然后使用它解密先前加密的字节[]消息。我的代码似乎能够加密该字节[],但是当我尝试解密时,我会收到以下消息:
mscorlib.dll
'system.ObjectDisposedexception'类型的例外
我在Visual Studios中使用C#。我的主要功能看起来像这样:
try
{
string testValue = "TestKeyContainer";
string message = "This is the test message!";
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] originalData = ByteConverter.GetBytes(message);
byte[] encryptedData;
byte[] decryptedData;
RSACryptoServiceProvider rsa = null;
//Create a public-private key pair and store them in a key container.
MakeAndSaveKey(testValue);
//[Attempt to] retrieve the key from the container
rsa = GetKeyFromContainer(testValue);
//Read message
Console.WriteLine("Reading the test message... *ahem*...n{0}", ByteConverter.GetString(originalData));
//Encrypt, then read message
encryptedData = encrypt(originalData, rsa);
Console.WriteLine("Reading the encrypted message...n....n{0}", ByteConverter.GetString(encryptedData));
//Decrypt, then read message
decryptedData = decrypt(encryptedData, rsa);
Console.WriteLine("Reading the decrypted message...n{0}", ByteConverter.GetString(decryptedData));
//Delete key from the container
//DeleteKey("TestKeyContainer");
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
deletekey(string(未完成,因此发表了评论。makeandsavekey(字符串(的代码为:
private static void MakeAndSaveKey(string containerName)
{
CspParameters cp = new CspParameters();
cp.KeyContainerName = containerName;
CspParameters cp = new CspParameters();
cp.KeyContainerName = containerName;
return;
}
getKeyfromContainer(字符串(是:
private static RSACryptoServiceProvider GetKeyFromContainer(string containerName)
{
CspParameters cp = new CspParameters();
cp.KeyContainerName = containerName;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
return rsa;
}
应该注意的是,Makeandsavekey和getKeyfromContainer实际上只是Microsoft代码的C#翻译如何:将其to的方法存储在密钥容器中。
。加密的代码(byte [],rsacryptoserviceprovider(是:
private static byte[] encrypt(byte[] message, RSACryptoServiceProvider rsa)
{
using (rsa)
{
message = RSAEncrypt(message, rsa.ExportParameters(false), false);
}
return message;
}
和解密(字节[],rsacryptoserviceprovider(:
private static byte[] decrypt(byte[] message, RSACryptoServiceProvider rsa)
{
using (rsa)
{
try
{
message = RSADecrypt(message, rsa.ExportParameters(true), false);
}
catch (CryptographicException e)
{
Console.WriteLine("Couldn't decrypt the message. n:ERROR: {0}", e.Message);
}
}
return message;
}
程序在失速之前输出以下内容:
阅读测试消息... ahem ...
这是测试消息!
阅读加密消息...
....
?????????????????????????????????????????????????????????????????????????????????????????????????????????? S ??????? H?
,除了'='有三行而不是两行。我只是不知道在哪里可以找到该符号。
之后,我得到了本文顶部描述的例外。具体来说,它指向语句
message = RSADecrypt(message, rsa.ExportParameters(true), false);
从解密(字节[],rsacryptoserviceProvider(定义。有人知道为什么会发生这种情况,还是如何解决?
using (resource)
{
// do something
}
是句法糖:
try
{
// do something
}
finally
{
if (resource!= null)
resource.Dispose();
}
因此,这意味着decrypt
试图使用已经处置的对象。如果您将resource
(s(固定在自己的容器中,则意味着您自己管理它,不应使用using
。删除使用using
的使用,并确保在完成后手动处置所有资源。它应该起作用。