Triple DES encryption
Asked Answered
A

3

6

Please note that the issue I am having here is with the key size. At first, based on the comments included in the below code, I figured that my key needed to be 24 bytes (192 bits). This didn't work so I gave 16, 32, and 8 byte keys a shot - nothing seems to be working. By "not working" I mean that after my text has been encrypted and decrypted it does not hold the same value as my original text.

Example:

Original Text: 'Example test this should work '

Encrypted Text: ¸¹pÕô6

Decrypted Text: 'Example '

Here are the two functions I am using (Encrypt / Decrypt functions). Also I will include how I am calling each function.

        // 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
        // plain-text should be 8-bytes, key should be 24 bytes.
        public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
        {
            // Create a new 3DES key.
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

            // Set the KeySize = 192 for 168-bit DES encryption.
            // The msb of each byte is a parity bit, so the key length is actually 168 bits.

            des.KeySize = 192;
            des.Key = key;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.None;

            ICryptoTransform ic = des.CreateEncryptor();

            byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);

            return enc;
        }

        public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
        {
            // Create a new 3DES key.
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

            // Set the KeySize = 192 for 168-bit DES encryption.
            // The msb of each byte is a parity bit, so the key length is actually 168 bits.
            des.KeySize = 192;
            des.Key = key;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.None;

            ICryptoTransform ic = des.CreateDecryptor();

            byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);

            return dec;
        }

// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text), StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));

// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text), StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));

Thank you for any help,

Evan

Apogee answered 9/8, 2011 at 0:51 Comment(2)
Doesn't solve your problem, but the CipherMode.ECB strikes me as funny given the website... codinghorror.com/blog/2009/05/…Flunkey
What are you getting at here ... ?Apogee
H
2

The clue is in the name of the function you're using: TripleDesEncryptOneBlock

This method only encrypts one block of the input string (8 bytes or 64 bits). To encrypt the entire string you need to chain multiple calls to this method.

Hash answered 9/8, 2011 at 1:1 Comment(3)
It seems very odd that I'd have to call this method multiple times. Is there some way to modify this code so that I don't need to do this?Apogee
The purpose is so you can implement different chaining methods such as CBC or CFB.Hash
What is the propery way to call this function in blocks of 8? Could you give me a hand with this? Should I just split the data into 8 byte pieces and call it?Apogee
C
2

Use this:

byte[] enc = ic.TransformFinalBlock(plainText, 0, plainText.Length);

I hope it will encrypt/decrypt your whole string. Also you will need not to call this method multiple times

Cloraclorinda answered 12/12, 2012 at 7:20 Comment(0)
S
0

Your problem is here:

byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
                                                  ^

you only encode the first 8 characters of your array So when you decode, there are only those 8 characters to decode, which results in 'Example '.

if you want to encode all your text, you have to increase that value. But be carefull, if you use PaddingMode.None it will fail if the lenght of the array-to-be-encoded is not a multiple of 8.

I added some padding to my text like this:

int length = plainText.Length / 8;
if(plainText.Length%8 > 0)
{
    length++;
}
byte[] paddedText = new byte[length * 8];
plainText.CopyTo(paddedText, 0);
byte[] enc = ic.TransformFinalBlock(paddedText, 0, length * 8);
Sauers answered 19/3, 2020 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.