azure数据块中的PGP加密



我急需你的帮助:D我用PGP在python中写了一个代码,我有一个可信的公钥,我可以用这个代码完美地加密我的按摩,但当我在数据块上运行它时,我遇到了问题:gnuphome应该是一个目录,但它不是我想知道如何访问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)

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

我使用下面的代码片段,除了计划加密的文件和密钥之外,它不需要您访问任何目录中的任何内容。在代码中,我将公钥作为名为"publicb64"的秘密存储在密钥库中。如果你想从某个地方读asc版本,你可以把它读到KEY_PUB中。不要忘记使用pip-install-pppy安装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)) 

最新更新