Is there any sample Java code that encrypts AES just like this site?

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

If you go to this site and enter the following:

"Key In Hex": 00000000000000000000000000123456 "Plain Text in Hex": 00000000000000000000000000000000 

And click the "Encrypt" button, you will see that the encrypted text is in hexadecimal format:

 3fa9f2a6e4c2b440fb6f676076a8ba04 

Is there a Java program in which I can do this (i.e. is there an AES library that will enter “Key In Hex” above with “Plain Text in Hexadecimal Expression” above and generate Encrypted Text in Hex above?)

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

+4
source share
2 answers

See the code below for a standard way to do this using 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)); } } 

Print

3FA9F2A6E4C2B440FB6F676076A8BA04

+9
source

Instead of converting bytes to HEX, you can also convert to Base64. I like to use Apache Commons for this. Here is an example.

To compile you will need an additional Apache Commons Codec banner, which is available here:

http://commons.apache.org/proper/commons-codec/download_codec.cgi

 import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class Encryptor { public static String encrypt(String key1, String key2, String value) { try { IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(value.getBytes()); System.out.println("encrypted string:" + Base64.encodeBase64String(encrypted)); return Base64.encodeBase64String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; } public static String decrypt(String key1, String key2, String encrypted) { try { IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted)); return new String(original); } catch (Exception ex) { ex.printStackTrace(); } return null; } public static void main(String[] args) { String key1 = "Bar12345Bar12345"; // 128 bit key String key2 = "ThisIsASecretKet"; System.out.println(decrypt(key1, key2, encrypt(key1, key2, "Hello World"))); } } 
0
source

Source: https://habr.com/ru/post/1442519/


All Articles