# this is rsa.mpl # to see the demo do: maple < rsa.mpl #### The Making of Keys #### primebase := 2^64; keythreshold := 100; n := 0; p := 0; s := 0; while n <= 2^128 or p = s or p < keythreshold or s < keythreshold do #### first set primes p1 and p2 as you wish. ## for secure encryption choose large unobvious values, say 64 bits. ## p1 and p2 should both be large and be independent, ## in particular not be too close together. p1 := nextprime(primebase + rand()); p2 := nextprime(primebase + rand()); #### then compute the keys n := p1*p2; d := (p1-1)*(p2-1) + 1; factorlist := ifactor(d, lenstra); s := op(1, op(nops(factorlist), factorlist)); p := d/s; # no good if this is 1 or is s! ... ## reject and try different primes if p or s is small. od; # public key. Freely give this to anyone who wants to send secret # messages to you. P := [p, n]; # private (secret) key. Don't give this to anybody. Make it so your # encryption machine self-destructs if anyone tries to get this key out of it. S := [s, n]; # encryption/decryption function en_or_deCrypt := proc(M_block, Key) M_block &^ Key[1] mod Key[2]; ## ^^ this makes the exponentiation and mod work efficiently. end; #### The Using of Keys #### # trial encryption M_block := 1234567890123456789012345678901234567; # To send an encrypted message to the holder of the secret key, I do this. C_block := en_or_deCrypt(M_block, P); # A possessor of C_block can't figure out M_block without the private key. # C_block can be transmitted over insecure channels to the holder of the # private key. # Upon receipt of C_block, secret key owner does this. # M2_block will equal M_block: message decoded! M2_block := en_or_deCrypt(C_block, S); #### Topics Worthy of Note #### # # 1. The encryption and decryption processes are efficient, because we have # pretty good algorithms for computing powers mod n. # # 2. encryption/decryption works, in the sense that the decrypted result # is the original value because of this theorem of Euler: # Theorem: For n, p, s constructed as above, # if 0 <= a < n and # a is relatively prime to n, then (a^p)^s = a mod n. # # 3. Encryption/decryption works, in the sense that it is hard for a third # party to break the code. This is true even of a third party who knows # the method and knows n and p, but doesn't know p1 or p2 or s. # How hard to break? No one knows of any reasonable way to break RSA. # and many experts have tried. To break the code is tantamount to factoring # the large number n, finding p1 and p2. Mathematitions have studied this # problem extensively and have good reason, based in the theory of computation, # to think it is very hard, harder for example than to break other encryption # systems in current use (e.g. DSA). # # 4. RSA is a "public key" system. This means that knowing the encryption # method and encryption key is of no help to the code breaker. Encryption # systems with this property have only been discovered recently. # Public key systems are a very important development because they # facilititate key distribution and use and enable document signatures. # Signatures exploit the fact that the secret key and public key can be used # in either order. To sign a document I "encrypt" it with my private key. # To verify my signature you "decrypt" the result with my public key. # If what you get makes sense, you know that only a possessor of my private # key could have sent the document. #### Ignore this: out of curiosity... ifactor(d-1, lenstra); ifactor(p1-1, lenstra); ifactor(p2-1, lenstra); quit