5

Is there any way to get the same result as doing in MySQL

SELECT AES_ENCRYPT("text", "key") 

using a Java function?

And if possible, what's the other function to simulate the AES_DECRYPT.

hex494D49
  • 9,109
  • 3
  • 38
  • 47
infinito
  • 1,985
  • 1
  • 15
  • 16

3 Answers3

4

If you need the code to decrypt the algorithm is here JAVA

 public static String aes_decrypt(String passwordhex, String strKey) throws Exception {
    try {
        byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);

        SecretKey key = new SecretKeySpec(keyBytes, "AES");
        Cipher decipher = Cipher.getInstance("AES");

        decipher.init(Cipher.DECRYPT_MODE, key);

        char[] cleartext = passwordhex.toCharArray();

        byte[] decodeHex = Hex.decodeHex(cleartext);

        byte[] ciphertextBytes = decipher.doFinal(decodeHex);

        return new String(ciphertextBytes);

    } catch (Exception e) {
        e.getMessage();
    }
    return null;
}

It received a standard hex format string but variable and returns the password. Test with those in main method

    System.out.println(aes_encrypt("your_string_password", "your_string_key"));
    System.out.println(aes_decrypt("standard_hex_format_string ", "your_string_key"));

firstable test only with encrypt, then just with decrypt. By the way you must install 'commons-codec-1.6.jar' so you can use the Hex class http://commons.apache.org/proper/commons-codec/download_codec.cgi

Greetings from Ecuador, Ibarra

4

Ok, I've managed to get it working like this.

MySQL Query:

SELECT HEX(aes_encrypt("password", "0123456789012345"));

Java function:

public static String aes_encrypt(String password, String strKey) {
    try {
        byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);

        SecretKey key = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] cleartext = password.getBytes("UTF-8");
        byte[] ciphertextBytes = cipher.doFinal(cleartext);

        return new String(Hex.encodeHex(ciphertextBytes));

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } return null;
}
infinito
  • 1,985
  • 1
  • 15
  • 16
  • 3
    Please, please, please get rid of this ugly error handling. And don't use printStackTrace(), ever. Either rethrow the exception, or use logging (either JDK or 3rd party). If you're not going to handle the errors in any useful way, just try { ... some code ... } catch(Exception e) { throw new RuntimeException(e); }. At the moment, if an error occurs, a stack trace will print somewhere (maybe, depending on what platform you're on), and then your code will proceed as if nothing went wrong. – Jesse Barnum Jul 02 '11 at 05:16
  • Probably also worth pointing out that that Hex class is from Apache Commons: http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html – Jeremy Logan Feb 04 '12 at 21:44
  • any one have equivalent java implemetation for Mysql AES CBC-256 implementation – MasterCode Nov 03 '17 at 05:14
  • Dear infinito, I have 2 notes: 1)you switch the password with text in mysql and java code, 2) you can use the UTF-8 instead of ASCII -> should be SELECT HEX(aes_encrypt("0123456789012345","password")); byte[] keyBytes = Arrays.copyOf(password.getBytes("UTF-8"/*"ASCII"*/), 16); ..... byte[] cleartext = strKey.getBytes("UTF-8"); – Hazim Jan 26 '21 at 09:45
0

in my case i just code like this

 private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
    public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    for (int j = 0; j < bytes.length; j++) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = HEX_ARRAY[v >>> 4];
        hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
    }
    return new String(hexChars);
 }

and than create method aes 128

public static String aes_encrypt(String password, String strKey) {
        try {
            byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);

            SecretKey key = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);

            byte[] cleartext = password.getBytes("UTF-8");
            byte[] ciphertextBytes = cipher.doFinal(cleartext);

            return new String(bytesToHex(ciphertextBytes));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } 
        return null;
}

example : mysql code

SELECT HEX(aes_encrypt("text", "0123456889812345"))

java code

System.out.println(aes_encrypt("text", "0123456889812345"));