我打算在我的一个项目中使用 TripleDES。我正在做一些实验以适应它。我知道三重 DES 的块大小是 8 个字节,所以我假设如果给出 8 个字节的数据,我应该得到 8 个字节的加密数据。但我得到的是:
输入大小 | 加密大小 . | . . | . 6 字节 | 8 个字节 7 个字节 | 8 个字节 8 字节 | 16 字节 9 个字节 | 16 字节 . | . . | .
正常吗?这是它应该工作的方式。以下是我尝试使用三重 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 ();
}
}