Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DES encryption of 8 bytes plain text results in 16 bytes array

Tags:

c#

encryption

des

I am using this code to encrypt a 8 bytes PlainText with a 8 bytes Key but the result is always a 16 bytes array.

public static byte[] Encrypt(byte[] PlainText, byte[] key)
{
    MemoryStream ms = new MemoryStream();
    DESCryptoServiceProvider mDES = new DESCryptoServiceProvider();
    mDES.Mode = CipherMode.ECB;
    mDES.Key = key;

    CryptoStream encStream = new CryptoStream(ms, mDES.CreateEncryptor(), CryptoStreamMode.Write);
    BinaryWriter bw = new BinaryWriter(encStream);

    bw.Write(PlainText);
    bw.Close();
    encStream.Close();

    byte[] buffer = ms.ToArray();
    ms.Close();

    return buffer;
}

The first 8 bytes of the ouptut is what I expect but the rest I don't what it is.. is it something wrong with this code?

like image 331
eledu81 Avatar asked Jan 24 '23 08:01

eledu81


1 Answers

The input will be padded to the next block size using PKCS padding. 7 bytes input will be padded to 8. 8-15 to 16. 16-23 to 24 and so on and so forth.

like image 132
Remus Rusanu Avatar answered Jan 25 '23 21:01

Remus Rusanu