Chris_M_Thomasson

HMAC Stream Cipher with Feedback v:0.0.0...

May 21st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. # Chris M. Thomasson Copyright 2018 (c)
  2. # Experimental HMAC Stream Cipher
  3. #____________________________________________________________
  4.  
  5.  
  6. # Our external libs
  7. #____________________________________________________________
  8. import random;
  9. import hashlib;
  10. import hmac;
  11.  
  12.  
  13. # Some Utilities
  14. #____________________________________________________________
  15. def ct_bytes_to_hex(origin, offset):
  16.     hex = "";
  17.     n = len(origin);
  18.     t = "0123456789ABCDEF";
  19.     for i in range(offset, n):
  20.         c = ord(origin[i]);
  21.         nibl = c & 0x0F;
  22.         nibh = (c & 0xF0) >> 4;
  23.         hex = hex + t[nibh];
  24.         hex = hex + t[nibl];
  25.         hex = hex + " ";
  26.         if (not ((i + 1) % 16) and i != n - 1):
  27.             hex = hex + "\r\n";
  28.     return hex;
  29.  
  30.  
  31. # Generate n random bytes
  32. # These need should ideally be from a truly random, non-repeatable
  33. # source. TRNG!
  34. def ct_rand_bytes(n):
  35.     rb = "";
  36.     for i in range(n):
  37.         rb = rb + chr(random.randint(0, 255));
  38.         #rb = rb + chr(i);
  39.     return rb;
  40.  
  41.  
  42. # Secret Key
  43. SK_hmac_key = "Password";
  44. SK_hmac_algo = hashlib.sha384;
  45. SK_rand_n = 13;
  46.  
  47.  
  48. # Alice ENCRYPTS!!!
  49. P = "XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAX";
  50.  
  51. print("P:%s\n" % ct_bytes_to_hex(P, 0));
  52.  
  53. # Generate IV
  54. IV = ct_rand_bytes(SK_rand_n);
  55.  
  56. H = hmac.new(SK_hmac_key.encode(), None, SK_hmac_algo);
  57. H.update(IV.encode());
  58. D = H.digest();
  59.  
  60. cidx = 0;
  61.  
  62. C = "";
  63.  
  64. for i in range(0, len(P)):
  65.     CI = ord(P[i]) ^ D[cidx];
  66.     C = C + chr(CI);
  67.     H.update(chr(CI).encode());
  68.     H.update(chr(cidx).encode());
  69.     cidx = cidx + 1;
  70.     if (cidx == len(D)):
  71.         D = H.digest();
  72.         cidx = 0;
  73.  
  74. print("IV:%s\n" % ct_bytes_to_hex(IV, 0));
  75. print("ciphertext:%s\n" % ct_bytes_to_hex(C, 0));
  76.  
  77.  
  78.  
  79.  
  80. # Bob DECRYPTS!!!
  81. H = hmac.new(SK_hmac_key.encode(), None, SK_hmac_algo);
  82. H.update(IV.encode());
  83. D = H.digest();
  84.  
  85. cidx = 0;
  86.  
  87. BOB = "";
  88.  
  89. for i in range(0, len(C)):
  90.     CI = ord(C[i]) ^ D[cidx];
  91.     BOB = BOB + chr(CI);
  92.     H.update(C[i].encode());
  93.     H.update(chr(cidx).encode());
  94.     cidx = cidx + 1;
  95.     if (cidx == len(D)):
  96.         D = H.digest();
  97.         cidx = 0;
  98.  
  99. print("BOB:%s\n" % ct_bytes_to_hex(BOB, 0));
Add Comment
Please, Sign In to add comment