pmacs3/aesutil.py

30 lines
819 B
Python
Raw Normal View History

import Crypto.Hash.SHA256
import Crypto.Cipher.AES
2007-03-06 10:05:38 -05:00
class Crypter(object):
ALIGNMENT = 16
def __init__(self, password, salt='aes.py'):
2007-03-06 10:05:38 -05:00
self.password = password
self.salt = salt
self.hash = Crypto.Hash.SHA256.new(password + salt)
self.cipher = Crypto.Cipher.AES.new(self.hash.digest())
def pad(self, s):
xtra = len(s) % self.ALIGNMENT
if xtra:
return s + '\x00' * (self.ALIGNMENT - xtra)
2007-03-06 10:05:38 -05:00
else:
return s
def unpad(self, s):
l = len(s)
while l > 0 and s[l - 1] == '\x00':
l -= 1
return s[:l]
def encrypt(self, data):
return self.cipher.encrypt(self.pad(data))
def decrypt(self, data):
return self.unpad(self.cipher.decrypt(self.pad(data)))