RSA主要是modular power計算,這個版次不好找好用的modular exponentiation計算,就自己做。另外字串加解密一起完成
- 1024//8
- plaintext='這是測試的明文,就試試1024-bit RSA,很長不是?但值定要小於n'
- print(plaintext)
- m=plaintext.encode('utf-8')
- mm=int.from_bytes(m, byteorder='little')
- mm
- def mod_powMSB(x, exp, n):
- e=bin(exp)[2:]
- # print(e)
- y=1
- for i in range(len(e)):
- y=(y*y)%n#模平方
- if e[i]=='1':
- y=y*x%n
- return y
- cc=mod_powMSB(mm, e, n)
- cc
- m2=mod_powMSB(cc, d, n)
- print(mm==m2)
- sz=int(np.ceil(m2.bit_length()/8))
- mm2=int.to_bytes(m2,sz,byteorder='little')
- print(mm2.decode('utf-8'))
沒有留言:
張貼留言