1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| """ANSI X 9.17的伪随机数产生器""" import time import yaml from DES.function import xor from DES.function import hex2bin from DES.function import bin2hex from DES.en_for3 import en_for3
def TD(DT, K1, K2): """三重DES""" return en_for3(DT, K1, K2)
def DTi(present): """DTi(64):当前日期与时间, 每产生一个Ri后,自动更新一次""" now = list(str(present).ljust(16, '0')) now.remove('.') return "".join([hex(int(i))[2:] for i in (now[:16])])
def one_circle(present, K1, K2, Vi): """一轮加密""" print("present,Vi,K1,K2\n", present, Vi, K1, K2) value1 = TD(DTi(present), K1, K2) value2 = bin2hex("".join(xor(hex2bin(Vi), hex2bin(value1)))) Ri = TD(value2, K1, K2) value4 = bin2hex("".join(xor(hex2bin(value1), hex2bin(Ri)))) Vi_next = TD(value4, K1, K2) print("种子:", Ri, "\tVi+1:", Vi_next) return Ri, Vi_next
def init(from_file=False): Vi = "AAAAAAAAAAAAAAAA" K1 = "AAAAAAAAAAAAAAAA" K2 = "AAAAAAAAAAAAAAAA" if from_file: try: with open("init.yaml", "r") as f: params = yaml.load(f.read(), Loader=yaml.SafeLoader) Vi, K1, K2 = params['Vi'], params['K1'], params['K2'] except: pass return Vi, K1, K2
def generation(num_circle): Vi, K1, K2 = init(True) result = [] for num in range(num_circle): present = time.time() Ri, Vi = one_circle(present, K1, K2, Vi) result.append(Ri) params = { "Vi": Vi, "K1": K1, "K2": K2, } with open("init.yaml", "w") as f: f.write(yaml.dump(params)) return result
print(generation(1))
|