我使用像這樣的RSACryptoServiceProvider ...RSACryptoServiceProvider KeyContainer似乎超時?
private byte[] RSAEncrypt(byte[] DataToEncrypt, string ContainerName, bool DoOAEPPadding)
{
try
{
byte[] encryptedData;
// Create a new instance of CspParameters. Pass
// 13 to specify a DSA container or 1 to specify
// an RSA container. The default is 1.
CspParameters cspParams = new CspParameters();
// Specify the container name using the passed variable.
cspParams.KeyContainerName = ContainerName;
cspParams.Flags = CspProviderFlags.UseDefaultKeyContainer;
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspParams))
{
//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
return encryptedData;
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException ex)
{
sl.Write(ex, MessageType.Error);
throw;
}
}
然後我嘗試關閉我的Outlook插件的Windows窗體應用程序,並把它背在其上使用這個代碼peice的是什麼後,對數據進行解密。解密代碼看起來像這樣...
private byte[] RSAEncrypt(byte[] DataToEncrypt, string ContainerName, bool DoOAEPPadding)
{
try
{
byte[] encryptedData;
// Create a new instance of CspParameters. Pass
// 13 to specify a DSA container or 1 to specify
// an RSA container. The default is 1.
CspParameters cspParams = new CspParameters();
// Specify the container name using the passed variable.
cspParams.KeyContainerName = ContainerName;
cspParams.Flags = CspProviderFlags.UseDefaultKeyContainer;
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspParams))
{
//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
return encryptedData;
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException ex)
{
sl.Write(ex, MessageType.Error);
throw;
}
}
直到東西來了,我不能把我的手指上
的偉大工程。我不知道它是否像日期變化或什麼。會發生什麼是我試圖解密數據,並得到「錯誤的數據」錯誤。現在它再次運行良好,直到某段時間,或關閉應用程序或用戶註銷。我只是不知道,也不能確定是什麼原因造成的。當我吹走來自文本文件的加密數據並重新創建並解密時,我沒有問題。即使我在加密/保存到文件和從文件讀取/解密它之間重新啓動應用程序也會很好!發生了一些事情,我只是不知道KeyContainers是否足夠了解可能會使CspParameters過期的最佳猜測?