-
Notifications
You must be signed in to change notification settings - Fork 19
/
AgeEncHandler.py
70 lines (52 loc) · 2.06 KB
/
AgeEncHandler.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from age.cli import decrypt, encrypt
import io
import os
import sys
import config, utils
def ageDecrypt(content):
fname = os.path.expanduser("~/.config/age/" + utils.getTimestamp(True, True) + ".txt")
content = content.encode('utf-8')
f = open(fname, 'wb')
decrypt(infile=io.BytesIO(content),outfile=f,ascii_armored=True)
f = open(fname, 'r')
out = f.read()
f.close()
os.remove(fname)
return(out)
def ageEncrypt(content):
fname = os.path.expanduser("~/.config/age/" + utils.getTimestamp(True, True) + ".txt")
f = open(fname, 'wb')
encrypt(recipients=[config.getAgePublicKey()], infile=io.BytesIO(content.encode('utf-8')), outfile=f,ascii_armored=True)
f = open(fname, 'r')
out = f.read()
f.close()
os.remove(fname)
return(out)
def isAgeEncrypted(content):
if content.startswith("-----BEGIN AGE ENCRYPTED FILE----- "):
return 1
elif content.startswith("-----BEGIN AGE ENCRYPTED FILE-----\n"):
return 2
else:
return 0
def convertToAgeString(content):
s = "-----BEGIN AGE ENCRYPTED FILE-----\n"
s += '\n'.join(content.split("FILE----- ")[1].split(" -----END")[0].split(' '))
s += "\n-----END AGE ENCRYPTED FILE-----"
return s
# encContent = """-----BEGIN AGE ENCRYPTED FILE-----
# YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSA1eWx4L2lHcXp6VEowWi9S
# VjdSWnJlbzNkK0JwTVEzQU9UcndTKzdnVGw4Cll1MjJEczhJNGpTWWJBZWRKeEdm
# VXlOL2J6WElRbmtkUE9ING9FUXRsQTgKLT4gZ1FdODdTMi1ncmVhc2UgTS0ofmM+
# ICZCPVMgRzNtJnlOIC5pSD11ClRKM1ZwRHRaeHNDR0Qwa0pKU0hmdElkWUlpSFdw
# dzZOSGQ4U2xQM29JaDVpR3ZJS3IzYUdjUW5HdHh6TVV3Mm8KRmUxS2tCbHEzMWw4
# V1NTNWZQSUczSTRXT293Ci0tLSB0dGhjR1Z4SnFCbXdLSEdZTXBsUTc3WDFlMVpX
# a3QvQ0h1WkphU3ZsSnhRCo57XUKTWVOgacwUNCN81+T4nUKzLMwddOXYpvpa1QwI
# SgimgEyvpVSBt06F6iQq34yc+4HgH40nrJr+v/V7zZE=
# -----END AGE ENCRYPTED FILE-----"""
# if isAgeEncrypted(encContent) == 1:
# print(ageDecrypt(convertToAgeString(encContent)))
# elif isAgeEncrypted(encContent) == 2:
# print(ageDecrypt(encContent))
# CLEARTEXT = "Hello World!"
# print(ageDecrypt(ageEncrypt(CLEARTEXT)))