首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >azure数据库中的PGP加密

azure数据库中的PGP加密
EN

Stack Overflow用户
提问于 2021-11-19 18:29:10
回答 1查看 217关注 0票数 1

我非常需要你的帮助:我用PGP用python写了一个代码,我有一个可信的公钥,我可以用这个代码完美地加密我的消息,但当我在数据砖上运行它时,我遇到了问题: gnupghome应该是一个目录,而不是我想知道如何访问数据库中的目录。

代码语言:javascript
运行
复制
import gnupg
from pprint import pprint
import os

gpg = gnupg.GPG(gnupghome='/root/.pnugp')
key_data = open("/dbfs/mnt/xxxx/SCO/oracle/xxx/Files/publickey.asc").read()
    
import_result = gpg.import_keys(key_data)
pprint(import_result.results)
with open("/dbfs/mnt/xxxxx-storage/SCO/oracle/xxx/Files/FileToEncrypt.txt",'rb') as f:
  status = gpg.encrypt_file(
    f, recipients=['securxxxxfertuca@xx.ca'],
    output='my-encrypted.txt.gpg')
  print( 'ok: ', status.ok)
  print ('status: ', status.status)
  print ('stderr: ', status.stderr)
EN

回答 1

Stack Overflow用户

发布于 2021-11-20 00:28:31

我怀疑这在本地运行成功。它在databricks上不起作用,因为它在根目录中查找data bricks不允许访问的.pnugp。

我使用下面的代码片段,除了您计划加密的文件和密钥之外,它不需要您访问任何目录中的任何内容。在代码中,我将我的公钥作为一个名为‘public b64’的秘密存储在密钥库中。如果你想从某个地方读取asc版本,你可以直接把它读入KEY_PUB。不要忘记使用pip install pgpy安装pgpy。

代码语言:javascript
运行
复制
#Encrypting a file using public key
import pgpy
from pgpy.constants import PubKeyAlgorithm, KeyFlags, HashAlgorithm, SymmetricKeyAlgorithm, CompressionAlgorithm
from timeit import default_timer as timer
import base64 
import io
 
KEY_PUB = base64.b64decode(publicb64).decode("ascii").lstrip()  
#print(KEY_PUB)

pub_key = pgpy.PGPKey()
pub_key.parse(KEY_PUB)
pass
# -READ THE FILE FROM MOUNT POINT-----------------
with io.open('/dbfs/mnt/sample_data/california_housing_test.csv', "r",newline='') as csv_file:
    input_data = csv_file.read()                   # The io and newline retains the CRLF
    
t0 = timer()
#PGP Encryption start
msg = pgpy.PGPMessage.new(input_data)
###### this returns a new PGPMessage that contains an encrypted form of the original message
encrypted_message = pub_key.encrypt(msg)
pgpstr = str(encrypted_message)
with open('/dbfs/mnt/sample_data/california_housing_test.csv.pgp', "w") as text_file:
    text_file.write(pgpstr)
print("Encryption Complete :" + str(timer()-t0)) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70039364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档