-1

我在用 C# 加密某些东西时遇到了困难。

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

这两个值必须经过 XOR 运算才能获得 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 位的结果。但是还是有问题。

这是我更新的代码。

keyblock值为ababababababababab,数据块值为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;
    }
4

2 回答 2

0

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

desEncrypt.Padding = PaddingMode.None;

但是你真的应该好好想想,如果 DES 和 ECB 真的是你想要的。


顺便提一句

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

可以替换为:

encryptedData = encryptedStream.ToArray();
于 2012-05-25T08:27:53.727 回答
0

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

于 2014-10-22T10:59:42.727 回答