我借助密码为我加密和解密字符串的代码。
当我输入错误的密码时,我收到此错误:
mscorlib.dll中发生未处理的“System.Security.Cryptography.CryptographicException”类型异常
这是我的加密类代码:
using System;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace EncryptStringSample
{
public static class StringCipher
{
// This constant string is used as a "salt" value for the PasswordDeriveBytes function calls.
// This size of the IV (in bytes) must = (keysize / 8). Default keysize is 256, so the IV must be
// 32 bytes long. Using a 16 character string here gives us 32 bytes when converted to a byte array.
private const string initVector = "tu89geji340t89u2";
// This constant is used to determine the keysize of the encryption algorithm.
private const int keysize = 256;
public static string Encrypt(string plainText, string passPhrase)
{
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
byte[] keyBytes = password.GetBytes(keysize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
return Convert.ToBase64String(cipherTextBytes);
}
public static string Decrypt(string cipherText, string passPhrase)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
byte[] keyBytes = password.GetBytes(keysize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
}
此处发生错误:
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
我在这里打电话给所有人:
// encrypting the raw text using PrivateKey
text_encrypted = EncryptStringSample.StringCipher.Encrypt(text_raw, PrivateKey);
// decrypting encrypted message using Partners Private Key
string text_decrypted = EncryptStringSample.StringCipher.Decrypt(decrypt_me, partner_PrivateKey);
导致此异常的原因是什么以及如何处理?
答案 0 :(得分:3)
使用无效密码时会出现CryptographicException。
提供正确密码时,您的代码工作正常,只需捕获异常并做出正确反应(向最终用户显示消息或其他内容)。
或者你可以添加
symmetricKey.Padding = PaddingMode.Zeros;
并且在解密之后,您应该删除八个显示解密成功的\0
值。