-
Notifications
You must be signed in to change notification settings - Fork 1
/
decrypto.py
75 lines (54 loc) · 1.52 KB
/
decrypto.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
71
72
73
74
75
__author__ = 'CirnoTxm'
from charm.toolbox.pairinggroup import PairingGroup,GT,pair
from charm.core.math.pairing import hashPair as sha1
from charm.schemes.abenc.abenc_waters09 import CPabe09
from charm.toolbox.symcrypto import SymmetricCryptoAbstraction
from charm.core.engine.util import objectToBytes,bytesToObject
import json
import os
debug = True
keyUrl = '/Users/cirnotxm/down/key/'
configUrl = '/Users/cirnotxm/down/config/down_config'
with open(configUrl,"rb") as f:
ss = f.read()
s = eval(ss)
fileUrl= s[0]
fileName = s[1]
asl = s[2]
asl = eval(asl)
if debug :
print fileUrl
print fileName
print type(asl)
groupObj = PairingGroup('SS512')
cpabe = CPabe09(groupObj)
keyFile = keyUrl+fileName
with open(keyFile+'.mk',"rb") as fmk:
mk = fmk.read()
with open(keyFile+".pk","rb") as fpk:
pk = fpk.read()
with open(keyFile+".ci","rb") as fci:
cipher = fci.read()
# print mk+"\n"
# print pk+"\n"
# print cipher+"\n"
mk = bytesToObject(mk,groupObj)
pk = bytesToObject(pk,groupObj)
cipher = bytesToObject(cipher,groupObj)
sk = cpabe.keygen(pk,mk,asl)
print sk
try:
plaintext = cpabe.decrypt(pk,sk,cipher)
except:
print "Error"
if plaintext!=False :
a = SymmetricCryptoAbstraction(sha1(plaintext))
with open(fileUrl,"rb") as f:
cloudfile = f.read()
file = a.decrypt(cloudfile)
with open(fileUrl,"wb") as re:
re.write(file)
else:
print "Dcrypto Fail!"
del groupObj
#os.remove(configUrl)