Guest User

Untitled

a guest
Jan 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. CHAR_MASK = ((1 << 8) - 1)
  2.  
  3. # Compute a 1 byte version of the encryption key
  4. def cape_compute_reduced_key(key, length):
  5. reduced_key = 0
  6. # Reduced key computation
  7. for i in xrange(0, length):
  8. reduced_key = (reduced_key ^ (key[i] << (i % 8))) & CHAR_MASK
  9. return reduced_key
  10.  
  11. def normalize_bytes(key):
  12. if isinstance(key, basestring):
  13. return map(lambda c: ord(c), key)
  14. if isinstance(key, int):
  15. return [key]
  16. else:
  17. return key
  18.  
  19. def stringify(bytes):
  20. for c in bytes:
  21. if c < ord(' ') or c > ord('~'):
  22. return repr(bytes)
  23. return ''.join(map(lambda c: chr(c), bytes))
  24.  
  25.  
  26. class Cape:
  27. def __init__(self, key, salt=0):
  28. self.salt = normalize_bytes(salt)[0]
  29. self.key = normalize_bytes(key)
  30. self.length = len(key)
  31. self.reduced_key = cape_compute_reduced_key(self.key, self.length)
  32.  
  33. # Decrypt data (max 65535 characters)
  34. def decrypt(self, _source):
  35. source = normalize_bytes(_source)
  36. length = len(source) - 1
  37. saltKey = (self.salt ^ self.reduced_key) & CHAR_MASK
  38. iv = (source[-1] ^ length ^ self.key[(length ^ saltKey) % self.length]) & CHAR_MASK
  39.  
  40. destination = [0] * length
  41. for i in xrange(0, length):
  42. destination[i] = (source[i] ^ iv ^ i ^ self.key[(saltKey ^ i) % self.length]) & CHAR_MASK
  43. return destination
  44.  
  45. # Stream chipher, private key, initialization vector based encryption
  46. # algorithm (max 65535 characters)
  47. def encrypt(self, _source, iv):
  48. source = normalize_bytes(_source)
  49. length = len(source)
  50. saltKey = (self.salt ^ self.reduced_key) & CHAR_MASK
  51. destination = [0] * length
  52. destination.append((iv ^ length ^ self.key[(length ^ saltKey) % self.length]) & CHAR_MASK)
  53. for i in xrange(0, length):
  54. destination[i] = (source[i] ^ iv ^ i ^ self.key[(saltKey ^ i) % self.length]) & CHAR_MASK
  55. return destination
  56.  
  57. # Symmetric chiper using private key, reduced key and optionally salt:
  58. def hash(self, _source):
  59. saltKey = (self.salt ^ self.reduced_key) & CHAR_MASK
  60. source = normalize_bytes(_source)
  61. destination = []
  62. for i in xrange(0, len(source)):
  63. iSaltKey = saltKey ^ i
  64. c = (source[i] ^ iSaltKey ^ self.key[iSaltKey % self.length]) & CHAR_MASK
  65. destination.append(c)
  66. return destination
  67.  
  68. cape = Cape('YOUR-ENCRYPTION-KEY', 'S')
  69. print("HASHING")
  70. print("=======")
  71. print(stringify(cape.hash('CRYPTMEPLEASE')))
  72. print(stringify(cape.hash(cape.hash('CRYPTMEPLEASE'))))
  73.  
  74. print("")
  75. print("ENCRYPT")
  76. print("=======")
  77. print(stringify(cape.encrypt('CRYPTMEPLEASE', 381)))
  78.  
  79. print("")
  80. print("DECRYPT")
  81. print("=======")
  82. print(stringify(cape.decrypt(cape.encrypt('CRYPTMEPLEASE', 2847))))
Add Comment
Please, Sign In to add comment