3
我打算在我的一個項目中使用TripleDES。我正在做一些實驗以適應它。我知道三重DES的塊大小是8個字節,所以我假設如果給8個字節的數據,我應該得到8個字節的加密數據。但我得到的是:使用Triple DES時的加密數據大小
Input Size | Encrypted Size . | . . | . 6 bytes | 8 bytes 7 bytes | 8 bytes 8 bytes | 16 bytes 9 bytes | 16 bytes . | . . | .
這是正常的嗎?它是應該如何工作的。這裏是我想使用三重DES:
class TripleDESEncryption
{
private readonly TripleDESCryptoServiceProvider engine;
public TripleDESEncryption() : this (256) { }
public TripleDESEncryption (int keySizeInBits) {
engine = new TripleDESCryptoServiceProvider { KeySize = keySizeInBits };
engine.GenerateKey();
}
public byte[] Encrypt (byte[] plain) {
return engine.CreateEncryptor().TransformFinalBlock (plain, 0, plain.Length);
}
public byte[] Decrypt (byte[] encrypted) {
return engine.CreateDecryptor().TransformFinalBlock (encrypted, 0, encrypted.Length);
}
}
class Program
{
static readonly int MAX_TEXT_LENGTH = 128;
static void Main (string[] args) {
Console.WriteLine ("{0,10}{1,10}{2,10}{3,10}", "Algo", "Key Size", "Input Size", "Encrypted Size");
var tripleDES = new TripleDESEncryption();
var input = new List<byte>();
for (int i = 0; i <= MAX_TEXT_LENGTH; i++) {
var plain = input.ToArray();
var encrypted = tripleDES.Encrypt (plain);
Console.WriteLine ("{0,10}{1,10}{2,10}{3,10}", "Triple DES", keySize, input.Count, encrypted.Length);
input.Add (0x65);
}
Console.ReadLine();
}
}
非常感謝。您的解決方案奏效 我只是不明白的實施。如果給出的塊是塊大小的確切倍數,則不應該填充任何東西。我想,任何理智的程序員都會這樣做。 – Hemant 2009-04-20 11:19:21