以下加解密在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 使用相同的密鑰進(jìn)行解密?
MySQL內(nèi)部使用OpenSSL算法,以EVP_BytesToKey作為推導(dǎo)函數(shù)。看看這個(gè)網(wǎng)址
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!!' ));
有一個(gè) JAR 支持此 EVP_BytesToKey 密鑰派生函數(shù)。
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)); }
}
這樣終于實(shí)現(xiàn)了互操作性!希望這對(duì)嘗試做同樣事情的人有所幫助。