|\^/| Maple 8 (SUN SPARC SOLARIS) ._|\| |/|_. Copyright (c) 2002 by Waterloo Maple Inc. \ MAPLE / All rights reserved. Maple is a registered trademark of <____ ____> Waterloo Maple Inc. | Type ? for help. # this is rsa.mpl # to see the demo do: maple < rsa.mpl > #### The Making of Keys #### > > primebase := 2^64; primebase := 18446744073709551616 > keythreshold := 100; keythreshold := 100 > > n := 0; p := 0; s := 0; 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; p1 := 18446744501129220769 p2 := 18446744394820244917 n := 340282380728886628302388063081943081173 d := 340282380728886628265494574185993615489 bytes used=460044836, alloc=4979824, time=91.96 factorlist := (4211902492442758319) (1881708063773) (42934747) s := 42934747 p := 7925570883855135521480878277587 > # public key. Freely give this to anyone who wants to send secret # messages to you. > P := [p, n]; P := [7925570883855135521480878277587, 340282380728886628302388063081943081173] # 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]; S := [42934747, 340282380728886628302388063081943081173] > # 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; en_or_deCrypt := proc(M_block, Key) `&^`(M_block, Key[1]) mod Key[2] end proc > #### The Using of Keys #### > # trial encryption > M_block := 1234567890123456789012345678901234567; 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); C_block := 14959322133543351539349782604567312101 > # 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); M2_block := 1234567890123456789012345678901234567 > #### 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); bytes used=536050992, alloc=4979824, time=108.25 7 4 (2) (3) (7) (17309379675127) (21660461) (2957056001) (4229) > ifactor(p1-1, lenstra); bytes used=564053556, alloc=4979824, time=114.91 5 2 (2) (3) (2957056001) (21660461) > ifactor(p2-1, lenstra); 2 2 (2) (3) (7) (17309379675127) (4229) > > quit bytes used=566656440, alloc=4979824, time=115.49