/libraries/cakemail/CakeCrypt.py

https://github.com/silent1mezzo/HackMTL · Python · 54 lines · 41 code · 13 blank · 0 comment · 6 complexity · 5d64d98c1089dccd422952941a6808d8 MD5 · raw file

  1. import CakeGlobals
  2. from Crypto.Cipher import Blowfish
  3. class CakeCrypt:
  4. def __init__(self):
  5. alg = CakeGlobals.CAKE_MCRYPT_ALG.upper();
  6. if CakeGlobals.CAKE_MCRYPT_ALG.upper() != 'BLOWFISH':
  7. raise Exception('Only Blowfish algoritm supported')
  8. if CakeGlobals.CAKE_MCRYPT_MODE.upper() != 'ECB':
  9. raise Exception('Only ECB crypt mode supported')
  10. Blowfish.key_size = len(CakeGlobals.CAKE_INTERFACE_KEY)
  11. self.block_size = Blowfish.block_size
  12. self._set_iv("u3rMyC31")
  13. def _get_cryptor(self):
  14. return Blowfish.new(CakeGlobals.CAKE_INTERFACE_KEY, eval('Blowfish.MODE_' + CakeGlobals.CAKE_MCRYPT_MODE.upper()), self._iv)
  15. def CakeEncryptHex(self, data):
  16. return self.CakeEncrypt(data).encode("hex")
  17. def CakeEncrypt(self, data):
  18. self._cryptor = self._get_cryptor()
  19. data = self._AdjustSize(data, self.block_size)
  20. return self._cryptor.encrypt(data)
  21. def CakeDecryptHex(self, data):
  22. return self.CakeDecrypt(data.decode("hex")).replace("\0", "")
  23. def CakeDecrypt(self, data):
  24. if CakeGlobals.CAKE_MCRYPT_MODE.upper() != "ECB":
  25. self.iv = data[0:self._iv_len - 1].decode("hex")
  26. data = data[self._iv_len:len(data)]
  27. self._cryptor = self._get_cryptor()
  28. return self._cryptor.decrypt(data)
  29. def _AdjustSize(self, data, size):
  30. origSize = len(data)
  31. if origSize % size != 0 or origSize == 0:
  32. newSize = ((origSize // size) + 1) * size
  33. for i in range(origSize, newSize):
  34. data += "\0"
  35. return data
  36. def _get_iv(self):
  37. return self._iv
  38. def _set_iv(self, value):
  39. self._iv = value
  40. self._iv_len = len(self._iv)
  41. iv = property(_get_iv, _set_iv)