-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.py
69 lines (44 loc) · 1.42 KB
/
encryption.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
#Imports
from Crypto.Cipher import AES
import base64
import os
from decryption import decryption
#Functions
#Function to decrypr message
def decrypt():
mess_file = open('message.txt','r')
mess_file = mess_file.read()
print "Read string : " + mess_file
decryption(mess_file)
#Function to write to a file.
def fileWrite(filename,text):
'''
@param Input: The filename in which the given text is to be written
@none Output: Text is written in the file.
'''
file = open(filename,'w')
file.write(text)
file.close
#Function to encrypt a text.
def encryption(privateInfo):
'''
@param Input: The message which is to be encrypted.
@print Output: The encrypted message.
'''
BLOCK_SIZE = 16 # 128 bit AES encryption(16 bytes)
PADDING = '{'
pad = lambda s: s+(BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
# secret = os.urandom(BLOCK_SIZE)
# print 'encryption key:', secret
# fileWrite('key.txt',secret)
secret = open('key.txt','r').read()
cipher = AES.new(secret)
encoded = EncodeAES(cipher,privateInfo)
fileWrite('message.txt', encoded)
return encoded
# print 'Encrypted string:', encoded
# decrypt()
# f = raw_input("Enter message to be encoded: ")
# message = f
# encryption(message)