jukaukor

RSA_encrypt_decrypt.jl

Sep 5th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. # Julia-ohjelma RSA_encrypt_decrypt.jl
  2. # originmsg UTF-8 merkistön mukaista tekstiä
  3. # tekstin pituus < julkisen avaimen pituus (77 merkkiä)
  4. # teksti dekoodataan byte-muotoon jotta kelpaa powermod-funtioon
  5. # esimerkissä julkinen avain d on 256-bittinen eli 77 numeroinen
  6. # Juhani Kaukoranta 4.9.2019
  7.  
  8. function rsaencrypt(originmsg, n, e)
  9. bytes = parse(BigInt, "0x" * bytes2hex(collect(UInt8, originmsg)))
  10. return powermod(bytes, e, n)
  11. end
  12.  
  13. function rsadecrypt(cryptomsg, n, d)
  14. decoded = powermod(cryptomsg, d, n)
  15. return join(Char.(hex2bytes(string(decoded,base=16))))
  16. end
  17.  
  18. originmsg = "Hiphei - hörhöt!"
  19. n = big(98192617636653636141238012338108484137954487342964318282087546585872528680299)
  20. e = 65537
  21. d = big(33574153109097960014509849390092679748726042392305041135147739978253446722693)
  22.  
  23. cryptomsg = rsaencrypt(originmsg, n, e)
  24. plaintext = rsadecrypt(cryptomsg, n, d)
  25.  
  26. println("originmsg = ",originmsg)
  27. println("cryptomsg = ",cryptomsg)
  28. println("plaintext = ",plaintext)
Advertisement
Add Comment
Please, Sign In to add comment