Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Chris M. Thomasson 9/6/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):
- print("\n\nPlaintext");
- print("_________________________________________");
- print(ct_bytes_to_hex(ptxt, 0));
- print("_________________________________________");
- def ct_ctxt_display(ctxt):
- n = len(ctxt[0]);
- print("\n\nCiphertext");
- print("_________________________________________");
- for i in range(n):
- print("%s" % (ct_bytes_to_hex(ctxt[0][i], 0)));
- print("%s" % (ct_bytes_to_hex(ctxt[1][i], 0)));
- print("_________________________________________");
- # HMAC Encrypt
- #____________________________________________________________
- def ct_hmac_encrypt(skey, ptxt):
- crand = [];
- ctxt = [];
- ctxt_block = "";
- n = len(ptxt);
- # the block length in bytes
- rbn = 16;
- # Get random bytes for a block
- rb = ct_rand_bytes(rbn);
- hi = 0;
- # Initalize HMAC for this session
- h = hmac.new((rb + skey).encode());
- h.update((skey + rb).encode());
- hbytes = h.digest();
- hn = len(hbytes);
- i = 0;
- # encrypt each plaintext byte
- while (i < n):
- if (hi < hn):
- p = ord(ptxt[i]);
- # Update HMAC with the plaintext byte if
- # we are not the first character.
- if (i > 0): h.update(ptxt[i].encode());
- # Simply XOR the plain byte with the HMAC byte.
- c = p ^ hbytes[hi];
- ctxt_block = ctxt_block + chr(c);
- hi = hi + 1;
- i = i + 1;
- if (i == n):
- crand.append(rb);
- ctxt.append(ctxt_block);
- break;
- else:
- # Append the random bytes and cipher block to
- # the ciphertext.
- crand.append(rb);
- ctxt.append(ctxt_block);
- ctxt_block = "";
- # Get random bytes for the next block.
- rb = ct_rand_bytes(rbn);
- hi = 0;
- # Update HMAC with the skey + the random bytes
- h.update((skey + rb).encode());
- hbytes = h.digest();
- hn = len(hbytes);
- return (crand, ctxt);
- # HMAC Decrypt
- #____________________________________________________________
- def ct_hmac_decrypt(skey, ctxt):
- ptxt = "";
- n = len(ctxt[0]);
- # Initalize HMAC for this session
- h = hmac.new((ctxt[0][0] + skey).encode());
- ci = 0;
- # Decrypt each byte
- for i in range(n):
- # Update HMAC with the skey + the random bytes
- h.update((skey + ctxt[0][i]).encode());
- hbytes = h.digest();
- hi = 0;
- for c in ctxt[1][i]:
- b = hbytes[hi];
- # Simply XOR the cipher byte with the HMAC byte.
- p = ord(c) ^ b;
- # Update HMAC with the plaintext byte if
- # we are not the first character.
- if (ci > 0): h.update(chr(p).encode());
- ptxt = ptxt + chr(p);
- hi = hi + 1;
- ci = ci + 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!";
- 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