加密/解密的Java函数,如Mysql的AES_ENCRYPT和AES_DECRYPT

时间:2021-11-16 18:25:16

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

有没有办法得到与MySQL相同的结果

SELECT AES_ENCRYPT("text", "key") 

using a Java function?

使用Java函数?

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

如果可能的话,模拟AES_DECRYPT的另一个功能是什么。

2 个解决方案

#1


1  

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;
}

#2


1  

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

如果你需要代码解密算法就在这里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

只有加密才能进行第一次测试,然后只需要解密。顺便说一下,你必须安装'commons-codec-1.6.jar',这样你才能使用Hex类http://commons.apache.org/proper/commons-codec/download_codec.cgi

Greetings from Ecuador, Ibarra

来自厄瓜多尔的问候,Ibarra

#1


1  

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;
}

#2


1  

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

如果你需要代码解密算法就在这里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

只有加密才能进行第一次测试,然后只需要解密。顺便说一下,你必须安装'commons-codec-1.6.jar',这样你才能使用Hex类http://commons.apache.org/proper/commons-codec/download_codec.cgi

Greetings from Ecuador, Ibarra

来自厄瓜多尔的问候,Ibarra