Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Stitch-Bot/Encryption.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
35 lines (28 sloc)
970 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Crypto.Cipher import AES | |
from Crypto.Hash import SHA256 | |
class encryptionAES128: | |
def __init__(self, key): | |
self.BLOCK_SIZE = 16 | |
self.KEY = self.hash(key) | |
self.CIPHER = AES.new(self.KEY, AES.MODE_ECB) | |
def encrypt(self, data): | |
encrypted = self.CIPHER.encrypt(bytes(self.pad(data))) | |
return encrypted | |
def decrypt(self, data): | |
decrypted = self.CIPHER.decrypt(data) | |
msg = self.depad(decrypted.decode()) | |
return msg | |
def pad(self, data): | |
if (len(data) % self.BLOCK_SIZE) != 0: | |
data = data + (self.BLOCK_SIZE - (len(data) % self.BLOCK_SIZE)) * '~' | |
return data.encode() | |
else: | |
return data | |
@staticmethod | |
def depad(data): | |
padsize = data.count('~') | |
raw_data = data[:len(data) - padsize] | |
return raw_data | |
def hash(self, data): | |
hashing = SHA256.new(bytes(data.encode())) | |
return hashing.digest() |