2014-10-10 106 views
0

我使用密码帮助我加密和解密字符串。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); 

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

+0

有些东西可能会帮助您获得答案:您在哪里致电您的代码?加密或解密中的错误?你传递给这些方法的参数是什么?如有疑问,可以包含错误堆栈跟踪。事实上,您提供的信息不足以发现您的问题。另外,请查看C#[''using'](http://msdn.microsoft.com/zh-cn/library/yh598w02.aspx)声明:您应该将它用于您的Streams。 – 2014-10-10 18:39:32

+0

您好,我解密时遇到问题。这里是该代码:string text_decrypted = EncryptStringSample.StringCipher.Decrypt(decrypt_me,partner_PrivateKey); – d0ve 2014-10-10 18:44:41

+0

和所说的我得到的错误,当私钥与加密时输入的不一样(代码:text_encrypted = EncryptStringSample.StringCipher.Encrypt(text_raw,PrivateKey);) – d0ve 2014-10-10 18:45:30

回答

2

使用无效密码时,预计会发生CryptographicException。

当您提供正确的密码时,您的代码正常工作,只需简单地捕获异常并做出适当的反应(将消息显示给最终用户或其他东西)。

另外,您可以添加

symmetricKey.Padding = PaddingMode.Zeros; 

和解密后,你应该删除8个\0值显示,解密成功。

+0

谢谢,帮助。 – d0ve 2014-10-10 19:13:08