/lib/crypto.py

https://github.com/olix0r/pub · Python · 48 lines · 34 code · 14 blank · 0 comment · 2 complexity · fa4e29b2ff6453e7e813ba646c603846 MD5 · raw file

  1. import re, sys
  2. from hashlib import md5
  3. from Crypto import Util
  4. from Crypto.Cipher import AES, Blowfish, DES3
  5. from Crypto.PublicKey import RSA, DSA
  6. from pyasn1.codec.der import decoder as DERDecoder
  7. from twisted.conch.ssh.keys import (
  8. BadKeyError, EncryptedKeyError,
  9. Key as _Key)
  10. from twisted.conch.ssh import common
  11. from twisted.conch.ssh.transport import _DummyCipher
  12. from twisted.python.util import InsensitiveDict
  13. from jersey import log
  14. class Key(_Key):
  15. _idLen = 8
  16. @property
  17. def id(self):
  18. return self.fingerprint().replace(":", "")[-self._idLen:].upper()
  19. def encrypt(self, plaintext):
  20. s = self.keyObject.size() / 8
  21. ciphertext = ""
  22. while plaintext:
  23. d, plaintext = plaintext[:s], plaintext[s:]
  24. ciphertext += self.keyObject.encrypt(d, None)[0]
  25. return ciphertext
  26. def decrypt(self, ciphertext):
  27. s = self.keyObject.size() / 8 + 1
  28. plaintext = ""
  29. while ciphertext:
  30. e, ciphertext = ciphertext[:s], ciphertext[s:]
  31. plaintext += self.keyObject.decrypt(e)
  32. return plaintext
  33. def public(self):
  34. return self.__class__(self.keyObject.publickey())