云数据库 GAUSSDB-使用JDBC操作密态数据库:执行密态等值密文解密

时间:2023-11-24 09:43:09

执行密态等值密文解密

数据库连接接口PgConnection类型新增解密接口,可以对全密态数据库的密态等值密文进行解密。解密后返回其明文值,通过schema.table.column找到密文对应的加密列并返回其原始数据类型。

表1 新增org.postgresql.jdbc.PgConnection函数接口

方法名

返回值类型

支持JDBC 4

decryptData(String ciphertext, Integer len, String schema, String table, String column)

ClientLogicDecryptResult

Yes

参数说明:
  • ciphertext

    需要解密的密文。

  • len

    密文长度。当取值小于实际密文长度时,解密失败。

  • schema

    加密列所属schema名称。

  • table

    加密列所属table名称。

  • column

    加密列所属column名称。

    下列场景可以解密成功,但不推荐:

    • 密文长度入参比实际密文长。
    • schema.table.column指向其他加密列。此时将返回被指向的加密列的原始数据类型。
表2 新增org.postgresql.jdbc.clientlogic.ClientLogicDecryptResult函数接口

方法名

返回值类型

描述

支持JDBC4

isFailed()

Boolean

解密是否失败,若失败返回True,否则返回False。

Yes

getErrMsg()

String

获取错误信息。

Yes

getPlaintext()

String

获取解密后的明文。

Yes

getPlaintextSize()

Integer

获取解密后的明文长度。

Yes

getOriginalType()

String

获取加密列的原始数据类型。

Yes

// 通过非密态连接、逻辑解码等其他方式获得密文后,可使用该接口对密文进行解密
import org.postgresql.jdbc.PgConnection;
import org.postgresql.jdbc.clientlogic.ClientLogicDecryptResult;

// conn为密态连接
// 调用密态PgConnection的decryptData方法对密文进行解密,通过列名称定位到该密文的所属加密列,并返回其原始数据类型
ClientLogicDecryptResult decrypt_res = null;
decrypt_res = ((PgConnection)conn).decryptData(ciphertext, ciphertext.length(), schemaname_str,
        tablename_str, colname_str);
// 检查返回结果类解密成功与否,失败可获取报错信息,成功可获得明文及长度和原始数据类型
if (decrypt_res.isFailed()) {
    System.out.println(String.format("%s\n", decrypt_res.getErrMsg()));
} else {
    System.out.println(String.format("decrypted plaintext: %s size: %d type: %s\n", decrypt_res.getPlaintext(),
        decrypt_res.getPlaintextSize(), decrypt_res.getOriginalType()));
}
support.huaweicloud.com/fg-gaussdb-cent/gaussdb-48-0014.html