C#DES ECB加密

时间:2012-05-25 08:22:27

标签: c# encryption des ecb

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

我有3个变量。 第一个是16位十六进制,我们称之为X值I.E 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位的结果。但仍有问题。

这是我更新的代码。

密钥值为abababababababab 数据块值为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;
    }

2 个答案:

答案 0 :(得分:0)

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

desEncrypt.Padding = PaddingMode.None;

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


b.t.w。

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

可以替换为:

encryptedData = encryptedStream.ToArray();

答案 1 :(得分:0)

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

相关问题