In postgreSQL, I use pgcrypto module to call pgp_sym_encrypt function (return bytea type) and save the result into a text column:
For example I have test table with column columnA(text):
CREATE EXTENSION pgcrypto;
insert into test (columnA) values (pgp_sym_encrypt('test','test'));
If I run in console, the result is similar to (escaped binary):
\303\015\004\007\003\002\022\261B\015\376\235\023\010j\3225\001\244l\253\332\026\037\\Q\305\253\365H\264\222\021\233\345\326\036Ma\346vwq\373\201\303\300\000\303\354\327:\017\020\036Q\201\025\210\364%\215$\017\304Y^_&\267
If I run in JDBC, the result is similar to (hex format):
public class Main {
public static void main(String[] args) throws Exception {
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/db", "postgres", "postgres");
Statement stmt = connection.createStatement();
String sql = "insert into test (columnA) values (pgp_sym_encrypt('test','test'))";
stmt.executeUpdate(sql);
connection.close();
}
}
Result:
\xc30d040703026859a0885a496bc66bd240018d9cbede4805fc5b733afc706d061ae613600962222f008ab2fc4e99cc1c87841e3929833066ba42697784276c49efa67655a399fa9f77264769a42eb7c85e
If I use pgp_sym_decrypt to decrypt, both are decrypted fluently, but I wonder why the results are in different formats. If I want the same format, how can I do?
UPDATE: I know they are escaped format & hext format and can be set as @Vao Tsun answer. But I wonder why, I thought they have to use same default format? Did the JDBC override format? Is there any config for JDBC for this default format or I have to call the set statement in every transaction?