Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Chris M. Thomasson 9/19/2016
- import math;
- import random;
- import hmac;
- # Some Utilities
- #____________________________________________________________
- def ct_bytes_to_hex(origin, offset):
- hex = "";
- n = len(origin);
- t = "0123456789ABCDEF";
- for i in range(offset, n):
- c = ord(origin[i]);
- nibl = c & 0x0F;
- nibh = (c & 0xF0) >> 4;
- hex = hex + t[nibh];
- hex = hex + t[nibl];
- hex = hex + " ";
- if (not ((i + 1) % 16) and i != n - 1):
- hex = hex + "\r\n";
- return hex;
- def ct_hex_to_bytes(origin, offset):
- bytes = "";
- n = len(origin);
- t = "0123456789ABCDEF";
- i = offset + 1;
- while (i < n):
- nibh = 0;
- nibl = 0;
- try:
- nibh = t.index(origin[i - 1]);
- nibl = t.index(origin[i]);
- except ValueError:
- i = i + 1;
- continue;
- if (nibh > -1 and nibl > -1):
- c = nibh * 16 + nibl;
- bytes = bytes + chr(c);
- i = i + 2;
- return bytes;
- # Generate n random bytes
- def ct_rand_bytes(n):
- rb = "";
- for i in range(n):
- rb = rb + chr(random.randint(0, 255));
- return rb;
- def ct_ptxt_display(ptxt):
- n = len(ptxt);
- print("\n\nPlaintext, size:%s" % (n));
- print("_________________________________________");
- print(ct_bytes_to_hex(ptxt, 0));
- print("_________________________________________");
- def ct_ctxt_display(ctxt):
- n = len(ctxt[0]) + len(ctxt[1]);
- print("\n\nCiphertext, size:%s" % (n));
- print("_________________________________________");
- print("%s" % (ct_bytes_to_hex(ctxt[0], 0)));
- print("%s" % (ct_bytes_to_hex(ctxt[1], 0)));
- print("_________________________________________");
- # HMAC Encrypt
- #____________________________________________________________
- def ct_hmac_encrypt(skey, ptxt):
- ctxt = "";
- n = len(ptxt);
- # Initialize HMAC for this session
- crand = ct_rand_bytes(32); # 32 bytes of random iv
- h = hmac.new((crand + skey).encode());
- h.update((skey + crand).encode());
- i = 0;
- while (i < n):
- # Grab HMAC buffer
- hbytes = h.digest();
- hn = len(hbytes);
- hi = 0;
- while (i < n and hi < hn):
- p = ord(ptxt[i]);
- c = p ^ hbytes[hi];
- h.update(ptxt[i].encode());
- h.update(chr(c).encode());
- ctxt = ctxt + chr(c);
- i = i + 1;
- hi = hi + 1;
- return (crand, ctxt);
- # HMAC Decrypt
- #____________________________________________________________
- def ct_hmac_decrypt(skey, ctxt):
- ptxt = "";
- n = len(ctxt[1]);
- # Initialize HMAC for this session
- h = hmac.new((ctxt[0] + skey).encode());
- h.update((skey + ctxt[0]).encode());
- i = 0;
- while (i < n):
- hbytes = h.digest();
- hn = len(hbytes);
- hi = 0;
- while (i < n and hi < hn):
- c = ord(ctxt[1][i]);
- p = c ^ hbytes[hi];
- h.update(chr(p).encode());
- h.update(chr(c).encode());
- ptxt = ptxt + chr(p);
- i = i + 1;
- hi = hi + 1;
- return ptxt;
- # Main Program
- #____________________________________________________________
- print("Experimental HMAC Encryption");
- print("Chris M. Thomasson 9/6/2016");
- print("==============================================================\n\n");
- ptxt = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
- # Secret Key
- skey = "This is the Secret HMAC Key! BTW, this should be longer...";
- print("skey:%s" % (skey));
- # Encrypt
- print("\n\n\nEncrypted:");
- print("\nptxt:%s" % (ptxt));
- ct_ptxt_display(ptxt);
- ctxt = ct_hmac_encrypt(skey, ptxt);
- ct_ctxt_display(ctxt);
- # Decrypt
- dtxt = ct_hmac_decrypt(skey, ctxt);
- print("\n\n\nDecrypted:");
- ct_ptxt_display(dtxt);
- print("\n\ndtxt:%s" % (dtxt));
- if (dtxt != ptxt):
- print("Data Corrupted!");
Advertisement
Add Comment
Please, Sign In to add comment