问题
I have successfully setup encryption for a field in my model using the answer provided in this SO post. But I would like to know how to manually decrypt the field from SQL client for debugging purposes. I want this information for Mysql (Prod database) and preferably for H2 (dev database). As per E-bean documentation Mysql uses AES_ENCRYPT/AES_DECRYPT and H2 uses ENCRYPT/DECRYPT functions.
@Encrypted
@Column(columnDefinition="varchar(50)")
public String password;
Note: I have set the datatype of encrypted field as varchar instead of binary as shown below. Hence Ebean might additionally Hex the generated binary data.
class CustomEncryptKey implements EncryptKey{
private String tableName;
private String columnName;
public CustomEncryptKey(String tableName, String columnName){
this.tableName = tableName;
this.columnName = columnName;
}
@Override
public String getStringValue() {
return "my-encryption-key";
}
}
回答1:
I managed to Identify the answer. For My-SQL
to decrypt:
SELECT CAST(AES_DECRYPT(encrypted-field,'my-encryption-key') as CHAR(50)) from table
to encrypt:
SELECT AES_ENCRYPT(encrypted-field,'my-encryption-key') from table;
For H2:
encrypt:
ENCRYPT('AES', STRINGTOUTF8('<encryption-key>'), STRINGTOUTF8('<text to be encrypted>'))
decrypt:
TRIM(CHAR(0) FROM UTF8TOSTRING(DECRYPT('AES', STRINGTOUTF8('<encryption-key>'), '<text to be encrypted>')))
来源:https://stackoverflow.com/questions/30435718/ebean-manual-decryption