对于本地查询,Oracle函数调用有时会失败

时间:2019-07-17 10:18:08

标签: java oracle function

我正在使用继承的一些oracle函数进行密码混淆。

这是日志文件中的错误:

    2019-07-17 12:50:43 DEBUG JobDAO:2179 - Decrypting with SQL=select XXX_MONITORING_CONFIGURATION.ENCRYPT_DECRYPT_PASSWORD.get_decrypted_password(?,?,?,?) from dual
2019-07-17 12:50:43 DEBUG JobDAO:2180 - Decrypting parameters=SERVER_SERVICES,PASSWORD,SERVER_SERVICES_ID,101
2019-07-17 12:50:43 DEBUG JobDAO:2192 - Error while decrypting encrypted password for SERVER_SERVICES--PASSWORD--SERVER_SERVICES_ID--101 org.hibernate.exception.GenericJDBCException: could not execute query
2019-07-17 12:50:43 DEBUG MonitorJob:2087 - Could not decrypt password for tomcat7-8888
2019-07-17 12:50:43 DEBUG JobDAO:2179 - Decrypting with SQL=select XXX_MONITORING_CONFIGURATION.ENCRYPT_DECRYPT_PASSWORD.get_decrypted_password(?,?,?,?) from dual
2019-07-17 12:50:43 DEBUG JobDAO:2180 - Decrypting parameters=SERVER_LOGFILES,LOGFILE_PASSWORD,SERVER_LOGFILES_ID,101

请注意,第二次调用似乎成功,并且在SQL Developer中手动执行查询也可以。

什么会导致一个呼叫失败而下一个呼叫正常?

java代码是:

public String getDecryptedPassword(String tableName, String columnName,
        String tableId, String tableIdVal)
{
    try
    {
        String callFnEncDec = "select XXX_MONITORING_CONFIGURATION.ENCRYPT_DECRYPT_PASSWORD.get_decrypted_password(?,?,?,?) from dual";
        logger.debug("Decrypting with SQL=" + callFnEncDec);
        //logger.debug("Decrypting parameters=" + tableName + "," + columnName + "," +tableId + "," +tableIdVal);

        Query callFnEncDecQuery = entityManager
                .createNativeQuery(callFnEncDec);
        callFnEncDecQuery.setParameter(1, tableName);
        callFnEncDecQuery.setParameter(2, columnName);
        callFnEncDecQuery.setParameter(3, tableId);
        callFnEncDecQuery.setParameter(4, tableIdVal);

        return (String) callFnEncDecQuery.getSingleResult();
    } catch (Exception e)
    {
        logger.debug("Error while decrypting encrypted password for "
                + tableName + "--" + columnName + "--" + tableId + "--"
                + tableIdVal + " " + e.getMessage());
    }
    return null;

}

解密功能是这样的:

  l_key RAW(128) := utl_raw.cast_to_raw('secret_password');

FUNCTION get_decrypted_password( table_name IN varchar2,column_name IN varchar2,table_id IN varchar2,table_id_val IN varchar2 ) RETURN VARCHAR2
  IS
     encrypted_pas varchar2(100);
     decrypted_pas varchar2(100);

   BEGIN
      EXECUTE IMMEDIATE 'select ' || column_name || ' from ' || table_name || ' where ' || table_id || ' = ' || table_id_val
         INTO encrypted_pas;
         Select decrypt_val(encrypted_pas) into decrypted_pas from dual;
          return decrypt_val(encrypted_pas);
  END get_decrypted_password;

END encrypt_decrypt_password;

FUNCTION decrypt_val( p_val IN varchar2 ) RETURN VARCHAR2
  IS
    l_decrypted RAW(2048);
    l_val RAW(2048) := utl_raw.cast_to_raw(p_val);
   BEGIN
         l_decrypted := dbms_crypto.decrypt
                ( src =>  l_val,
                  typ => dbms_crypto.des_cbc_pkcs5,
                  key => l_key );

         return utl_raw.cast_to_varchar2(l_decrypted);

  END decrypt_val;

更新:我将“ PASSWORD”列重命名为“ SERVER_PASSWORD”,以排除此处的Oracle关键字问题。问题仍然存在。

0 个答案:

没有答案