RosettaCodeData/Task/RSA-code/FreeBASIC/rsa-code.basic

45 lines
1.1 KiB
Plaintext

' version 17-01-2017
' compile with: fbc -s console
#Include Once "gmp.bi"
Dim As Mpz_ptr e, d, n, pt, ct
e = Allocate(Len(__mpz_struct))
d = Allocate(Len(__mpz_struct))
n = Allocate(Len(__mpz_struct))
pt = Allocate(Len(__mpz_struct)) : mpz_init(pt)
ct = Allocate(Len(__mpz_struct)) : mpz_init(ct)
mpz_init_set_str(e, "65537", 10)
mpz_init_set_str(d, "5617843187844953170308463622230283376298685", 10)
mpz_init_set_str(n, "9516311845790656153499716760847001433441357", 10)
Dim As ZString Ptr plaintext : plaintext = Allocate(1000)
Dim As ZString Ptr text : text = Allocate(1000)
*plaintext = "Rosetta Code"
mpz_import(pt, Len(*plaintext), 1, 1, 0, 0, plaintext)
If mpz_cmp(pt, n) > 0 Then GoTo clean_up
mpz_powm(ct, pt, e, n)
gmp_printf(!" Encoded: %Zd\n", ct)
mpz_powm(pt, ct, d, n)
gmp_printf(!" Decoded: %Zd\n", pt)
mpz_export(text, NULL, 1, 1, 0, 0, pt)
Print "As string: "; *text
clean_up:
DeAllocate(plaintext) : DeAllocate(text)
mpz_clear(e) : mpz_clear(d) : mpz_clear(n)
mpz_clear(pt) : mpz_clear(ct)
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End