【问题标题】:PGP encryption in azure databrickazure databrick 中的 PGP 加密
【发布时间】:2021-11-19 18:29:09
【问题描述】:

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

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)

【问题讨论】:

  • 嗨@Marian,我在下面的回答是否回答了您的问题?如果有,请将其标记为正确答案。

标签: python azure-databricks


【解决方案1】:

我怀疑这在本地成功运行。它不适用于数据块,因为它正在根目录中查找数据块不允许您访问的 .pnugp。

我使用下面的 sn-p 代码,除了您计划加密的文件和密钥之外,您不需要从任何目录访问任何内容。 在代码中,我将我的公钥作为名为“publicb64”的秘密存储在密钥库中。如果您想从某个地方读取 asc 版本,您可以将其读入 KEY_PUB。不要忘记使用 pip install pgpy 安装 pgpy。

#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)) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2012-05-06
    • 2019-02-12
    相关资源
    最近更新 更多