Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any sample Java code that does AES encryption exactly like this website?

http://www.hanewin.net/encrypt/aes/aes-test.htm

If you go to this website and enter the following:

"Key In Hex":        00000000000000000000000000123456

"Plain Text in Hex": 00000000000000000000000000000000

And click on "Encrypt" button you will see the ciphertext in hex is:

3fa9f2a6e4c2b440fb6f676076a8ba04

Is there a Java program out there that I can do this (I.e. Is there an AES library that will input the "Key In Hex" above with the "Plain Text In Hex" above and generate the Ciphertext in Hex above? )?

I would appreciate any advice or links to Java sample code that does this.

like image 231
user1068636 Avatar asked Dec 20 '22 14:12

user1068636


1 Answers

See the code below for the standard way to do this with JCE classes.

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

public class EncryptionExample {

  public static void main(String[] args) throws Exception {
    final String keyHex = "00000000000000000000000000123456";
    final String plaintextHex = "00000000000000000000000000000000";

    SecretKey key = new SecretKeySpec(DatatypeConverter
        .parseHexBinary(keyHex), "AES");

    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, key);

    byte[] result = cipher.doFinal(DatatypeConverter
        .parseHexBinary(plaintextHex));

    System.out.println(DatatypeConverter.printHexBinary(result));
  }
}

Prints:

3FA9F2A6E4C2B440FB6F676076A8BA04

like image 153
Duncan Jones Avatar answered Dec 24 '22 01:12

Duncan Jones