C# DES ECB Encryption

本文关键字:Encryption ECB DES | 更新日期: 2023-09-27 18:35:14

我在 C# 中加密某些内容时遇到困难。

我有 3 个变量。第一个是 16 位十六进制,我们称之为 X 值,即 0072701351979990第二个也是一个 16 位十六进制值,我们称之为 Y I.E 3008168011FFFFFF

这两个值必须进行异或运算才能获得DES-ECB加密的密钥。

从而产生 307a66934068666f。现在,这是我加密的关键块。然后我把它作为我的数据块,它是 64 位用于加密 0E329232EA6D0D73

现在我有以下代码用于加密。加密的结果应再次与数据块进行异或运算,并且结果为 64 位。事实并非如此。

这是我的加密代码

$ public static string DESEncrypt(string keyBlock,string dataBlock){
        DES desEncrypt = new DESCryptoServiceProvider();
        byte[] keyBlockBytes = BitConverter.GetBytes(Convert.ToInt64(keyBlock, 16));
        byte[] dataBlockBytes = BitConverter.GetBytes(Convert.ToInt64(dataBlock, 16));
        desEncrypt.Mode = CipherMode.ECB;
        desEncrypt.Key = keyBlockBytes;
        ICryptoTransform transForm = desEncrypt.CreateEncryptor();
        MemoryStream enecryptedStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(enecryptedStream, transForm, CryptoStreamMode.Write);
        cryptoStream.Write(dataBlockBytes, 0, dataBlockBytes.Length);
        cryptoStream.FlushFinalBlock();
        byte[] encryptedData = new byte[enecryptedStream.Length];
        enecryptedStream.Position = 0;
        enecryptedStream.Read(encryptedData, 0, encryptedData.Length);
        string enCryptedHex = BitConverter.ToString(encryptedData);
        return enCryptedHex.Replace("-",""); 
    }

我做错了什么?

更新的问题我已经从CodeInChaos测试了上述解决方案。它确实给了我一个 64 位的结果。但还是有些不对劲。

这是我更新的代码。

密钥块值为 ababab数据块值为 215135734068666F。

生成的 64 位结果应再次与数据块进行异或运算。

最终答案应该是 414945DD33C97C47,但我得到288a08c01a57ed3d.

为什么结果不对?

以下是供应商文档中的加密规范。

加密是符合 FIPS 46-3 的 DEA,ECB 模式下的单个 DES,使用单个 64-位 DES 键与奇偶校验。

$      public static string DESEncrypt(string keyBlock,string dataBlock){
        DES desEncrypt = new DESCryptoServiceProvider();
        byte[] keyBlockBytes = BitConverter.GetBytes(Convert.ToInt64(keyBlock, 16));
        byte[] dataBlockBytes = BitConverter.GetBytes(Convert.ToInt64(dataBlock, 16));
        desEncrypt.Mode = CipherMode.ECB;
        desEncrypt.Key = keyBlockBytes;
        desEncrypt.Padding = PaddingMode.None;
        ICryptoTransform transForm = desEncrypt.CreateEncryptor();
        MemoryStream enecryptedStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(enecryptedStream, transForm, CryptoStreamMode.Write);
        cryptoStream.Write(dataBlockBytes, 0, dataBlockBytes.Length);
        cryptoStream.FlushFinalBlock();
        byte[] encryptedData = enecryptedStream.ToArray();
        string enCryptedHex = BitConverter.ToString(encryptedData);
        enCryptedHex = enCryptedHex.Replace("-", "");
        long iDeaEncrypt = Convert.ToInt64(enCryptedHex, 16);
        long iDataBlock = Convert.ToInt64(dataBlock, 16);
        long decoderKey = iDeaEncrypt ^ iDataBlock;
        string decKeyHex = Convert.ToString(decoderKey, 16);
        return decKeyHex;
    }

C# DES ECB Encryption

我认为您需要将填充设置为PaddingMode.None

desEncrypt.Padding = PaddingMode.None;

但你真的应该认真思考,如果DES和欧洲央行真的是你想要的。


B.T.W.

byte[] encryptedData = new byte[enecryptedStream.Length];
encryptedStream.Position = 0;
encryptedStream.Read(encryptedData, 0, encryptedData.Length);

可以替换为:

encryptedData = encryptedStream.ToArray();

也许有必要将 DES 提供程序设置为使用 FIPS 46-3 标准,以便 DEA 使用 FIPS 46-3 中指定的排列表等。 不幸的是,我也在为同样的问题而苦苦挣扎。