1 USING: math.miller-rabin kernel math math.functions namespaces
5 ! The private key is the only secret.
7 ! p,q are two random primes of numbits/2
11 ! private = public modinv phi
13 TUPLE: rsa modulus private-key public-key ;
19 : public-key 65537 ; inline
21 : rsa-primes ( numbits -- p q )
22 2/ 2 unique-primes first2 ;
24 : modulus-phi ( numbits -- n phi )
25 #! Loop until phi is not divisible by the public key.
26 dup rsa-primes [ * ] 2keep
28 dup public-key gcd nip 1 = [
36 : generate-rsa-keypair ( numbits -- <rsa> )
38 public-key over mod-inv +
41 : rsa-encrypt ( message rsa -- encrypted )
42 [ rsa-public-key ] keep rsa-modulus ^mod ;
44 : rsa-decrypt ( encrypted rsa -- message )
45 [ rsa-private-key ] keep rsa-modulus ^mod ;