【发布时间】:2011-10-22 20:21:06
【问题描述】:
请注意,我在这里遇到的问题是 size 键。起初,根据下面代码中包含的 cmets,我认为我的密钥需要是 24 字节(192 位)。这没有用,所以我试了一下 16、32 和 8 字节键 - 似乎没有任何效果。 “不起作用”是指在我的文本被加密和解密后,它的值与我的原始文本不同。
例子:
原文:'Example test this should work '
加密文本: ¸¹pÕô6
解密文本: 'Example '
这是我正在使用的两个函数(加密/解密函数)。我还将包括我如何调用每个函数。
// 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 ")));
感谢您的帮助,
埃文
【问题讨论】:
-
不能解决您的问题,但 CipherMode.ECB 让我觉得这个网站很有趣...codinghorror.com/blog/2009/05/…
-
你在这干什么……?
标签: c# encryption des