3
내 프로젝트 중 하나에서 TripleDES를 사용하려고합니다. 나는 그것에 익숙해지기 위해 몇 가지 실험을하고있었습니다. 트리플 DES의 블록 크기가 8 바이트라고 이해하므로 8 바이트의 데이터를 제공하면 8 바이트의 암호화 된 데이터를 가져와야한다고 가정합니다. 하지만 내가 얻는 것은 :트리플 DES를 사용하는 동안 암호화 된 데이터 크기
Input Size | Encrypted Size . | . . | . 6 bytes | 8 bytes 7 bytes | 8 bytes 8 bytes | 16 bytes 9 bytes | 16 bytes . | . . | .
정상입니까? 그것은 그것이 작동 해야하는 방식입니다.
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();
}
}
PaddingMode.None
덕분에 많이 설정, 패딩을 사용하지 않도록합니다. 솔루션이 효과적이었습니다. 저는 구현을 이해하지 못합니다. 주어진 블록이 blocksize의 정확한 배수이면 아무 것도 채워서는 안됩니다. 어떤 제정신 프로그래머라도 그렇게 할 것입니다. – Hemant가역 패딩은 제거 방법을 알리기 위해 일부 데이터를 일반 텍스트에 추가해야합니다. 따라서 n * 8 바이트를 (n + 1) * 8 바이트로 확장해야합니다. –