System.FormatException:Base-64 char数组或字符串的长度无效

时间:2015-10-14 19:05:49

标签: asp.net cryptography

我在保存密码的同时使用加密技术,加密工作正常,但是当我解密密码时,有些是解密但有些会出现此错误

System.FormatException:Base-64 char数组或字符串的长度无效。

加密代码:

reduce <- function(word, counts) {
  if(sum(counts) > 3)
    keyval(word, sum(counts))
}

解密代码:

public string Encrypt(string clearText)
    {
        string EncryptionKey = "H1E2X3A4PE34RKS";
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
        }
        return clearText;
    }

0 个答案:

没有答案