2
votes

I'm using dll for cryptography(des) which written by someone.Dll includes encrypt,decyrpt class,and that classes includes methods.Des required 8 bytes(64 bit) key.I describes a string for key.(a character is one byte).And then encoding bytes.

        string keyText= "abcdefghsdsdfsdfsdf";

        UTF8Encoding encoding = new UTF8Encoding();

        byte[] keyfile = new byte[8];
        keyfile = UTF8Encoding.UTF8.GetBytes(keyValue);

above way,even though i described size of byte array 8,size of byte array overflow,it s been length of string value.

Any suggestion. Thanks.

3
Shouldn't that be new byte[8], not 7? - Tim
DES has known vulnerabilities. Do not use it. Also, use System.Security.Cryptography. - SLaks
There is no need to initialize keyfile to an empty byte array. GetBytes returns a new byte array. - Marcelo Cantos
@Tim its 8,i wrote wrong sorry,@SLaks i prefer to write my code,but i have to use dll. - engcmreng
@engcmreng - Ok. I deleted my answer (since it was wrong on more than one account) Marcelo's answer is the one you should look at. - Tim

3 Answers

4
votes

To do it correctly, look at the PasswordDeriveBytes Class, and pick one of the overloaded methods.

Yes, you'll have to pick a Salt but that can be a fixed value baked into your program. A Salt does not have to be kept secret.

And to answer the technical, not security related question:

    byte[] keyfile = new byte[8];
    keyfile = UTF8Encoding.UTF8.GetBytes(keyValue)

This code creates 2 arrays. The first one is 8 bytes but it is immediately discarded. GetBytes() creates a new one with a size it determines. You need to hash that array and then you can pick the first 8 of the hash, which is what PasswordDeriveBytes does for you.

1
votes

There is no point in initializing keyfile to an empty byte array. GetBytes returns a new byte array that replaces the initial one, and it will be as large as necessary to encode the entire input.

To get just eight bytes of the encoding, why don't you just supply the first eight characters? They are all ASCII, so they'll occupy one byte each:

 byte[] keyfile = UTF8Encoding.UTF8.GetBytes(keyValue.Substring(0, 8));

More to the point, if you are using keyText as a password to synthesise a encryption key, this is a really bad idea. You are simply discarding any password characters after the first eight. Instead, use some form of cryptographic hash to convert passwords into keys. I just noticed Henk Holterman's answer points to an appropriate API.

0
votes

You have created an array with 7 items (not 8), but you are not using that array at all. The GetBytes method returns a new array, and you put the reference of that array in the variable, leaving the array that you created to the garbage collector.

If you want to get the first 8 bytes of an array, copy the contents instead of replacing the array:

string keyText= "abcdefghsdsdfsdfsdf";

UTF8Encoding encoding = new UTF8Encoding();

byte[] keyfile = new byte[8];
byte[] decoded = UTF8Encoding.UTF8.GetBytes(keyValue);

Array.Copy(decoded, keyfile, 8);