Advertisement
Chris_M_Thomasson

Experimental HMAC Based Encryption ver (0.0.2)

Jan 25th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. # Chris M. Thomasson 9/24/2018
  2. # Experimental HMAC Cipher
  3.  
  4. import random;
  5. import hmac;
  6.  
  7.  
  8. # Some Utilities
  9. #____________________________________________________________
  10. def ct_bytes_to_hex(origin, offset):
  11. hex = "";
  12. n = len(origin);
  13. t = "0123456789ABCDEF";
  14.  
  15. for i in range(offset, n):
  16. c = ord(origin[i]);
  17.  
  18. nibl = c & 0x0F;
  19. nibh = (c & 0xF0) >> 4;
  20.  
  21. hex = hex + t[nibh];
  22. hex = hex + t[nibl];
  23.  
  24. hex = hex + " ";
  25.  
  26. if (not ((i + 1) % 16) and i != n - 1):
  27. hex = hex + "\r\n";
  28.  
  29. return hex;
  30.  
  31.  
  32. # Generate n random bytes
  33. # These can be from a truly random, non-repeatable source.
  34. def ct_rand_bytes(n):
  35. rb = "";
  36. for i in range(n):
  37. rb = rb + chr(random.randint(0, 255));
  38. return rb;
  39.  
  40.  
  41. # The Secret Key
  42. #____________________________________________________________
  43. class ct_secret_key:
  44. def __init__(self, hmac_pword, pbyte_pword, pbyte, crand_bytes, rounds):
  45. self.hmac_pword = hmac_pword;
  46. self.pbyte_pword = pbyte_pword;
  47. self.pbyte = pbyte;
  48. self.crand_bytes = crand_bytes;
  49. self.rounds = rounds;
  50.  
  51. def __repr__(self):
  52. return "hmac_pword:%s\npbyte_pword:%s\npbyte:%s\ncrand_bytes:%s\nrounds:%s" % (self.hmac_pword, self.pbyte_pword, self.pbyte, self.crand_bytes, self.rounds);
  53.  
  54. def __str__(self): return self.__repr__();
  55.  
  56.  
  57. # The Plaintest
  58. #____________________________________________________________
  59. class ct_plaintext:
  60. def __init__(self, ptxt):
  61. self.bytes = ptxt;
  62.  
  63. def __repr__(self):
  64. return "bytes:%s" % (ct_bytes_to_hex(self.bytes, 0));
  65.  
  66. def __str__(self): return self.__repr__();
  67.  
  68.  
  69. # The Ciphertext
  70. #____________________________________________________________
  71. class ct_ciphertext:
  72. def __init__(self, crand, ctxt):
  73. self.crand = crand;
  74. self.bytes = ctxt;
  75.  
  76. def __repr__(self):
  77. return "iv:%s\nbytes:%s" % (ct_bytes_to_hex(self.crand, 0), ct_bytes_to_hex(self.bytes, 0));
  78.  
  79. def __str__(self): return self.__repr__();
  80.  
  81.  
  82. # Encrypt Round
  83. #____________________________________________________________
  84. def ct_encrypt_round(skey, crand, ptxt):
  85. ctxt = "";
  86. n = len(ptxt.bytes);
  87. pbyte_len = len(skey.pbyte_pword);
  88.  
  89. h = hmac.new((crand + skey.hmac_pword).encode());
  90. h.update((skey.hmac_pword + crand).encode());
  91.  
  92. i = 0;
  93. pbyte = ord(skey.pbyte_pword[skey.pbyte % pbyte_len]);
  94. while (i < n):
  95. # Grab HMAC buffer
  96. hbytes = h.digest();
  97. hn = len(hbytes);
  98. hi = 0;
  99.  
  100. while (i < n and hi < hn):
  101. p = ord(ptxt.bytes[i]);
  102. c = p ^ hbytes[hi];
  103. c = c ^ pbyte;
  104. pbyte = c ^ ord(skey.pbyte_pword[i % pbyte_len]);
  105. h.update(ptxt.bytes[i].encode());
  106. h.update(chr(c).encode());
  107. ctxt = ctxt + chr(c);
  108. i = i + 1;
  109. hi = hi + 1;
  110.  
  111. return ct_ciphertext(crand, ctxt);
  112.  
  113.  
  114. # Decrypt Round
  115. #____________________________________________________________
  116. def ct_decrypt_round(skey, ctxt):
  117. ptxt = "";
  118. n = len(ctxt.bytes);
  119. pbyte_len = len(skey.pbyte_pword);
  120.  
  121. # Initialize HMAC for this session
  122. h = hmac.new((ctxt.crand + skey.hmac_pword).encode());
  123. h.update((skey.hmac_pword + ctxt.crand).encode());
  124.  
  125. i = 0;
  126. pbyte = ord(skey.pbyte_pword[skey.pbyte % pbyte_len]);
  127.  
  128. while (i < n):
  129. hbytes = h.digest();
  130. hn = len(hbytes);
  131. hi = 0;
  132.  
  133. while (i < n and hi < hn):
  134. c = ord(ctxt.bytes[i]);
  135. p = c ^ hbytes[hi];
  136. p = p ^ pbyte;
  137. pbyte = c ^ ord(skey.pbyte_pword[i % pbyte_len]);
  138. h.update(chr(p).encode());
  139. h.update(chr(c).encode());
  140. ptxt = ptxt + chr(p);
  141. i = i + 1;
  142. hi = hi + 1;
  143.  
  144. return ct_plaintext(ptxt);
  145.  
  146.  
  147. # Encrypt
  148. #____________________________________________________________
  149. def ct_encrypt(skey, ptxt):
  150. crand = ct_rand_bytes(skey.crand_bytes); # n bytes of random iv
  151.  
  152. for i in range(skey.rounds):
  153. ctxt = ct_encrypt_round(skey, crand, ptxt);
  154. ptxt.bytes = ctxt.bytes[::-1];
  155. ctxt = ct_encrypt_round(skey, crand, ptxt);
  156.  
  157. return ctxt;
  158.  
  159. def ct_decrypt(skey, ctxt):
  160. for i in range(skey.rounds):
  161. ptxt = ct_decrypt_round(skey, ctxt);
  162. ctxt.bytes = ptxt.bytes[::-1];
  163. ptxt = ct_decrypt_round(skey, ctxt);
  164.  
  165. return ptxt;
  166.  
  167.  
  168.  
  169. # Main Program
  170. #____________________________________________________________
  171. print("Experimental HMAC Encryption");
  172. print("Chris M. Thomasson 9/24/2018");
  173. print("==============================================================\n\n");
  174.  
  175.  
  176. # Alice and Bob_II's secret key
  177. skey = ct_secret_key(
  178. "This is the Secret HMAC Key! BTW, this should be longer...",
  179. "Another Password Used With the Previous Byte...",
  180. 103,
  181. 32,
  182. 5
  183. );
  184.  
  185. print("Secret Key:\n"
  186. "______________________________\n"
  187. "%s\n\n" % (skey)
  188. );
  189.  
  190.  
  191. # Our plaintext
  192. ptxt_bytes = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  193.  
  194. ptxt = ct_plaintext(ptxt_bytes);
  195.  
  196. print("Original Plaintext:\n"
  197. "______________________________\n"
  198. "%s\n\n" % (ptxt)
  199. );
  200.  
  201.  
  202.  
  203. # Encrypt the plaintext
  204. crand = ct_rand_bytes(32);
  205. ctxt = ct_encrypt(skey, ptxt);
  206.  
  207. print("Ciphertext:\n"
  208. "______________________________\n"
  209. "%s\n\n" % (ctxt)
  210. );
  211.  
  212.  
  213.  
  214. # Decrypt the ciphertext
  215. ptxt_decrypt = ct_decrypt(skey, ctxt);
  216.  
  217. print("Decrypted Plaintext:\n"
  218. "______________________________\n"
  219. "%s\n\n" % (ptxt_decrypt)
  220. );
  221.  
  222.  
  223. # Check the data...
  224. if (ptxt_bytes != ptxt_decrypt.bytes):
  225. print("DATA CORRUPTED!!!!!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement