Ebean manual decryption

眉间皱痕 提交于 2019-12-08 10:54:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!