Message Boards Message Boards

RSA encryption and decryption. Code interpretation

Posted 4 years ago

Hey, I would love to know what each line does in one project that I found. And I would like to ask if someone kind would be able to write documentation for this code? I will be very grateful :)

keys=Sort[Flatten[Table[Prime[i]Prime[j],{i,10,24},{j,i+1,25}]]];
key[k_]:=keys[[k]]   (* 1 \[LessEqual] k \[LessEqual] 120 *)

encrypt1[k_,m1_]:=Mod[m1^17,key[k]];
encrypt2[k_,m2_]:=Mod[m2^17,key[k]];
encrypt3[k_,m3_]:=Mod[m3^17,key[k]]

cip1[k_,m1_]:=If[encrypt1[k,m1]<=999,"0"<>ToString[encrypt1[k,m1]],
    ToString[encrypt1[k,m1]]];

cip2[k_,m2_]:=If[encrypt2[k,m2]<=999,"0"<>ToString[encrypt2[k,m2]],
    ToString[encrypt2[k,m2]]];

cip3[k_,m3_]:=If[encrypt3[k,m3]<=999,"0"<>ToString[encrypt3[k,m3]],
    ToString[encrypt3[k,m3]]]

d[k_]:=
    1/17 
       (1+Catch[Do[If[Divisible[1+m EulerPhi[key[k]],17]==True,Throw[m]],                     
            {m,17}]]*EulerPhi[key[k]]) 

message1[k_,m1_]:=FromCharacterCode[Mod[encrypt1[k,m1]^d[k],key[k]]];
message2[k_,m2_]:=FromCharacterCode[Mod[encrypt2[k,m2]^d[k],key[k]]];
message3[k_,m3_]:=FromCharacterCode[Mod[encrypt3[k,m3]^d[k],key[k]]]

ca[1]=Darker[Green];ca[2]=White;ca[3]=White;
cb[1]=White;cb[2]=Darker[Green];cb[3]=Darker[Green];
cr[1]=White;cr[2]=White;cr[3]=Red;

public=Graphics[{Darker[Green],Thickness[.1],Line[{{.10,0},{.10,-.08}}],
    Line[{{.20,0},{.20,-.08}}],Line[{{.10,-.04},{.20,-.04}}],
    Circle[{-.1,0},{.07,.1}],Thickness[.15],Line[{{0,0},{.25,0}}],
    Text[Style["public key",24,Bold,White],{.13,0}]},
    ImageSize->{245,150}];

private:=
    Graphics[{Red,Thickness[.1],Line[{{.10,-.04},{.20,-.04}}],
       Line[{{.15,0},{.15,-.08}}],Circle[{-.1,0},{.07,.1}],
       Thickness[.15],Line[{{0,0},{.25,0}}],
       Text[Style["private key",24,Bold,White],{.13,0}]},
       ImageSize->{245,150}]

alice[k_,m1_,m2_,m3_,a_]:=
    Framed[
     Pane[Column[{Text[Style[" Alice",36,Bold,Black]],public,
       Text[Style[Row[{key[k],"  17"}],36,ca[a],Bold]],
       Text[
         Style[Row[{"plaintext: ",
         FromCharacterCode[m1],FromCharacterCode[m2],FromCharacterCode[m3]}],36,Bold, ca[a]]]}],{260,350},
       ImageSizeAction->"ResizeToFit"]]

bob[k_,m1_,m2_,m3_,a_]:=
    Framed[
    Pane[Column[{Text[Style[" Bob",36,Bold,Black]],private,
       Text[Style[d[k],36,cr[a],Bold]],
       Text[Style["ciphertext: ",36,Bold,cb[a]]],
       Text[Style[Row[{cip1[k,m1], cip2[k,m2],cip3[k,m3]}],36,
         Bold,cb[a]]],
       Text[Style[Row[{"message:  ", message1[k,m1], message2[k,m2], 
         message3[k,m3]}],36,Bold,cr[a]]]
       }],{260,350},ImageSizeAction->"ResizeToFit"]]

Manipulate[
    Grid[{{alice[k,m1,m2,m3,a],Text[Style["\[ShortRightArrow]",64,Bold,Black]] ,
       bob[k,m1,m2,m3,a]}}],
    {{a,1,"message"},{1->"compose", 2->"  send  ",3->"decrypt"},ControlType->Setter},
    {{k,60,"public key"},1,120,1,ControlType->Slider,ImageSize->Large},               
    {{m1,88,"three"},Table[n->FromCharacterCode[n],{n,65,90,1}]},
    {{m2,89,"letter"},Table[n->FromCharacterCode[n],{n,65,90,1}]},
    {{m3,90,"message"},Table[n->FromCharacterCode[n],{n,65,90,1}]},
    ControlType->Setter,TrackedSymbols->{k,m1,m2,m3,a},
    SaveDefinitions->True]
Attachments:
POSTED BY: Raffaello Sanzio
2 Replies

What is the source of the Mathematica code you posted?

POSTED BY: Murray Eisenberg

Perhaps this simpler introduction to RSA in Mathematica will help, namely, the file RSA.nb at: http://people.math.umass.edu/~murray/Math_455_Eisenberg/Files/files.html

POSTED BY: Murray Eisenberg
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract