简体   繁体   中英

AES encryption for hex key, IV and data in Java

I have to encrypt the hex string with key and IV also as Hex data. Please find the code i have used below. The problem is the string which i need to encrypt is in plaintext. I need to convert the plaintext into Hex and i do not know how to do that. Can any one help me with it ??

I have been confirming using the webtool,

http://aes.online-domain-tools.com/

static byte[] IV = {  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };

static String plaintext = "00010101010102020202020303030303"; 

static byte[] encryptionKey = {0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41  };

public static void main(String[] args) {
    try {

        System.out.println("==Java==");
        System.out.println("plain:   " + plaintext);

        byte[] cipher = encrypt(plaintext, encryptionKey);

        System.out.print("cipher:  ");
        for (int i = 0; i < cipher.length; i++) {
            // System.out.print(new Integer(cipher[i]) + " ");
            byte b = cipher[i];
            System.out.print(String.format("%02x", b & 0xFF) + " ");

        }

        String decrypted = decrypt(cipher, encryptionKey);

        System.out.println("decrypt: " + decrypted);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static byte[] encrypt(String plainText, byte[] encryptionKey)
        throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    SecretKeySpec key = new SecretKeySpec(encryptionKey, "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV));
    return cipher.doFinal(plainText.getBytes("UTF-8"));
}

public static String decrypt(byte[] cipherText, byte[] encryptionKey)
        throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    SecretKeySpec key = new SecretKeySpec(encryptionKey, "AES");
    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV));
    return new String(cipher.doFinal(cipherText), "UTF-8");
}

for the above data i should get,

b6 06 da b9 cd dc 2d 89 4b 49 0a ab 4e e7 dc 58

but instead am getting,

e3 62 34 3f ad 8b 89 37 57 81 91 31 ee 79 49 52 26 bf 40 cb d0 ce 36 bd 8a 04 6b af 34 d9 f3 d7

Thanks in Advance.

Probably the best thing to learn from this is that modern modes of operation such as CBC (as well as the underlying block cipher) operate on binary data, usually provided as a set of bytes.

In that sense it is probably best to stick to binary data as much as possible. Don't use more conversions than strictly necessary and only use hexadecimal encoding if it is meant for human consumption. Base 64 encoding can be used if you need to transfer binary using a text based representation.

Encoding/decoding should preferably be performed using a library such as Apache Commons Codec, Guava, Bouncy Castle etc., Java 8 still doesn't hex encode/decode byte arrays (sigh).


I've created a small code sample that outputs hex formatted as if it was a Java byte array literal. I'll leave it to you to create different versions.

static byte[] IV = {  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };

static byte[] plaintext = { 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03 }; 

static byte[] encryptionKey = {0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41  };

public static void main(String[] args) {
    try {

        System.out.println("==Java==");
        System.out.println("plain:   " + toJavaHex(plaintext));

        byte[] cipher = encrypt(plaintext, encryptionKey);

        System.out.println("cipher:  " + toJavaHex(cipher));

        String decrypted = toJavaHex(decrypt(cipher, encryptionKey));

        System.out.println("decrypt: " + decrypted);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static String toJavaHex(byte[] data) {
    StringBuilder sb = new StringBuilder(13 * data.length);
    for (int i = 0; i < data.length; i++) {
        if (i != 0) {
            sb.append(", ");
        }
        sb.append(String.format("(byte) 0x%02x", data[i]));
    }
    return sb.toString();
}

public static byte[] encrypt(byte[] plainText, byte[] encryptionKey)
        throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    SecretKeySpec key = new SecretKeySpec(encryptionKey, "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV));
    return cipher.doFinal(plainText);
}

public static byte[] decrypt(byte[] cipherText, byte[] encryptionKey)
        throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    SecretKeySpec key = new SecretKeySpec(encryptionKey, "AES");
    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV));
    return cipher.doFinal(cipherText);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM