使用 Triple DES 时的加密数据大小

发布于 2024-07-18 04:39:45 字数 1711 浏览 6 评论 0原文

我打算在我的一个项目中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2024-07-25 04:39:45

TripleDESCryptoServiceProvider 默认使用 PKCS7-padding。 这会将任何消息填充到块大小的下一个倍数。

要避免使用填充,只需将 Padding 属性设置为 PaddingMode.None

new TripleDESCryptoServiceProvider { 
  KeySize = keySizeInBits, 
  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 to PaddingMode.None

new TripleDESCryptoServiceProvider { 
  KeySize = keySizeInBits, 
  Padding = PaddingMode.None 
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文