patterns in ciphertext

Discussion in 'Java' started by vaneric, Nov 15, 2008.

  1. vaneric

    vaneric Guest

    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 & 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
     
    vaneric, Nov 15, 2008
    #1
    1. Advertising

  2. vaneric

    vaneric Guest

    On Nov 15, 4:14 pm, rossum <> 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
     
    vaneric, Nov 15, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Josh28
    Replies:
    0
    Views:
    479
    Josh28
    Nov 29, 2004
  2. Siz
    Replies:
    1
    Views:
    7,129
    Catherine Lowery
    Dec 12, 2004
  3. crichmon
    Replies:
    4
    Views:
    511
    Mabden
    Jul 7, 2004
  4. jimgardener
    Replies:
    1
    Views:
    3,903
    jimgardener
    Dec 5, 2008
  5. MRAB
    Replies:
    0
    Views:
    756
Loading...

Share This Page