Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Chris M. Thomasson Copyright 2018 (c)
- # Experimental HMAC Stream Cipher
- #____________________________________________________________
- # Our external libs
- #____________________________________________________________
- import random;
- import hashlib;
- 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;
- # Generate n random bytes
- # These need should ideally be from a truly random, non-repeatable
- # source. TRNG!
- def ct_rand_bytes(n):
- rb = "";
- for i in range(n):
- rb = rb + chr(random.randint(0, 255));
- #rb = rb + chr(i);
- return rb;
- # Secret Key
- SK_hmac_key = "Password";
- SK_hmac_algo = hashlib.sha384;
- SK_rand_n = 13;
- # Alice ENCRYPTS!!!
- P = "XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAX";
- print("P:%s\n" % ct_bytes_to_hex(P, 0));
- # Generate IV
- IV = ct_rand_bytes(SK_rand_n);
- H = hmac.new(SK_hmac_key.encode(), None, SK_hmac_algo);
- H.update(IV.encode());
- D = H.digest();
- cidx = 0;
- C = "";
- for i in range(0, len(P)):
- CI = ord(P[i]) ^ D[cidx];
- C = C + chr(CI);
- H.update(chr(CI).encode());
- H.update(chr(cidx).encode());
- cidx = cidx + 1;
- if (cidx == len(D)):
- D = H.digest();
- cidx = 0;
- print("IV:%s\n" % ct_bytes_to_hex(IV, 0));
- print("ciphertext:%s\n" % ct_bytes_to_hex(C, 0));
- # Bob DECRYPTS!!!
- H = hmac.new(SK_hmac_key.encode(), None, SK_hmac_algo);
- H.update(IV.encode());
- D = H.digest();
- cidx = 0;
- BOB = "";
- for i in range(0, len(C)):
- CI = ord(C[i]) ^ D[cidx];
- BOB = BOB + chr(CI);
- H.update(C[i].encode());
- H.update(chr(cidx).encode());
- cidx = cidx + 1;
- if (cidx == len(D)):
- D = H.digest();
- cidx = 0;
- print("BOB:%s\n" % ct_bytes_to_hex(BOB, 0));
Add Comment
Please, Sign In to add comment