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 (); } }
TripleDESCryptoServiceProvider默认使用 PKCS7-padding
要避免使用填充,只需设置 Padding -财产 PaddingMode.None
Padding
PaddingMode.None
new TripleDESCryptoServiceProvider { KeySize = keySizeInBits, Padding = PaddingMode.None };