三重DES加密

请注意,我在这里遇到的问题是密钥大小 。 首先,基于下面代码中包含的注释,我认为我的密钥需要是24字节(192位)。 这不起作用所以我给了16,32和8字节键一个镜头 – 似乎没有任何工作。 “不工作”是指在我的文本加密和解密后,它与原始文本的值不同。

例:

原文: 'Example test this should work '

加密文本: ¹pÕô6

解密文本: 'Example '

以下是我正在使用的两个function(加密/解密function)。 我还将包括我如何调用每个函数。

  // 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode) // plain-text should be 8-bytes, key should be 24 bytes. public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key) { // Create a new 3DES key. TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); // Set the KeySize = 192 for 168-bit DES encryption. // The msb of each byte is a parity bit, so the key length is actually 168 bits. des.KeySize = 192; des.Key = key; des.Mode = CipherMode.ECB; des.Padding = PaddingMode.None; ICryptoTransform ic = des.CreateEncryptor(); byte[] enc = ic.TransformFinalBlock(plainText, 0, 8); return enc; } public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key) { // Create a new 3DES key. TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); // Set the KeySize = 192 for 168-bit DES encryption. // The msb of each byte is a parity bit, so the key length is actually 168 bits. des.KeySize = 192; des.Key = key; des.Mode = CipherMode.ECB; des.Padding = PaddingMode.None; ICryptoTransform ic = des.CreateDecryptor(); byte[] dec = ic.TransformFinalBlock(plainText, 0, 8); return dec; } // Encrypt Text textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text), StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 "))); // Decrypt Text textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text), StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 "))); 

感谢您的任何帮助,

埃文

线索在您正在使用的函数的名称中: TripleDesEncryptOneBlock

此方法仅加密输入字符串的一个块(8个字节或64位)。 要加密整个字符串,您需要链接多次调用此方法。

用这个:

byte[] enc = ic.TransformFinalBlock(plainText, 0, plainText.Length);

我希望它会加密/解密你的整个字符串。 此外,您不需要多次调用此方法