Chris_M_Thomasson

HMAC Encryption Experiment

Sep 19th, 2016
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1. # Chris M. Thomasson 9/19/2016
  2.  
  3. import math;
  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. def ct_hex_to_bytes(origin, offset):
  33.     bytes = "";
  34.     n = len(origin);
  35.     t = "0123456789ABCDEF";
  36.  
  37.     i = offset + 1;
  38.     while (i < n):
  39.  
  40.         nibh = 0;
  41.         nibl = 0;
  42.  
  43.         try:
  44.             nibh = t.index(origin[i - 1]);
  45.             nibl = t.index(origin[i]);
  46.         except ValueError:
  47.             i = i + 1;
  48.             continue;
  49.  
  50.         if (nibh > -1 and nibl > -1):
  51.             c = nibh * 16 + nibl;
  52.             bytes = bytes + chr(c);
  53.  
  54.         i = i + 2;
  55.  
  56.     return bytes;
  57.  
  58.  
  59. # Generate n random bytes
  60. def ct_rand_bytes(n):
  61.     rb = "";
  62.     for i in range(n):
  63.         rb = rb + chr(random.randint(0, 255));
  64.     return rb;
  65.  
  66. def ct_ptxt_display(ptxt):
  67.     n = len(ptxt);
  68.     print("\n\nPlaintext, size:%s" % (n));
  69.     print("_________________________________________");
  70.     print(ct_bytes_to_hex(ptxt, 0));
  71.     print("_________________________________________");
  72.  
  73. def ct_ctxt_display(ctxt):
  74.     n = len(ctxt[0]) + len(ctxt[1]);
  75.     print("\n\nCiphertext, size:%s" % (n));
  76.     print("_________________________________________");
  77.     print("%s" % (ct_bytes_to_hex(ctxt[0], 0)));
  78.     print("%s" % (ct_bytes_to_hex(ctxt[1], 0)));
  79.     print("_________________________________________");
  80.  
  81.  
  82.  
  83. # HMAC Encrypt
  84. #____________________________________________________________
  85. def ct_hmac_encrypt(skey, ptxt):
  86.     ctxt = "";
  87.     n = len(ptxt);
  88.  
  89.     # Initialize HMAC for this session
  90.     crand = ct_rand_bytes(32); # 32 bytes of random iv
  91.     h = hmac.new((crand + skey).encode());
  92.     h.update((skey + crand).encode());
  93.  
  94.     i = 0;
  95.     while (i < n):
  96.         # Grab HMAC buffer
  97.         hbytes = h.digest();
  98.         hn = len(hbytes);
  99.         hi = 0;
  100.         while (i < n and hi < hn):
  101.             p = ord(ptxt[i]);
  102.             c = p ^ hbytes[hi];
  103.             h.update(ptxt[i].encode());
  104.             h.update(chr(c).encode());
  105.             ctxt = ctxt + chr(c);
  106.             i = i + 1;
  107.             hi = hi + 1;
  108.  
  109.     return (crand, ctxt);
  110.  
  111.  
  112. # HMAC Decrypt
  113. #____________________________________________________________
  114. def ct_hmac_decrypt(skey, ctxt):
  115.     ptxt = "";
  116.     n = len(ctxt[1]);
  117.  
  118.     # Initialize HMAC for this session
  119.     h = hmac.new((ctxt[0] + skey).encode());
  120.     h.update((skey + ctxt[0]).encode());
  121.  
  122.     i = 0;
  123.     while (i < n):
  124.         hbytes = h.digest();
  125.         hn = len(hbytes);
  126.         hi = 0;
  127.         while (i < n and hi < hn):
  128.             c = ord(ctxt[1][i]);
  129.             p = c ^ hbytes[hi];
  130.             h.update(chr(p).encode());
  131.             h.update(chr(c).encode());
  132.             ptxt = ptxt + chr(p);
  133.             i = i + 1;
  134.             hi = hi + 1;
  135.  
  136.     return ptxt;
  137.  
  138.  
  139.  
  140. # Main Program
  141. #____________________________________________________________
  142. print("Experimental HMAC Encryption");
  143. print("Chris M. Thomasson 9/6/2016");
  144. print("==============================================================\n\n");
  145.  
  146.  
  147. ptxt = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  148.  
  149. # Secret Key
  150. skey = "This is the Secret HMAC Key! BTW, this should be longer...";
  151. print("skey:%s" % (skey));
  152.  
  153.  
  154. # Encrypt
  155. print("\n\n\nEncrypted:");
  156. print("\nptxt:%s" % (ptxt));
  157. ct_ptxt_display(ptxt);
  158. ctxt = ct_hmac_encrypt(skey, ptxt);
  159. ct_ctxt_display(ctxt);
  160.  
  161. # Decrypt
  162. dtxt = ct_hmac_decrypt(skey, ctxt);
  163. print("\n\n\nDecrypted:");
  164. ct_ptxt_display(dtxt);
  165. print("\n\ndtxt:%s" % (dtxt));
  166.  
  167. if (dtxt != ptxt):
  168.     print("Data Corrupted!");
Advertisement
Add Comment
Please, Sign In to add comment