无法将Java对象插入Aurora Mysql表的Blob中

时间:2019-07-18 16:53:37

标签: java mysql jdbc blob amazon-rds-aurora

我们最近迁移到AWS中的Aurora Mysql-似乎在插入Java对象(用于加密字符串的SecretKey)时遇到问题。当我查询数据库时,它现在显示为0字节-因此我们无法解密也存储为blob的String。

此方法在迁移到Aurora RDS MySQL实例之前是可行的-但现在不起作用,并且在插入之后-它显示为0个字节(my_object字段。

现在,我正试图弄清楚如何使它正常工作,以便我们可以存储SecretKey并将其拉回以进行解密。

这是执行加密的代码:

byte[] encBytes = null;
String xform = "Blowfish";   
int klength = 128;
try{
 conn = hs.getEinsofConnection(sc);
 SecretKey key = this.generateKey(xform,klength);

 byte[] dataBytes = inputstr.getBytes("UTF8");            

 encBytes = encrypt(dataBytes, key, xform);

 //this is the object that got encrypted
 EncBlob eb = new EncBlob();
 eb.setSiteID(sc.getSite().getSiteID());
 eb.setMyObject(Base64.encode(encBytes));
 eb.setClassName("");
 eb.db = hs.getDBType();
 eb.save(conn); 

 //this is the object that is the SecretKey so we can use to decrypt
 EncBlob eb2 = new EncBlob();
 eb2.setSiteID(sc.getSite().getSiteID());
 eb2.setMyObject(key);
 eb2.setClassName(key.getClass().getName());
 eb2.db = hs.getDBType();
 eb2.save(conn);

这是加密方法:

private static byte[] encrypt(byte[] inpBytes,SecretKey key, String xform) throws Exception {
    byte[] iv = {(byte)0, (byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)0};
  Cipher cipher = Cipher.getInstance(xform);
  cipher.init(Cipher.ENCRYPT_MODE, key);
  return cipher.doFinal(inpBytes);
}

这是将对象存储到数据库中的实际Mysql语句代码:

private static final String INSERT_SQL = "insert into enc_blob(site_id, for_einsof_object_id, class_name, my_blob) values(?,?,?,?)";

StringBuilder insert_sql = new StringBuilder(INSERT_SQL);

statement = conn.prepareStatement(insert_sql.toString());
      statement.setInt(1, this.site_id);
      statement.setInt(2,this.for_einsof_object_id);
      statement.setString(3, this.class_name);
      statement.setObject(4,this.my_object);
//execute insert SQL
        statement.execute();
        statement.close();

//in the object, the "my_object" variable is defined as this:
private Object      my_object = null;

我得到的错误是解密时的类强制转换异常-但在我的探索中,我发现该对象未存储到数据库中-对象字段中为0字节...所以看我需要进行更改以解决此问题。

任何帮助都将不胜感激。

0 个答案:

没有答案