C# DES ECB Encryption

2.7k views Asked by At

I am having difficulty encrypting something in C#.

I have 3 variables. First one is a 16 digit hex,lets call it X value I.E 0072701351979990 Second one is also a 16 digit hex value, lets call it Y I.E 3008168011FFFFFF

These two values have to be XOR 'ed to get the key for the DES-ECB encryption.

Thus resulting in 307a66934068666f . Now thus is my keyblock for the encryption. Then i have this as my datablock,which is 64 bits for encryption 0E329232EA6D0D73

Now i have the following code for encryption this. The result of the encryption should be XOR'ed with the datablock again and result in a 64bit result. This is not the case.

This is my code for the encryption

$ 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("-",""); 
    }

What am i doing wrong?

UPDATED QUESTION I have tested the above solution from CodeInChaos. It does give me back a 64 bit result. But still there is something wrong.

Here is my updated code.

The keyblock value is abababababababab and the data block value is 215135734068666F.

The resultant 64 bit result should be XOR'ed with the data block again.

The final answer is suppose to be 414945DD33C97C47 but I get 288a08c01a57ed3d.

Why does it not come out right?

Here is the specifications in suppliers documentation for the encryption.

Encryption is DEA in accordance with FIPS 46-3, single DES in ECB mode, using a single 64- bit DES Key with odd parity.

$      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

There are 2 answers

2
CodesInChaos On

I think you need to set the padding to PaddingMode.None:

desEncrypt.Padding = PaddingMode.None;

But you should really think hard, if DES and ECB is really what you want.


b.t.w.

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

can be replaced by:

encryptedData = encryptedStream.ToArray();
0
Pieter On

Perhaps it is necessary to set DES Provider to use the FIPS 46-3 Standard so that the DEA uses the permutation tables etc. specified in FIPS 46-3. Unfortunately I’m also struggling with this same issue.