from hashlib import md5 as dgst
import string
from zlib import compress, decompress
from base64 import urlsafe_b64encode as b64, urlsafe_b64decode as b64d
from os import system
MAX_LEN = 30
PRIMES = [0x6f9bf, 0x1f9e5, 0x61219, 0x3b929, 0x73c97, 0x31603, 0x4c7b9, 0x36f2b, 0x75bd7, 0x51647, 0x6c56f, 0x322cf, 0x61883, 0x3b0db, 0x584ab, 0x6b149, 0x6d523, 0x38de9, 0x4964f, 0x35405, 0x3152f, 0x1aaad, 0x3fa3f, 0x62b43, 0x75f5f, 0x1e1f1, 0x6f295, 0x5f3dd, 0x2757d, 0x1eee3, 0x58b61, 0x1eadb, 0x24a73, 0x375df, 0x36aff, 0x5c7f5, 0x5925b, 0x607b5, 0x2e7a1, 0x37f45, 0x4ac21, 0x2da03, 0x2d2c3, 0x4a4f5, 0x4d56f, 0x621fd, 0x34b65, 0x3610d, 0x65cfb, 0x553cd, 0x46ecd, 0x66b05, 0x63343, 0x73243, 0x66637, 0x6e4c7, 0x746d7, 0x22f9d, 0x2e039, 0x765a1, 0x368e7, 0x70957, 0x4e5ef, 0x5a72d, 0x695ed, 0x28643, 0x73e2b, 0x437ed, 0x66f41, 0x2f1d3, 0x1fd99, 0x6ec27, 0x75989, 0x1a62f, 0x26407, 0x1efe5, 0x60587, 0x74f2f, 0x6d75f, 0x286b1, 0x1b973, 0x3c31d, 0x694eb, 0x5439b, 0x59c69, 0x51043, 0x449bf, 0x63d6f, 0x2e86d, 0x2939f, 0x4273f, 0x51583, 0x1daeb, 0x58db7, 0x65209, 0x5f91d, 0x3b4b9, 0x35b07, 0x3fd57, 0x467a7, 0x370d5, 0x531cd, 0x724bd, 0x77bdb, 0x741dd, 0x69629, 0x57539, 0x60fb5, 0x730b1, 0x3c727, 0x6d379, 0x1d55d, 0x760af, 0x4dd3d, 0x41615, 0x72f67, 0x2b6df, 0x41c27, 0x385a3, 0x1ba4b, 0x5ca2f, 0x26779, 0x25def, 0x3adad, 0x5df0b, 0x36545, 0x2f507, 0x36217, 0x77f99, 0x3a9bb, 0x417cd, 0x4e1ed, 0x1c8e9, 0x265cd, 0x29e8b, 0x5adff, 0x63337, 0x1a11f, 0x48223, 0x4ff27, 0x5bf91, 0x558fb, 0x58211, 0x23e2d, 0x6f32f, 0x74bdb, 0x4d2b7, 0x1fc4f, 0x44bcd, 0x32e69, 0x5d095, 0x454b7, 0x31ddb, 0x42f0b, 0x52007, 0x2b6f9, 0x3d507, 0x2fa1f, 0x53495, 0x24727, 0x5305d, 0x221b7, 0x18f95, 0x6604d, 0x19093, 0x654d1, 0x78791, 0x61637, 0x55093, 0x6a4c5, 0x47e1f, 0x78b17, 0x5bb9d, 0x77035, 0x3bac7, 0x6ff85, 0x74c41, 0x6de97, 0x638ef, 0x3a459, 0x3c239, 0x428b3, 0x529f7, 0x2522f, 0x511a9, 0x7024f, 0x6d427, 0x3af3d, 0x66b87, 0x4c8e3, 0x63b11, 0x57cc5, 0x4fa09, 0x62765, 0x34063, 0x3ffdd, 0x6a8fd, 0x43745, 0x20f89, 0x1dcc3, 0x2a4eb, 0x40477, 0x33259, 0x311cf, 0x42101, 0x3a795, 0x2cf93, 0x1a747, 0x1fdb3, 0x1add7, 0x3ec93, 0x3693d, 0x54721, 0x65083, 0x4b427, 0x18af9, 0x29825, 0x74a7f, 0x2a113, 0x22e7d, 0x4daaf, 0x20ce3, 0x77b63, 0x2e921, 0x342df, 0x1b433, 0x1bd5d, 0x52547, 0x4905b, 0x22b11, 0x7730f, 0x2116d, 0x70feb, 0x26081, 0x26e81, 0x77c29, 0x5b817, 0x3a6e5, 0x3fb87, 0x7669d, 0x2353d, 0x239e9, 0x5be49, 0x517bd, 0x1ecb3, 0x624b5, 0x364d3, 0x59f9b, 0x68615, 0x20bc7, 0x722e9, 0x77633, 0x496d9, 0x2f01b, 0x4bef9, 0x5ea8f]
def get_ab(s,r):
ab = {}
cd = {}
for c in range(256):
ab.update( {c: h(c,s,r)} )
cd.update( {h(c,s,r): c} )
return(ab,cd)
def h(m,s,r):
b = chr(m)+s
b = b.encode()
for i in range(r):
b = dgst(b).hexdigest()[:MAX_LEN].encode()
return(b.decode())
def l():
return( len(dgst(b'').hexdigest()[:MAX_LEN]) )
def z(m,w='c'):
if( w == 'c' ):
return(compress(m.encode()))
else:
return(decompress(m.encode()))
def eh(d,a,hd):
b = ""
i = 0
for c in d:
i += 1
index = (c+PRIMES[i%len(PRIMES)]) % 256
b += a[ index ]
return(hd+z(b,'c'))
def dh(d,a,l,hd):
b = b""
j = 0
d = decompress(d[len(hd):]).decode()
for i in range(0,len(d),l):
j += 1
index = d[i:i+l]
c = a[index] - (PRIMES[j%len(PRIMES)]%256)
b += bytes( [ c ] )
return(b)
a,b = get_ab("s4lt",90)
txt = open("/tmp/z","rb").read()
hd = b""
enc = eh(txt,a,hd)
open("/tmp/z.enc","wb").write(enc)
system("xxd /tmp/z.enc|head -n 10")
dec = dh(enc,b,l(),hd)
open("/tmp/z.dec","wb").write(dec)
system("md5 /tmp/z /tmp/z.dec")