如何使用pgcrypto addon加密postgres数据库中的列?
我正在使用postgres 9.3,我需要加密我的一个专栏,postgres是否也支持Aes加密或任何意思我可以实现它?
我需要加密PostgreSQL 9.6数据库中的一些列.被加密的数据本质上是敏感的; 但是,数据不是密码或其他身份验证凭据.需要对这些数据进行解密,以供用户进行统计分析和消费.
看完几个问题和答案后:
......并考虑以下评论:
...使用pgcrypto模块似乎最大的问题是在同一个数据库中存储密钥.
是否与将密钥存储在不同数据库中并通过外部数据包装器(如Postgresql_FDW)访问它的最佳实践一致?
我想从python中调用一些pgcrypto函数.即px_crypt.我似乎无法弄清楚正确的目标文件链接似乎.
这是我的代码:
#include <Python.h>
#include "postgres.h"
#include "pgcrypto/px-crypt.h"
static PyObject*
pgcrypt(PyObject* self, PyObject* args)
{
const char* key;
const char* setting;
if (!PyArg_ParseTuple(args, "ss", &key, &setting))
return NULL;
return Py_BuildValue("s", px_crypt(key, setting, "", 0));
}
static PyMethodDef PgCryptMethods[] =
{
{"pgcrypt", pgcrypt, METH_VARARGS, "Call pgcrypto's crypt"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initpypgcrypto(void)
{
(void) Py_InitModule("pypgcrypto", PgCryptMethods);
}
Run Code Online (Sandbox Code Playgroud)
和gcc命令和输出:
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/home/ionut/github/postgres/contrib/ -I/usr/include/postgresql/9.4/server/ -I/usr/include/python2.7 -c …
Run Code Online (Sandbox Code Playgroud) Postgres(或 Postgres 扩展)是否具有 uuid v5 函数(即采用 UUID 名称空间和字符串来生成新 UUID 的函数)?
如果不是本机函数,那么如何编写这个函数?
我正在使用 pgcrypto 来加密基础的某些列的 postgresql 基础。
Java 方面,我使用 JPA 将我的类“映射”到我的数据库。
目前,为了加密和解密 java 中的字段,我使用@ColumnTransformer
. 例子 :
@Column(name = "my_column", nullable = false)
@ColumnTransformer(read = "pgp_sym_decrypt(my_column, "my_password")", write = "pgp_sym_encrypt(?, "my_password")")
private String myColumn;
Run Code Online (Sandbox Code Playgroud)
这工作正常。但我想改变我的密码。我的应用程序将部署在多台服务器上,并且每台服务器的密码都不同。
我尝试了很多东西,我在互联网上搜索,但没有找到任何东西。所以我开始思考......这个注释是否可能?或者我应该以另一种方式加密/解密?也许在存储库中?
PS 这里有一些我尝试过的例子:
@ColumnTransformer(read = "pgp_sym_decrypt(my_column, ${application.security.pgcryptoPassword})", write = "pgp_sym_encrypt(?, ${application.security.pgcryptoPassword})")
Run Code Online (Sandbox Code Playgroud)
在我的配置文件中定义了 application.security.pgcryptoPassword
@Value("${application.security.pgcryptoPassword}")
private static final String pgcryptoPassword;
private static final String readCreator = "pgp_sym_decrypt(creation_aladdin_par, " + pgcryptoPassword + ")";
@ColumnTransformer(read = readCreator, write = "pgp_sym_encrypt(?, my_password)")
Run Code Online (Sandbox Code Playgroud)
编辑
我尝试了两件事:
private static final String pgcryptoPassword = …
Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码在 Java 中加密和解密,它似乎工作正常:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.SecureRandom;
public class MainNew {
public static void main(String[] args) {
String iv = getEncryptionIV();
System.out.println(" iv = "+iv);
String encryptedData= encryptWithIVandKey(iv,encryptionKey,"rakesh.test@eltropy.com");
System.out.println(encryptedData);
String decryptedData = decrypt (iv,encryptionKey,encryptedData);
System.out.println(decryptedData);
}
static final String encryptionKey = "rakesh1@n1111111";
static byte[] doFinal(int encryptMode, SecretKey key, String iv, byte[] bytes) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(encryptMode, key, new IvParameterSpec(DatatypeConverter.parseHexBinary(iv)));
byte[] data = cipher.doFinal(bytes);
return data;
} catch (Exception …
Run Code Online (Sandbox Code Playgroud) 我想用 aes 256 加密加密我的 postgreSQL 数据库中的 area_code 列。这是发生的事情.. 我能够轻松地加密它,但我无法解密它。
这是工作正常的加密查询:
update mytable t1 set area_code = t2.area_code from (select pgp_sym_encrypt(area_code,'Password', 'compress-algo=1, cipher-algo=aes256') as area_code,area_name from mytable) t2 where t1.area_name = t2.area_name;
Run Code Online (Sandbox Code Playgroud)
但是,如果我给出类似的内容,解密查询似乎不起作用
update mytable t1 set area_code = t2.area_code from (select pgp_sym_decrypt(area_code,'Password') as area_code,area_name from mytable) t2 where t1.area_name = t2.area_name;
Run Code Online (Sandbox Code Playgroud)
甚至当我尝试查看解密的 area_code 时
select pgp_sym_decrypt((select area_code from ci), 'Password') ;
Run Code Online (Sandbox Code Playgroud)
唯一有效的是当我使用单个记录并直接输入加密文本作为输入时。
select pgp_sym_decrypt('aes.encrypted.string.given.as.input', 'Password') ;
Run Code Online (Sandbox Code Playgroud) 我把pgcrypto扩展称为超级用户,如下所示:
CREATE EXTENSION pgcrypto;
Run Code Online (Sandbox Code Playgroud)
作为超级用户,我测试了它,它的工作原理是:
select gen_salt('bf');
gen_salt
-------------------------------
$2a$06$CJPcLcOBZnCEl.Z5ChrSbO
Run Code Online (Sandbox Code Playgroud)
但是,当以其他用户身份登录时,我收到如下错误:
select gen_salt('bf');
ERROR: function gen_salt(unknown) does not exist
Run Code Online (Sandbox Code Playgroud)
如何让pgcrypto库对所有用户可见?
谢谢.
我有以下用于创建表的查询,
CREATE TABLE IF NOT EXISTS company (
id uuid CONSTRAINT companyid PRIMARY KEY DEFAULT gen_random_uuid(),
name varchar(128) NOT NULL,
db_uri varchar(255) NOT NULL,
c_uri varchar(255) NOT NULL,
date_c timestamp DEFAULT now(),
date_m timestamp DEFAULT now()
) WITH (fillfactor=90);
Run Code Online (Sandbox Code Playgroud)
当我通过 pgAdminIII 运行它时,出现以下错误。
ERROR: function gen_random_uuid() does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function gen_random_uuid() does not exist
SQL state: 42883
Hint: No …
Run Code Online (Sandbox Code Playgroud) 我将密码哈希值存储在使用以下命令生成的 Postgresql 数据库中:
password_hash($password, PASSWORD_DEFAULT);
现在我还希望能够使用 Postgresql 和 pgcrypto 验证用户密码。
但 pgcrypto 的crypt()
功能无法验证现有的密码哈希值。
但是 - 我可以使用 PHP 验证 Postgresql 生成的密码哈希值password_verify
。
例如:
password_hash('hello', PASSWORD_DEFAULT);
$2y$10$fD2cw7T6s4dPvk1SFHmiJeRRaegalE/Oa3zSD6.x5WncQJC9wtCAS
Run Code Online (Sandbox Code Playgroud)
postgres=# SELECT crypt('hello', gen_salt('bf'));
crypt
--------------------------------------------------------------
$2a$06$7/AGAXFSTCMu9r.08oD.UulYR0/05q7lmuCTC68Adyu/aNJkzpoIW
Run Code Online (Sandbox Code Playgroud)
确认:
// php_verify with the Postgresql hash
php > var_dump(password_verify('hello', '$2a$06$7/AGAXFSTCMu9r.08oD.UulYR0/05q7lmuCTC68Adyu/aNJkzpoIW'));
bool(true)
Run Code Online (Sandbox Code Playgroud)
postgres=# SELECT crypt('hello', '$2y$10$fD2cw7T6s4dPvk1SFHmiJeRRaegalE/Oa3zSD6.x5WncQJC9wtCAS');
crypt
---------------
$2JgKNLEdsV2E
(1 Zeile)
Run Code Online (Sandbox Code Playgroud)
我的问题基本上是:
我有一个可以在本地运行的现有 Rails 7 / PostgreSQL 应用程序,并且当前部署在 Heroku 上。我正在将其迁移到运行 Ubuntu 22 LTS(最初是 Ubuntu 20 LTS)的 VPS,但是当我尝试运行迁移(直接或通过 Capistrano)时,它们失败并出现错误:
PG::UndefinedFunction:错误:函数 gen_random_uuid() 不存在
第一个(十个!)迁移失败了,因此,我没有重写历史记录并插入/修改早期迁移,而是通过手动创建扩展在服务器上修复了它pgcrypto
:
myApp$ sudo su - postgres
postgres$ psql
postgres=# \c myapp_production
myapp_production=# CREATE EXTENSION pgcrypto;
CREATE EXTENSION
myapp_production=# exit
Run Code Online (Sandbox Code Playgroud)
据我所知,我没有执行任何操作来在 Mac 上启用该扩展,我只是在第一次迁移中运行rails new myapp --database=postgresql
并使用了该扩展。create_table :people, id: :uuid do |t|
为什么我需要在 Ubuntu 上创建/启用此扩展,而不是 macOS 或 Heroku?
在我的 Mac 上,我使用 Postgres 14.5(psql (PostgreSQL) 14.5,Homebrew)。在 Ubuntu 22 LTS 服务器上,我还使用 Postgres 14.5(psql (PostgreSQL) 14.5、Ubuntu 14.5-0ubuntu0.22.04.1)。
当我第一次注意到它时,我在服务器上使用了不同版本的 PostgreSQL(psql …
例如,在Django中有一个repo:https://sourcegraph.com/github.com/dcwatson/django-pgcrypto.
SQLAlchemy手册中有一些讨论,但我使用的是非字节列:http://docs.sqlalchemy.org/en/rel_0_9/core/types.html
我正在使用SQLAlchemy在Heroku上运行Flask.
一个代码示例和/或一些讨论将是最受欢迎的.
我有oracle数据库移动到新的postgresql服务器.
有些表具有字段sesitive,并且这些表都通过DBMS_OBFUSCATION_TOOLKIT.DESENCRYPT/DESDECRYPT加密.
问题出在这里.postgresql的加密数据大小(bytea类型)的大小应该与oracle相同.
我试图用aes(加密/解密)完成它,它比原始数据大约三倍.(oracle使用des算法需要16byte,postgresql使用aes需要33byte,原始数据需要13byte.)
我也尝试了postgresql crypt,但是手册没有提到解密它的方式,限制了8byte的原始数据大小.
现在我真的需要加密方法,它采用尽可能小的加密数据大小并提供解密方法.
有没有好办法或其他选择?提前致谢.
pgcrypto ×13
postgresql ×13
encryption ×5
heroku ×2
java ×2
aes ×1
c ×1
crypt ×1
cryptography ×1
database ×1
hibernate ×1
php ×1
python ×1
python-c-api ×1
sql ×1
sqlalchemy ×1
ubuntu ×1