4

I know It's a probably common question but I cannot find answer anywhere. So I have byte array key and byte array value and I need to produce new 8 byte array that has been encrypted with DES in C#

emirkljucanin
  • 804
  • 1
  • 9
  • 20
  • 3
    Is [google](http://www.google.com/#sclient=psy&hl=en&q=c%23+des+encrypt&aq=f&aqi=&aql=&oq=&pbx=1&fp=f478bdfafcb0c911&safe=active) down today? – Darin Dimitrov Jan 18 '11 at 11:07
  • No it's not down but i cannot find anywhere how to do exactly this. I have key and values and new array that is encrypted value with that key but I cannot get the same results in my code. – emirkljucanin Jan 18 '11 at 11:12
  • @Darin Hey, [that's what I said](http://stackoverflow.com/questions/4718646/block-cipher-string-encryption-decryption-in-c)! – Linus Kleen Jan 18 '11 at 11:14
  • 2
    @Darin, @gore, the only SO post in those google results is not a direct dupe. So it is a perfectly valid question here. – H H Jan 18 '11 at 11:42
  • 1
    Note that in many encryption modes the cyphertext is longer than the plaintext. – CodesInChaos Jan 18 '11 at 13:05

2 Answers2

4

Here is your sample code. Remember to pad trailig zeros with random data, remember the bytes written, and DES parametrs: Key, IV.

Best wishes ;)

using System.Security.Cryptography;
using System.IO;
namespace hash
{
    public static class Program
    {
        static void Main(string[] args)
        {
            byte[] data = new byte[10000];
            DES des = DES.Create();
            int bytesWritten = 0;
            data = Encode(data, des, out bytesWritten);
        }

        private static byte[] Encode(byte[] data, DES des, out int bytesWritten)
        {
            using (var input = new MemoryStream(data))
            using (var output = new MemoryStream())
            using (var csp = new DESCryptoServiceProvider())
            using (var encStream = new CryptoStream(output, csp.CreateEncryptor(des.Key, des.IV), CryptoStreamMode.Write))
            {
                int length = 0;
                byte[] buffer = new byte[256];
                bytesWritten = 0;
                while ((length = input.Read(buffer, 0, 256)) > 0)
                {
                    if (length < 256)
                    {
                        byte[] pad = new byte[256];
                        using (var rng = RNGCryptoServiceProvider.Create())
                        {
                            rng.GetBytes(pad);
                            for (int i = 0; i < 256 - length; i++)
                            {
                                buffer[length + i] = pad[i];
                            }
                        }
                        encStream.Write(buffer, 0, length);
                        bytesWritten += length;
                        break;
                    }
                    encStream.Write(buffer, 0, 256);
                    bytesWritten += length;
                }
                return output.ToArray();
            }
        }
    }
}
Alan Turing
  • 2,482
  • 17
  • 20
1

This is what I was looking for :D:D...thank you :D

   private static byte[] Encrypt(byte[] value, byte[] key)
    {
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider
                                                      {
                                                          Mode = CipherMode.ECB,
                                                          Padding = PaddingMode.None
                                                      };

        MemoryStream memoryStream = new MemoryStream();

        CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(key, key), CryptoStreamMode.Write);

        cryptoStream.Write(value, 0, value.Length);
        cryptoStream.Close();

        return memoryStream.ToArray();
    }
emirkljucanin
  • 804
  • 1
  • 9
  • 20
  • Firstly, You always should be cared about memory footprint in your code, that is why you should use using statement in FW objects, implemented with IDisposable interface, that is true, because if FW class is using IDisposable interface, in 99% of cases it will used unmanaged resources, like handles (memory handles also met), resources, etc. In some cases you can inform dependent object to force Close() on exit from specific stream, but this is more exception than rule. Secondly, please do not rewrite my code i had written already. And, in cryptography, less code is not the better, though ;) – Alan Turing Jan 20 '11 at 05:26
  • @Artur while your point about `IDisposable` is valid, people are free to write similar answers. – Marc Gravell Jan 20 '11 at 11:12
  • @Marc Gravell: It's OK, im thinkning about other thigs, I have already learned all tips and trick like answer stealing, rewriting, incorrect answer pushing, proning and other dirty thigs people do ;) Sometimes it looks really stupid when a bunch of idiots gives respect to highly rated, but incorrect answer from other user. Strangely, sometimes even monsters of industry (Erick) do not understand not even mention in their answers simpliest things ;) – Alan Turing Jan 21 '11 at 01:29