· 7 years ago · Apr 26, 2018, 06:07 PM
1#!/usr/bin/python
2
3import sys
4import os
5from Crypto.Cipher import AES
6from Crypto import Random
7
8def main():
9
10 if len(sys.argv) != 3:
11 print ("Text or secret key is not specified!")
12 sys.exit(-1)
13
14 plain_text = sys.argv[1]
15 secret_key = sys.argv[2]
16
17 text_as_bytes = list(bytearray(plain_text))
18 len_as_bytes = [hex(len(text_as_bytes) >> i & 0xff) for i in (24,16,8,0)]
19 print("Len: ", len_as_bytes)
20
21 value = len_as_bytes + text_as_bytes
22 print("Len: ", value)
23
24 key = secret_key[:32]
25 cipher = AESCipher(key)
26 encrypted = cipher.encrypt(value)
27
28 print encrypted
29
30BS = 256
31pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
32
33class AESCipher:
34 def __init__(self, key):
35 """
36 Requires hex encoded param as a key
37 """
38 self.key = key.decode("hex")
39
40 def encrypt(self, raw):
41 """
42 Returns hex encoded encrypted value!
43 """
44 raw = pad(raw)
45 cipher = AES.new(self.key, AES.MODE_ECB)
46 return cipher.encrypt(raw).encode("hex")
47
48if __name__ == "__main__":
49 main()