请注意,我在这里遇到的问题是 key size。起初,根据下面代码中包含的注释,我认为我的 key 需要为 24 字节(192 位)。这没有用,所以我试了一下 16、32 和 8 字节的 key ——似乎没有任何效果。 “不工作”是指在我的文本被加密和解密后,它与我的原始文本不具有相同的值(value)。
例子:
原文: '示例测试应该可以工作'
加密文本: ¸¹pÕô6
解密文本: '示例'
这是我正在使用的两个函数(加密/解密函数)。我还将包括我如何调用每个函数。
// 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
此方法仅加密输入字符串的一个 block (8 字节或 64 位)。要加密整个字符串,您需要链接多次调用此方法。
关于c# - 三重 DES 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6990286/