RSA demo Mike May, S.J., maymk@slu.edu A fast demo of RSA cryptography: (This is an exercise in using embedded componenets. In this example, all the code is in the action section of the buttons.) Set a size for the primes used in terms of bits. (The size must be between 5 and 600 bits.) Size of p1use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. s1 := parse(GetProperty(Textp1size,'value')): if not((type(s1,integer)) and (s1 > 5)) then s1 := 5; end if; if (s1 > 600) then s1 := 600; end if; SetProperty(Textp1size,'value',s1); end use; in bits. Size of p2 use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. s2 := parse(GetProperty(Textp2size,'value')): if not((type(s1,integer)) and (s2 > 5)) then s2 := 5; end if; if (s2 > 600) then s1 := 600; end if; SetProperty(Textp2size,'value',s2); end use; in bits. use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. s1 := parse(GetProperty(Textp1size,'value')); s2 := parse(GetProperty(Textp2size,'value')); p1 := nextprime(rand(2^s1)()); p2 := nextprime(rand(2^s2)()); SetProperty(Textp1,'value',p1); SetProperty(Textp2,'value',p2); n := p1*p2; n1 := (p1-1)*(p2-1); SetProperty(Textn, 'value', n); SetProperty(Textphin, 'value', n1); end use; p1 = , p2 = We also want to compute n = p1*p2 and phi(n) = (p1-1)*(p2-1). If we let Maple choose the primes, that was done by the last button. If we inserrt our own primes, the next button computes n and phi(n). use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. p1 := parse(GetProperty(Textp1,'value')); p2 := parse(GetProperty(Textp2,'value')); if (isprime(p1) and isprime(p2)) then n := p1*p2; n1 := (p1-1)*(p2-1); else n := "Either p1 or p2 is not prime"; n1 := n; end if: SetProperty(Textn, 'value', n); SetProperty(Textphin, 'value', n1); end use; n = phi(n) = Set the encryption key. The default is e = 2^16+1 = 65537. Recall that phi(n) and e should be relatively prime. e = . We now want to compute d, the inverse of e mod phi(n). use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. e := parse(GetProperty(Texte, 'value')); phin := parse(GetProperty(Textphin, 'value')); if (igcd(e,phin)=1) then; d := (1/e) mod phin; else; d := "Bad choice for e in relation to phi(n), try again"; end if; SetProperty(Textd,'value',d); end use; d = We would like to publish n and e, destroy p1 and p2, and keep d a secret. We are ready to send numerical message, Recall that the message should be less than n. message = The ciphertext is message^e mod n. use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. m := parse(GetProperty(Textmessage,'value')); e := parse(GetProperty(Texte,'value')); n := parse(GetProperty(Textn,'value')); if ((m<n) and type(m,integer)) then; c := Power(m,e) mod n; else; c := "Bad message, try again"; end if; SetProperty(Textciphertext,'value',c); end use; ciphertext = We can also check deciphering use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. c := parse(GetProperty(Textciphertext,'value')); SetProperty(Textdeciphermessage,'value',c); end use; deciphermessage= The decrypted message is ciphertext^d mod n. use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. n := parse(GetProperty(Textn,'value')); d := parse(GetProperty(Textd,'value')); c := parse(GetProperty(Textdeciphermessage,'value')); m := Power(c,d) mod n; SetProperty(Textdecodedmessage,'value',m); end use; decodedmess age= RSA with an ASCII message We need to set the modulus n and the exponent, which is either d or e. We can either enter our own values or copy the values from above. modulus = use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. n := parse(GetProperty(Textn,'value')); SetProperty(Textmodulus,'value',n); end use; exponent = use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. e := parse(GetProperty(Textd,'value')); if not(type(e,integer)) then e := "bad value"; end if: SetProperty(Textexponent,'value',e); end use; use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. e := parse(GetProperty(Texte,'value')); if not(type(e, integer)) then e := "bad value"; end if; SetProperty(Textexponent,'value',e); end use; text = use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. p1 := GetProperty(Texttext,'value'); p2 := convert(p1,bytes); num := 0: for count from 1 to nops(p2) do num := 256*num + op(count,p2); end do: SetProperty(Textnumber,'value',num); end use; number = use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. num := parse(GetProperty(Textnumber,'value')); exponent := parse(GetProperty(Textexponent,'value')); modulus := parse(GetProperty(Textmodulus,'value')); if (type(num,integer)) then if (num < modulus) then num2 := Power(num, exponent) mod modulus; else num2 := "error, number too big for modulus"; end if: else num2 := "error, number must be an integer."; end if: (SetProperty(Textnumber2,'value', num2)); end use; use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. e := parse(GetProperty(Textd,'value')); if not(type(e,integer)) then e := "bad value"; end if: SetProperty(Textexponent,'value',e); end use; use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. e := parse(GetProperty(Texte,'value')); if not(type(e, integer)) then e := "bad value"; end if; SetProperty(Textexponent,'value',e); end use; number2 = use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. num := parse(GetProperty(Textnumber2,'value')); exponent := parse(GetProperty(Textexponent,'value')); modulus := parse(GetProperty(Textmodulus,'value')); if (type(num,integer)) then if (num < modulus) then num2 := Power(num, exponent) mod modulus; else num2 := "error, number too big for modulus"; end if: else num2 := "error, number must be an integer."; end if: (SetProperty(Textnumber3,'value', num2)); end use; use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. e := parse(GetProperty(Textd,'value')); if not(type(e,integer)) then e := "bad value"; end if: SetProperty(Textexponent,'value',e); end use; use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. e := parse(GetProperty(Texte,'value')); if not(type(e, integer)) then e := "bad value"; end if; SetProperty(Textexponent,'value',e); end use; number3 = use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. num1 := parse(GetProperty(Textnumber2,'value')); l1 := []: while num1 > 0 do; l1 := [op(l1), irem(num1, 256)]; num1 := iquo(num1, 256); end do: l1 := ListTools[Reverse](l1): p3 := convert(l1,bytes); SetProperty(Texttext2,'value',p3) end use; use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. num := parse(GetProperty(Textnumber2,'value')); SetProperty(Textnumber,'value',num); end use; use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. num1 := parse(GetProperty(Textnumber3,'value')); l1 := []: while num1 > 0 do; l1 := [op(l1), irem(num1, 256)]; num1 := iquo(num1, 256); end do: l1 := ListTools[Reverse](l1): p3 := convert(l1,bytes); SetProperty(Texttext2,'value',p3) end use; use DocumentTools in # Enter Maple commands to be executed when the specified # action is carried out on the component. # Use: # GetProperty( component_name, attribute_name ) # and # SetProperty( component_name, attribute_name, value ) # to access any of the attributes of the component. # See ?CustomizingComponents for more information. num := parse(GetProperty(Textnumber3,'value')); SetProperty(Textnumber,'value',num); end use; text2 =