以下加解密在mysql(aes-256-cbc)模式下工作正常
SET block_encryption_mode = 'aes-256-cbc'; select cast( aes_decrypt( from_base64('StThdNXA+CWvlg+of/heJQ=='), sha2(concat('ssshhhhhhhhhhh!!','ENCRYPTION_KEY$&'),256), 'ssshhhhhhhhhhh!!' ) as char); select to_base64(aes_encrypt( 'test_value', sha2(concat('ssshhhhhhhhhhh!!','ENCRYPTION_KEY$&'),256), 'ssshhhhhhhhhhh!!' ));
我正在尝试解密在 mysql 中加密的值,但没有成功。
以下是我的mysql查询sha256(salt key)中的密钥
select sha2(concat('ssshhhhhhhhhhh!!','ENCRYPTION_KEY$&'),256);
与我在 java 中能够获得的值相同:
Hashing.sha256().hashString("ssshhhhhhhhhhh!!ENCRYPTION_KEY$&", StandardCharsets.UTF_8).toString();
是否有一种自定义方法可以使充气城堡/其他 API 使用相同的密钥进行解密?
P粉2390894432024-03-28 20:33:42
MySQL内部使用OpenSSL算法,以EVP_BytesToKey作为推导函数。看看这个网址
MySQL加密解密示例:
SET block_encryption_mode = 'aes-128-cbc'; SET block_encryption_mode = 'aes-256-cbc'; select cast( aes_decrypt( from_base64('MKicK+vAcZkq/g3wpTKxVg=='), 'ENCRYPTION_KEY$&', 'ssshhhhhhhhhhh!!' ) as char); select to_base64(aes_encrypt( 'test_value', 'ENCRYPTION_KEY$&', 'ssshhhhhhhhhhh!!' ));
有一个 JAR 支持此 EVP_BytesToKey 密钥派生函数。
JAR : not-going-to-be-commons-ssl-0.3.19
byte[] key = "ENCRYPTION_KEY$&".getBytes(StandardCharsets.UTF_8); byte[] iv = "ssshhhhhhhhhhh!!".getBytes(StandardCharsets.UTF_8); String cipher = "AES-128-CBC"; @Test public void testEncrypt() throws Exception { byte[] data = "test_message".getBytes(StandardCharsets.UTF_8); byte[] encrypted = OpenSSL.encrypt(cipher, key, iv, data, true); System.out.println(new String(encrypted)); } @Test public void testDecrypt() throws GeneralSecurityException, IOException { byte[] encrypted = "rQ8Y0ClNu5d4ODZQ3XcQtw==".getBytes(StandardCharsets.UTF_8); byte[] decrypted = OpenSSL.decrypt(cipher, key, iv, encrypted); System.out.println(new String(decrypted)); }
}
这样终于实现了互操作性!希望这对尝试做同样事情的人有所帮助。