我正在获取System.Security.Cryptography.CryptographicException: 'Length of the data to decrypt is invalid



加密工作正常,但在解密密文时出现异常。密文是通过加密"Test"生成的密码文本:u1jeSfKVfSRfSieLX01/uQ==

string encrypt(string plainText)
{
AesManaged aesCipher = new AesManaged();
aesCipher.KeySize = 128;
aesCipher.BlockSize = 128;
aesCipher.Mode = CipherMode.CBC;
aesCipher.Padding = PaddingMode.PKCS7;
aesCipher.Key = generatedKey();
byte[] iv = new byte[16];
// 15 1B 0F 03 56 3A 66 6D E5 E1 1D 83 12 21 B4 8E
iv[0] = 0x15;
iv[1] = 0x1B;
iv[2] = 0x0F;
iv[3] = 0x03;
iv[4] = 0x56;
iv[5] = 0x3A;
iv[6] = 0x66;
iv[7] = 0x6D;
iv[8] = 0xE5;
iv[9] = 0xE1;
iv[10] = 0x1D;
iv[11] = 0x83;
iv[12] = 0x12;
iv[13] = 0x21;
iv[14] = 0xB4;
iv[15] = 0x8E;
aesCipher.IV = iv;
byte[] b = System.Text.Encoding.UTF8.GetBytes(plainText);
ICryptoTransform encryptTransform = aesCipher.CreateEncryptor();
byte[] ctext = encryptTransform.TransformFinalBlock(b, 0, b.Length);
System.Console.WriteLine("IV:" + Convert.ToBase64String(aesCipher.IV));
System.Console.WriteLine("Cipher text: " + Convert.ToBase64String(ctext));
return Convert.ToBase64String(ctext);
}
string decrypt(String CipherText)
{
AesManaged aesCipher = new AesManaged();
aesCipher.KeySize = 128;
aesCipher.BlockSize = 128;
aesCipher.Mode = CipherMode.CBC;
aesCipher.Padding = PaddingMode.PKCS7;
byte[] key = generatedKey();
aesCipher.Key = key;
byte[] iv = new byte[16];
// 15 1B 0F 03 56 3A 66 6D E5 E1 1D 83 12 21 B4 8E
iv[0] = 0x15;
iv[1] = 0x1B;
iv[2] = 0x0F;
iv[3] = 0x03;
iv[4] = 0x56;
iv[5] = 0x3A;
iv[6] = 0x66;
iv[7] = 0x6D;
iv[8] = 0xE5;
iv[9] = 0xE1;
iv[10] = 0x1D;
iv[11] = 0x83;
iv[12] = 0x12;
iv[13] = 0x21;
iv[14] = 0xB4;
iv[15] = 0x8E;
aesCipher.IV = iv;
System.Console.WriteLine("IV:" + Convert.ToBase64String(aesCipher.IV));
// aesCipher.IV = new Ini
ICryptoTransform decryptTransform = aesCipher.CreateDecryptor(aesCipher.key, aesCipher.IV);
byte[] plainText = decryptTransform.TransformFinalBlock(Encoding.ASCII.GetBytes(CipherText), 0, CipherText.Length);
return System.Text.Encoding.UTF8.GetString(plainText);
}
byte[] generatedKey()
{
// byte[] salt = new byte[] { 172, 137, 25, 56, 156, 100, 136, 211, 84, 67, 96, 10, 24, 111, 112, 137, 3 };
byte[] salt = new byte[17];
// AC 89 19 38 9C 64 88 D3 54 43 60 0A 18 6F 70 89 03
salt[0] = 0xAC;
salt[1] = 0x89;
salt[2] = 0x19;
salt[3] = 0x38;
salt[4] = 0x9C;
salt[5] = 0x64;
salt[6] = 0x88;
salt[7] = 0xD3;
salt[8] = 0x54;
salt[9] = 0x43;
salt[10] = 0x60;
salt[11] = 0x0A;
salt[12] = 0x18;
salt[13] = 0x6F;
salt[14] = 0x70;
salt[15] = 0x89;
salt[16] = 0x03;

int iterations = 1024;
var rfc2898 = new System.Security.Cryptography.Rfc2898DeriveBytes("!CarIT.123#2017", salt, iterations);
byte[] key = rfc2898.GetBytes(16);
String keyB64 = Convert.ToBase64String(key);
System.Console.WriteLine("Key: " + keyB64);
return key;
}

我也尝试过使用byte[]plainText=decryptTransform.TransformFinalBlock(Convert.FromBase64String(CipherText(,0,CipherText.Length(;而不是byte[]plainText=decryptTransform.TransformFinalBlock(Encoding.ASCII.GetBytes(CipherText(,0,CipherText.Length(;但我犯了错误;值无效";在同一条线上。

我的坏,

byte[] plainText = decryptTransform.TransformFinalBlock(Encoding.ASCII.GetBytes(CipherText), 0, CipherText.Length);

应该像一样

byte[] plainText = decryptTransform.TransformFinalBlock(Encoding.ASCII.GetBytes(CipherText), 0, Encoding.ASCII.GetBytes(CipherText).Length);

现在它工作得很好,我在TransformFinalBlock()中传递了字节作为第一个参数,然后在第三个参数中获得了纯文本的长度,这是错误的。

相关内容

  • 没有找到相关文章

最新更新