以下加解密在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)); }
}
這樣終於實現了互通性!希望這對嘗試做同樣事情的人有所幫助。