简体   繁体   English

C#中的简单DES ECB加密和解密

[英]Simple DES ECB encryption and decryption in C#

I am trying to do an exercise which includes to decrypt a given encrypted session key with a given shared key. 我正在尝试进行一项练习,其中包括用给定的共享密钥解密给定的加密会话密钥。 I have decrypted the session key and printed the byte array on screen. 我解密了会话密钥并在屏幕上打印了字节数组。 (Same result is printed when ever I run the program). (运行程序时打印的结果相同)。

Then to check my work I am trying to encrypt the decrypted session key again (obviously with the same shared key) but the result is different all the time it, when is supposed to give me back the original encrypted session key. 然后检查我的工作我试图再次加密解密的会话密钥(显然使用相同的共享密钥)但结果是不同的,它应该给我回原始加密的会话密钥。

I cannot understand were is my mistake.... 我无法理解是我的错误....

Thanks 谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Crypto
{
    public class Program
    {
        static void Main(string[] args)
        {

        //Shared Master Key
        byte[] mkByteArray = { 0x12, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x23 };

        //Encrypted Session Key
        byte[] eskByteArray = { 0x4a, 0x4d, 0xe6, 0x87, 0x82, 0x47, 0xd3, 0x7b };

        PrintByteArray(eskByteArray);

        DES des = new DESCryptoServiceProvider();
        des.Mode = CipherMode.ECB;
        des.Padding = PaddingMode.None;
        des.Key = mkByteArray;

        ICryptoTransform ct1 = des.CreateDecryptor();
        byte[] resultArray1 = ct1.TransformFinalBlock(eskByteArray, 0, eskByteArray.Length);
        des.Clear();
        PrintByteArray(resultArray1);

        ICryptoTransform ct2 = des.CreateEncryptor();
        byte[] resultArray2 = ct2.TransformFinalBlock(resultArray1, 0, resultArray1.Length);
        des.Clear();
        PrintByteArray(resultArray2);
        }

        //-----Method to print the byte array on screen-----
        public static void PrintByteArray(byte[] bytes)
        {
            var sb = new StringBuilder("new byte[] { ");
            foreach (var b in bytes)
            {
                sb.Append(b + ", ");
            }
            sb.Append("}");
            Console.WriteLine(sb.ToString());
        }

    }
}

I found your problem, but now I am checking why it occurs exactly. 我找到了你的问题,但现在我正在检查它为什么会发生。

You call des.Clear(); 你叫des.Clear(); between encrypting and decrypting, if you do not do this, the input and output is the same. 在加密和解密之间,如果不这样做,输入和输出是相同的。


From Msdn : 来自Msdn

When called, the Clear method overwrites all sensitive data within the object with zeros 调用时,Clear方法用零覆盖对象内的所有敏感数据

Thus your masterkey in your DES object is set to zero which is why your output is different. 因此,DES对象中的masterkey设置为零,这就是输出不同的原因。

This happens because of the Initialisation Vector . 这是因为初始化矢量 This behaviour is there by design. 这种行为是设计的。 You don't want the encryption algorithm to produce exactly the same output for the same input. 您不希望加密算法为同一输入生成完全相同的输出。 Such algorythm is susceptible to plain text attack, where attacker can potentially infer encryption key from the input and the produced output. 这样的算法很容易受到明文攻击,攻击者可能会从输入和生成的输出中推断出加密密钥。

This happens in the line 这发生在行中

ICryptoTransform ct2 = des.CreateEncryptor();

According to msdn 根据msdn

If the current IV property is null, the GenerateIV method is called to create a new random IV. 如果当前IV属性为null,则调用GenerateIV方法以创建新的随机IV。

To fix this you need to set the IV for your encryptor. 要解决此问题,您需要为加密器设置IV。 You would then store IV alongside the encrypted bytes and use it for decryption. 然后,您将IV与加密字节一起存储并用于解密。 IV, in simple words, is some initial random noise that allows encryption algorithm to produce different input. IV,简单来说,是一些初始随机噪声,它允许加密算法产生不同的输入。 As a good practise, this noise must be different for each encryption call. 作为一种良好做法,每次加密呼叫的噪声必须不同。

Obviously, DES is a really weak algorithm, you would want to use at least 3DES. 显然,DES是一个非常弱的算法,你想要至少使用3DES。 But even 3DES is only used for legacy applications. 但即便是3DES也仅用于遗留应用程序。 Modern applications use AES. 现代应用程序使用AES。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM