herrDeng網內搜尋

自訂搜尋

Ads

2022年5月17日 星期二

python版RSA文字加解密


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

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章