看了很多博客,特别是关于python API 操作 GPG 的,真垃圾,掉坑里了。
最近项目在传输文件,需要进行加密、解密操作。
用我的代码就行
# coding=utf-8
"""
Note! pip install pretty_bad_protocol
"""
import os
class gpg_encrypt_decrypt(object):
def __init__(self):
self.path = os.getcwd()
self.input_path = '/home/qazwsx/test_up/'
self.output_path = '/home/qazwsx/test_down/'
# self.homedir = '/home/hadoop/.gnupg'
def encrypt_file(self, out_file_path):
encryed_file = out_file_path.split('/')[-1][0:-3] + 'gng'
encry_pt = 'gpg --recipient 用户 --always-trust --output %s --encrypt %s' % (encryed_file, out_file_path)
print(encry_pt)
os.system(encry_pt)
return encryed_file
def decrypt_file(self, input_file):
decry_input_file = input_file[0:-3] + 'txt'
decry_str = "echo '密码' | gpg --batch --passphrase-fd 0 --recipient 用户 --decrypt %s > %s" %( input_file, decry_input_file)
os.system(decry_str)
print(decry_str)
return decry_input_file
if __name__ == '__main__':
ged = gpg_encrypt_decrypt()
ged.encrypt_file('mat_20190705180023_20190706_0.txt')
#ged = gpg_encrypt_decrypt()
#ged.decrypt_file('')
对于gpg 的理解,常用命令,看阮一峰的博客就够了!!!!
千万别看别的博客,坑爹。!!!!!!!1