System.Security.Cryptography.Cryptographic给定错误密码时出现异常

本文关键字:密码 异常 错误 Security Cryptography Cryptographic System | 更新日期: 2023-09-27 18:26:13

我的代码在密码的帮助下为我加密和解密字符串。

当我输入错误的密码时,我会得到这个错误:

mscorlib.dll 中发生"System.Security.Cryptographic.CryptographyException"类型的未处理异常

这是我的加密类代码:

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

是什么导致了此异常,应该如何处理?

System.Security.Cryptography.Cryptographic给定错误密码时出现异常

使用无效密码时,应出现CryptographicException。

当提供正确的密码时,您的代码运行良好,所以只需捕捉异常并做出正确的反应(向最终用户显示消息或其他什么)。

或者,您可以添加

symmetricKey.Padding = PaddingMode.Zeros;

并且在解密之后,应该移除八个CCD_ 1值,这些值表明解密成功。