Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

patterns in ciphertext

39 views
Skip to first unread message

vaneric

unread,
Nov 15, 2008, 12:25:41 AM11/15/08
to
i was trying out some code to demonstrate that patterns in plaintext
cause patterns to appear in ciphertext when ECB is used for encrypt/
decrypt operations.I used bountycastle as provider.
I used DES and AES as the cipher algorithms and gave proper length
keybytes for creating SecretKeySpec.

i used an input byte[] as plaintext and another byte[] for creating
SecretKeySpec as below

byte[] input = new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
byte[]keyBytes = new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};//DES key
SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS7Padding", "BC");
System.out.println("input : " + Utils.toHex(input,input.length));
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText =new byte[cipher.getOutputSize(input.length)];
int ctLength =cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
System.out.println("cipher: " + Utils.toHex(cipherText, ctLength)+ "
bytes: " + ctLength);


----------------------
i used a toHex(byte[]data,int length) method to print the hex of a byte
[]
public static String toHex(byte[] data, int length) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i != length; i++)
{
int v = data[i] & 0xff;
buf.append(digits.charAt(v >> 4));
buf.append(digits.charAt(v & 0xf));
}
return buf.toString();
}
--------------------------

when i printed out the hex of plaintext and ciphertext i got this
>>
input : 000102030405060708090a0b0c0d0e0f0001020304050607
cipher:
e1b246e5a7c74cbc92c9db45300b932fe1b246e5a7c74cbce481a8d39714d0de
bytes: 32

Here the pattern is evident.The plaintext block 0001020304050607
causes the pattern e1b246e5a7c74cbc in ciphertext.

Then i tried the same with AES providing keybytes of
byte[]keyBytes = new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };

and creating
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");

then the program created
>>
input : 000102030405060708090a0b0c0d0e0f0001020304050607
cipher:
0060bffe46834bb8da5cf9a61ff220ae5cbbd8811851a91781d5d358213579fe
bytes: 32

Here i couldn't find the patterns in ciphertext eventhough i am using
ECB.
Can someone explain this?I am a beginner in this field and so
understand little of the mechanisms.
thanks
eric

rossum

unread,
Nov 15, 2008, 6:14:35 AM11/15/08
to

In your first example you were using DES, which has a 64 bit
blocksize, 8 bytes. Your plaintext is three blocks, plus padding:

block1: 0001020304050607
block2: 08090a0b0c0d0e0f
block3: 0001020304050607
block4: ----padding-----

As expected blocks 1 and 3 produce the same cyphertext, which is why
ECB mode should never be used for anything beyond toy systems.

Your second example used AES, which has 128 bit blocks, 16 bytes, so
with your padded plaintext there are not two identical blocks:

block1: 000102030405060708090a0b0c0d0e0f
block2: 0001020304050607----padding-----

These two blocks are different and so produce different cyphertext.

If you adjusted your AES plaintext so that the second half of the
first block matched the PKCS7 padding used in the last block then you
would see two repeated 128 bit cyphertext blocks:

block1: 00010203040506070808080808080808
block2: 0001020304050607----padding-----

For a good illustration (literally) of the failings of ECB mode have a
look at the Wikipedia article:
http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation

For real systems use Cypher Block Chaining (CBC) or Counter (CTR)
modes.

rossum

vaneric

unread,
Nov 15, 2008, 12:13:56 PM11/15/08
to
On Nov 15, 4:14 pm, rossum <rossu...@coldmail.com> wrote:
> Your second example used AES, which has 128 bit blocks, 16 bytes, so with your padded plaintext there are not two identical blocks:
>

thanks rossum.
eric

0 new messages