-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_encrypt.py
More file actions
42 lines (34 loc) · 1.07 KB
/
python_encrypt.py
File metadata and controls
42 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os, json
import dict2xml
from dict2xml import dict2xml
from pyDes import *
import bz2
import boto3
# Parameters
json_dir = "jsons/"
xml_dir = "xmls/file.xml"
password = '12345678'
bucketName = "s3bucketname"
s3_file = "enc_file.xml"
# Read the JSON file from the JSON directory Convert to XML
def convertjson2xml(json_dir):
for file in os.listdir(json_dir):
full_filename = "%s/%s" % (json_dir, file)
with open(full_filename,'r') as fi:
json_dict = json.load(fi)
xml_files = dict2xml(json_dict, 'template')
return xml_files
# Encrypt the XML file
def encrypt(xml_dir,password):
k = des(password, CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
d = k.encrypt(xml)
return d
# Execute the above functiosn to convert and encrypt the file
xml = convertjson2xml(json_dir)
encrypted_data = encrypt(xml,password)
enc_file = open(xml_dir,'wb')
enc_file.write(encrypted_data)
enc_file.close()
# Upload the encrypted file to S3 bucket
s3 = boto3.resource('s3')
s3.meta.client.upload_file(xml_dir,bucketName,s3_file)