Chris_M_Thomasson

Experimental HMAC Encryption

Sep 6th, 2016
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.96 KB | None | 0 0
  1. # Chris M. Thomasson 9/6/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. # Generate n random bytes
  59. def ct_rand_bytes(n):
  60.     rb = "";
  61.     for i in range(n):
  62.         rb = rb + chr(random.randint(0, 255));
  63.     return rb;
  64.  
  65. def ct_ptxt_display(ptxt):
  66.     print("\n\nPlaintext");
  67.     print("_________________________________________");
  68.     print(ct_bytes_to_hex(ptxt, 0));
  69.     print("_________________________________________");
  70.  
  71. def ct_ctxt_display(ctxt):
  72.     n = len(ctxt[0]);
  73.     print("\n\nCiphertext");
  74.     print("_________________________________________");
  75.     for i in range(n):
  76.         print("%s" % (ct_bytes_to_hex(ctxt[0][i], 0)));
  77.         print("%s" % (ct_bytes_to_hex(ctxt[1][i], 0)));
  78.     print("_________________________________________");
  79.  
  80.  
  81.  
  82. # HMAC Encrypt
  83. #____________________________________________________________
  84. def ct_hmac_encrypt(skey, ptxt):
  85.     crand = [];
  86.     ctxt = [];
  87.  
  88.     ctxt_block = "";
  89.  
  90.     n = len(ptxt);
  91.  
  92.     # the block length in bytes
  93.     rbn = 16;
  94.  
  95.     # Get random bytes for a block
  96.     rb = ct_rand_bytes(rbn);
  97.     hi = 0;
  98.  
  99.     # Initalize HMAC for this session
  100.     h = hmac.new((rb + skey).encode());
  101.     h.update((skey + rb).encode());
  102.     hbytes = h.digest();
  103.     hn = len(hbytes);
  104.  
  105.     i = 0;
  106.  
  107.     # encrypt each plaintext byte
  108.     while (i < n):
  109.         if (hi < hn):
  110.             p = ord(ptxt[i]);
  111.  
  112.             # Update HMAC with the plaintext byte if
  113.             # we are not the first character.
  114.             if (i > 0): h.update(ptxt[i].encode());
  115.  
  116.             # Simply XOR the plain byte with the HMAC byte.
  117.             c = p ^ hbytes[hi];
  118.             ctxt_block = ctxt_block + chr(c);
  119.             hi = hi + 1;
  120.             i = i + 1;
  121.  
  122.             if (i == n):
  123.                 crand.append(rb);
  124.                 ctxt.append(ctxt_block);
  125.                 break;
  126.         else:
  127.             # Append the random bytes and cipher block to
  128.             # the ciphertext.
  129.             crand.append(rb);
  130.             ctxt.append(ctxt_block);
  131.             ctxt_block = "";
  132.  
  133.             # Get random bytes for the next block.
  134.             rb = ct_rand_bytes(rbn);
  135.             hi = 0;
  136.  
  137.             # Update HMAC with the skey + the random bytes
  138.             h.update((skey + rb).encode());
  139.             hbytes = h.digest();
  140.             hn = len(hbytes);
  141.  
  142.     return (crand, ctxt);
  143.  
  144.  
  145. # HMAC Decrypt
  146. #____________________________________________________________
  147. def ct_hmac_decrypt(skey, ctxt):
  148.     ptxt = "";
  149.     n = len(ctxt[0]);
  150.  
  151.     # Initalize HMAC for this session
  152.     h = hmac.new((ctxt[0][0] + skey).encode());
  153.     ci = 0;
  154.  
  155.     # Decrypt each byte
  156.     for i in range(n):
  157.  
  158.         # Update HMAC with the skey + the random bytes
  159.         h.update((skey + ctxt[0][i]).encode());
  160.         hbytes = h.digest();
  161.         hi = 0;
  162.         for c in ctxt[1][i]:
  163.             b = hbytes[hi];
  164.  
  165.             # Simply XOR the cipher byte with the HMAC byte.
  166.             p = ord(c) ^ b;
  167.  
  168.             # Update HMAC with the plaintext byte if
  169.             # we are not the first character.
  170.             if (ci > 0): h.update(chr(p).encode());
  171.  
  172.             ptxt = ptxt + chr(p);
  173.             hi = hi + 1;
  174.             ci = ci + 1;
  175.  
  176.     return ptxt;
  177.  
  178.  
  179.  
  180. # Main Program
  181. #____________________________________________________________
  182. print("Experimental HMAC Encryption");
  183. print("Chris M. Thomasson 9/6/2016");
  184. print("==============================================================\n\n");
  185.  
  186.  
  187. ptxt = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  188.  
  189. # Secret Key
  190. skey = "This is the Secret HMAC Key!";
  191. print("skey:%s" % (skey));
  192.  
  193.  
  194. # Encrypt
  195. print("\n\n\nEncrypted:");
  196. print("\nptxt:%s" % (ptxt));
  197. ct_ptxt_display(ptxt);
  198. ctxt = ct_hmac_encrypt(skey, ptxt);
  199. ct_ctxt_display(ctxt);
  200.  
  201. # Decrypt
  202. dtxt = ct_hmac_decrypt(skey, ctxt);
  203. print("\n\n\nDecrypted:");
  204. ct_ptxt_display(dtxt);
  205. print("\n\ndtxt:%s" % (dtxt));
  206.  
  207. if (dtxt != ptxt):
  208.     print("Data Corrupted!");
Advertisement
Add Comment
Please, Sign In to add comment