Purush Pawar
Purush Pawar

Reputation: 4283

Java AES encryption weird result

I'm trying to encrypt a simple string using AES algorithm. Everything seems fine and i'm able to decrypt it. Just for curiosity i printed the encrypted data to the console and i was surprised to see it.

Input String : raw text string.

Encrypted data : Díå¼[¶cE¶Ÿ¸’E‚;èýaó1ÒŽ&ýÈZ

Decrypted Data : raw text string.

Can anyone explain me why the encrypted data bits are twice long as that of input string..?

Upvotes: 0

Views: 453

Answers (2)

John Watts
John Watts

Reputation: 8865

Encryption works on blocks of data of a fixed size. Appropriate padding is added during encoding and removed during decoding.

Upvotes: 1

f_puras
f_puras

Reputation: 2505

I would not rely on displaying a special character String to tell its length. On encryption, you typically get a byte[] for which length should give you the exact value. If you want it printed, a hex representation looks clearer:

public static void showHex(byte[] data) {
    final String HEXDIGITS = "0123456789abcdef";
    StringBuilder res = new StringBuilder();

    for (int i = 0; i < data.length; i++) {
        int v = data[i] & 0xff;
        res.append(HEXDIGITS.charAt(v >> 4));
        res.append(HEXDIGITS.charAt(v & 0xf));
    }

    System.out.println(res);
}

Upvotes: 0

Related Questions