-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaes.py
More file actions
36 lines (28 loc) · 938 Bytes
/
aes.py
File metadata and controls
36 lines (28 loc) · 938 Bytes
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
#!/usr/bin/env python3
from Crypto.Cipher import AES
# AESCipher used to do text manipulation/cryptography
# key length : 16 character
# message length : multiple of 16
class AESCipher:
def __init__(self, key):
self.key = str.encode(key)
# encrypt encript message in msg using key
# and return ciphertext result
def encrypt(self, msg):
cipher = AES.new(self.key, AES.MODE_ECB)
cipherText = cipher.encrypt(str.encode(msg))
return cipherText.hex()
# decrypt try decrypt cipher text in cipherText using key
# and return secret message as result
def decrypt(self, cipherText):
decipher = AES.new(self.key, AES.MODE_ECB)
msg = decipher.decrypt(bytes.fromhex(cipherText))
return msg
if __name__ == "__main__":
c = AESCipher("abcdefghijklmnop")
secret = "SepertiYangBiasa"
print(secret)
cipherText = c.encrypt(secret)
print(cipherText)
secret = c.decrypt(cipherText)
print(secret)