使用 Triple DES 时的加密数据大小
我打算在我的一个项目中使用 TripleDES。 我做了一些实验来适应它。 我知道三重 DES 的块大小是 8 字节,所以我假设如果给出 8 字节的数据,我应该得到 8 字节的加密数据。 但我得到的是:
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 ();
}
}
I intend to use TripleDES in one of my project. I was doing some experiments to be comfortable with it. I understand block size of triple DES is 8 bytes so I assume that if give 8 byte of data, I should get 8 bytes of encrypted data. But what I get is:
Input Size | Encrypted Size . | . . | . 6 bytes | 8 bytes 7 bytes | 8 bytes 8 bytes | 16 bytes 9 bytes | 16 bytes . | . . | .
Is it normal? Is it the way it is supposed to work. Here is how I am trying to use triple 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 ();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TripleDESCryptoServiceProvider 默认使用 PKCS7-padding。 这会将任何消息填充到块大小的下一个倍数。
要避免使用填充,只需将
Padding
属性设置为PaddingMode.None
TripleDESCryptoServiceProvider defaults to using PKCS7-padding. This pads any message to the next multiple of the block-size.
To avoid using padding, just set the
Padding
-property toPaddingMode.None