密码错误时出现System.Security.Cryptography.CryptographicException [英] System.Security.Cryptography.CryptographicException when wrong password given

查看:203
本文介绍了密码错误时出现System.Security.Cryptography.CryptographicException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有的代码借助密码为我加密和解密字符串。

The code I have encrypts and decrypts strings for me with help of a password.

输入错误密码后,我会收到此错误:

When I enter a wrong password I get this error:


mscorlib.dll中发生了'System.Security.Cryptography.CryptographicException'类型的未处理异常

An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll

这是我的加密类代码:

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

什么原因导致此异常以及如何处理?

What causes this exception and how should it be handled?

推荐答案

使用无效密码时,应该具有CryptographicException。

It is to be expected to have CryptographicException when using invalid password.

提供正确的密码后,您的代码可以正常工作,因此只需捕获异常并做出正确的反应(向最终用户或其他人显示消息)即可。

Your code works fine when providing correct password so just simply catch exception and react properly (display message to end user or something).

或者,您可以添加

symmetricKey.Padding = PaddingMode.Zeros;

解密后,您应该删除八个 \0 表示解密成功的值。

and after decryption you should remove eight \0 values showing that decryption was successful.

这篇关于密码错误时出现System.Security.Cryptography.CryptographicException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆