【问题标题】:Triple DES encryption三重 DES 加密
【发布时间】:2011-10-22 20:21:06
【问题描述】:

请注意,我在这里遇到的问题是 size 键。起初,根据下面代码中包含的 cmets,我认为我的密钥需要是 24 字节(192 位)。这没有用,所以我试了一下 16、32 和 8 字节键 - 似乎没有任何效果。 “不起作用”是指在我的文本被加密和解密后,它的值与我的原始文本不同。

例子:

原文:'Example test this should work '

加密文本: ¸¹pÕô6

解密文本: 'Example '

这是我正在使用的两个函数(加密/解密函数)。我还将包括我如何调用每个函数。

        // 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
        // plain-text should be 8-bytes, key should be 24 bytes.
        public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
        {
            // Create a new 3DES key.
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

            // Set the KeySize = 192 for 168-bit DES encryption.
            // The msb of each byte is a parity bit, so the key length is actually 168 bits.

            des.KeySize = 192;
            des.Key = key;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.None;

            ICryptoTransform ic = des.CreateEncryptor();

            byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);

            return enc;
        }

        public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
        {
            // Create a new 3DES key.
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

            // Set the KeySize = 192 for 168-bit DES encryption.
            // The msb of each byte is a parity bit, so the key length is actually 168 bits.
            des.KeySize = 192;
            des.Key = key;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.None;

            ICryptoTransform ic = des.CreateDecryptor();

            byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);

            return dec;
        }

// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text), StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));

// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text), StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));

感谢您的帮助,

埃文

【问题讨论】:

标签: c# encryption des


【解决方案1】:

线索在您正在使用的函数的名称中:TripleDesEncryptOneBlock

此方法仅加密输入字符串的一个块(8 字节或 64 位)。要加密整个字符串,您需要链接多次调用此方法。

【讨论】:

  • 我不得不多次调用这个方法似乎很奇怪。有没有办法修改这段代码,这样我就不需要这样做了?
  • 目的是让您可以实现不同的链接方法,例如 CBC 或 CFB。
  • 在 8 个块中调用此函数的正确方法是什么?你能帮我解决这个问题吗?我应该将数据拆分为 8 个字节并调用它吗?
【解决方案2】:

使用这个:

byte[] enc = ic.TransformFinalBlock(plainText, 0, plainText.Length);

我希望它能加密/解密你的整个字符串。此外,您无需多次调用此方法

【讨论】:

    【解决方案3】:

    你的问题在这里:

    byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
                                                      ^
    

    您只对数组的前 8 个字符进行编码因此,当您解码时,只有这 8 个字符需要解码,结果为 'Example '

    如果您想对所有文本进行编码,则必须增加该值。但是要小心,如果你使用PaddingMode.None,如果要编码的数组的长度不是8的倍数,它会失败。

    我在文本中添加了一些填充,如下所示:

    int length = plainText.Length / 8;
    if(plainText.Length%8 > 0)
    {
        length++;
    }
    byte[] paddedText = new byte[length * 8];
    plainText.CopyTo(paddedText, 0);
    byte[] enc = ic.TransformFinalBlock(paddedText, 0, length * 8);
    

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多