def_KDF(self, bit_string, klen): klen = int(klen) ct = 0x00000001 rcnt = math.ceil(klen/32) zin = [i for i inbytes.fromhex(bit_string.decode())] ha = "" for i inrange(rcnt): msg = zin + \ [i for i in binascii.a2b_hex(('%08x' % ct).encode())] ha = ha + sm3.sm3_hash(msg) ct += 1 return ha[0: klen * 2]
defencrypt(self, M, mode): k = randint(1, int(self.ecc_param['n'], 16)-1) public_key = self.public_key print('[+] random number k:', k) # 实际上是随机值 # k = int('4C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F', 16)
l = len(self.ecc_param['n']) g = (int(self.ecc_param['g'][0: l], 16), int(self.ecc_param['g'][l:], 16)) result = self._mutiply_p(k, g)
t = self._KDF(binascii.hexlify(bytestring), klen) iM = C2 ^ int(t, 16)
u = sm3.sm3_hash([ i for i inbytes.fromhex('%s%s%s' % (hex(C1[0])[2:], hex(iM)[2:], hex(C1[1])[2:])) ])
if binascii.hexlify(C3).decode() == u: return binascii.a2b_hex(hex(iM)[2:]).decode()
else: print("[+] Something Error during decrypting...") exit(-1)
数据分析
输入数据1:
One is always on a strange road, watching strange scenery and listening to strange music. Then one day, you will find that the things you try hard to forget are already gone.
运行结果如下:
首先可以看到一系列初始化操作以及输入的明文,由随机数生成器产生的随机数。
这里针对加密明文的表现形式选择了非压缩模式,密文结果由16进制表示。
当解密时会有验证Hash的操作,当Hash验证成功且解密的密文与输入明文数据相同时,会输出“[+] You have successfully decrypted”,由此可以看到加密解密成功
输入数据2:
Most of us compare ourselves with anyone we think is happier — a relative, someone we know a lot, or someone we hardly know. As a result, what we do remember is anything that makes others happy, anything that makes ourselves unhappy, totally forgetting that there is something happy in our own life.
运行结果:
输入数据3:
Happiness is not about being immortal nor having food or rights in one’s hand. It’s about having each tiny wish come true, or having something to eat when you are hungry or having someone’s love when you need love.