Des encryption produces wrong output

This is a discussion on Des encryption produces wrong output within the C# Programming forums, part of the General Programming Boards category; I have the following function: Code: public static void Des(byte[] key, byte[] inBuf, byte[] outBuf) { if (key.Length != 8 ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,503

    Des encryption produces wrong output

    I have the following function:
    Code:
    public static void Des(byte[] key, byte[] inBuf, byte[] outBuf)
    {
        if (key.Length != 8 || inBuf.Length != 8 || outBuf.Length != 8)
        {
             throw new Exception("Des: Buffer or key length incorrect.");
        }
    
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        des.Key = key;
        des.Mode = CipherMode.ECB;
    
        ICryptoTransform encryptor = des.CreateEncryptor();
    
        byte[] tmpBuf = encryptor.TransformFinalBlock(inBuf, 0, 1);
        tmpBuf.CopyTo(outBuf, 0);
    }
    if I call it with some standard test data like so:
    Code:
    byte[] outBuf;
    MyClass.Des(new byte[] { 0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76 },
                new byte[] { 0x00, 0x4B, 0xD6, 0xEF, 0x09, 0x17, 0x60, 0x62 },
                out outBuf);
    I do not get the expected output of
    Code:
    { 0x88, 0xBF, 0x0D, 0xB6, 0xD7, 0x0D, 0xEE, 0x56 }
    instead I get
    Code:
    { 0xC6, 0x61, 0x8D, 0x63, 0x0B, 0x39, 0x31, 0xB0 }
    am I doing something wrong?

    I've even tried reversing the byte order of the test data, and I don't get the right result. I've tested the same data using C++ with CryptoPP on Linux, and I get the expected result, but I'm porting this code to run on .Net, and this is my main stoppage in the process at the moment.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    In Line 14 of your first block of code you are telling it to only encrypt the first byte. Change that to an 8 since you are sending 8 bytes.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Strangely, changing the read length to 8 results in a byte array containing 16 elements. The 8 bytes expected in the OP's expected output, followed by an additional 8 bytes. I don't really understand it. However using TransformBlock rather than TransformFinalBlock seems to result in the desired result:

    Code:
    using System;
    using System.Linq;
    using System.Security.Cryptography;
    
    namespace ConsoleApplication10
    {
        class Program
        {
            static void Main(string[] args)
            {
                byte[] outBuf = test(new byte[] { 0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76 },
                    new byte[] { 0x00, 0x4B, 0xD6, 0xEF, 0x09, 0x17, 0x60, 0x62 });
    
                foreach (byte b in outBuf)
                    Console.Write(String.Format("0x{0:X2}", b) + " ");
    
                Console.Read();
            }
    
            static byte[] test(byte[] key, byte[] data)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Key = key;
                des.Mode = CipherMode.ECB;
                ICryptoTransform enc = des.CreateEncryptor();
                byte[] buf = new byte[1024];
                int size = enc.TransformBlock(data, 0, data.Length, buf, 0);
                return buf.Take(size).ToArray();
            }
    
        }
    }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,503
    it generates the correct output now, and I also found the reason for the 16 bytes: you have to set padding to None on the DESCryptoServiceProvider, and then you get exactly 8 bytes of output for 8 bytes of input.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    You realize the weaknesses of ECB mode, right?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,503
    yes, and I also realize the weakness of DES, but this application only ever requires encrypting a single block at a time, so CBC would be pointless, and the proprietary smart card with which I'm interfacing doesn't give me the option of a different mode or encryption algorithm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program compiles, runs, but produces no output
    By happyclown in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 04:43 AM
  2. Wrong output
    By Grad in forum C Programming
    Replies: 4
    Last Post: 12-27-2005, 12:20 PM
  3. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM
  4. output produces funky characters
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2002, 08:14 PM
  5. What's wrong with my Stream Cipher Encryption?
    By Davros in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2002, 09:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21