This worksheet will build an RSA public key system for you. Enter your favorite (large) numbers inside the parentheses for "p" and "q"; the worksheet will find the smallest primes larger than your numbers, and take their product to build your modulus "N". Then enter your favorite number (less than ""N) for "e"; the worksheet will find the smallest number larger than yours that is relatively prime to (p-1)(q-1), and report it as your new "e". Then it will compute the inverse "d", mod (p-1)(q-1), of "e" and report it. Your public key is the pair (N,e) ; your secret decoding key is "d". These numbers should be entered into the encoding and decoding worksheets in the appropriate places. BUILDING A KEY: restart: with(numtheory): printlevel:=0: p:=nextprime(1848209356893535687356673569735690325609298573256098709865784344655375387903509539085397533460987536987309635096356903569083569087536879356908356093605367356907835879359782346907345322340972359023509763560973460978325609783250235035034096356980735609873560987356097834609783560983560935609390863568907469087346734609735790635609873560987360968356890356234234623470789009809872345789234509234548759478235357893907340983409335693568936356356908689696854069238907326572340970723409778098653876549583748594859438): q:=nextprime(27340356907835698735356908735609873569078346098735678969735697356703569073560935690873690783460973460973469087346098734608793460936403698235689022356907346907835690783569078356908735690873569087356098735690873460935609735690735690873907346907309739073569073097350973560973569073460973560973567357457858359350935890356908735785383503450935903356092356890234524324378923472457894235702457892454860485792357249753248095609089790876657507809870967908097823490234589235897685370432789356988640287651723655809865436): d:=1: N:=p*q; R:=(p-1)*(q-1): e:=3560792139672345781339857356907835609734609346908735607935690873562343245973597823498723498230230002349087356723409862349873526783503450340305935900578023457234578092435097823450942098735609792352372347823509324609825367892340978214765450: k:=10*floor(e/10)+2*(e-10*floor(e/10))+1: e:=k: while gcd(e,R)>1 do e:=e+2: end do; cat(`e:= `,e); gcdeR:=gcd(e,R); d:=1/e mod R; print("Publish N and e ; keep d secret. Destroy all evidence of p and q !"); ENCODING: restart: with(numtheory): printlevel:=0: > print(`This is my public key:`); N := 50530703457870752715703756173180064142965797133229821646531296701006446759045181787453809963947421348941174726295508572278040228467138300411751756173844312759617723371676972924616579040066742722721125795879304485139250675241951721576900737088418043524022980131152628171657068456983263507443595857425725891537085750348612093959133642414822242047671158740434368301751527451153139370155589640006725511206395108284848347212128069501791721351790498814621345225916614625495311536375965601981618126988633390567865241968372996712604095682412743788071642985715129626606215058881862947507755005581096670846578884559981076432173713247872840498939509343017459999947719918598562070550687925411426557841568611313785041713256644069574334056018299833461491082614281861114878811241953662001721142069432934320292297607988350924408466509753861410266665235871751090882976380649221830074008359679964838445166239477055104933336926909019732575268975459866668749277310166063499987077621486209383718097574216359202896753947776818370700921481; e := 3560792139672345781339857356907835609734609346908735607935690873562343245973597823498723498230230002349087356723409862349873526783503450340305935900578023457234578092435097823450942098735609792352372347823509324609825367892340978214765451;word:="Let's just type a few words. The Quick Brown Fox Jumped Over The Lazy Dog. 100163 043061!!!"; F:=table([("A")=11,("B")=12,("C")=13,("D")=14,("E")=15,("F")=16,("G")=17,("H")=18,("I")=19,("J")=20,("K")=21,("L")=22,("M")=23,("N")=24,("O")=25,("P")=26,("Q")=27,("R")=28,("S")=29,("T")=30,("U")=31,("V")=32,("W")=33,("X")=34,("Y")=35,("Z")=36,("a")=41,("b")=42,("c")=43,("d")=44,("e")=45,("f")=46,("g")=47,("h")=48,("i")=49,("j")=50,("k")=51,("l")=52,("m")=53,("n")=54,("o")=55,("p")=56,("q")=57,("r")=58,("s")=59,("t")=60,("u")=61,("v")=62,("w")=63,("x")=64,("y")=65,("z")=66,("0")=70,("1")=71,("2")=72,("3")=73,("4")=74,("5")=75,("6")=76,("7")=77,("8")=78,("9")=79,(" ")=81,("*")=82,("@")=83,(",")=84,(".")=85,("!")=86,("'")=87,("(")=88,(")")=89,("{")=90,("}")=91,("?")=92,("`&`")=93,("^")=94,("#")=95,("$")=96,("%")=97,("-")=98,("+")=99,("~")=37,("<")=38,(">")=39,("=")=40,(";")=67,(":")=68,("[")=69,("]")=80,("/")=10]): mess:=0: for i from length(word) to 1 by -1 do mess:=100*mess+F[word[i]] end do; message:=mess; printlevel:=1: if evalb(ilog10(mess)>ilog10(N)-1) then cat(`Message length, `,ilog10(mess),`, is more than modulus length, `,ilog10(N),`; cut message into pieces.`); else cat(`OK: message length, `,ilog10(mess),`, is less than modulus length, `,ilog10(N),` . Encoding...`); code:=mess&^e mod N; print(`Send the number labelled "code".`); end if; DECODING: restart: with(numtheory): printlevel:=0: code := 48060243676482550185323184292022419217928047933749938907846748780722721286359839816133384336438501386948505160897659383209290551471360365537175570834758204334968746748216487486929676662691867260009586902500350961902342830314682920779996098278522072512510094997434821314814618345628893781674324457478454424927170199183044309492352232294149922432533537681010130517842297616386157845621394732159969040290110093393012833091341044816723046811482785429305035925731864488953101320614535687096374184961254623816977860862673418696786082848130612003151713611309441026971193221355061182162817021468424613534347873149896853820493690131801648140149511220831112717029453828242903039289611052546322670365956496544618177604245432425536337722105314342920148309628743434068453886400423918729795592465313352694975814954646013225389122075975481890288473949158780502753671598488457437573559544762426105180253850624507696915747866010213887549186669422216366951508826736567327396428408507275046927448902945747653828889705644439635476404971; N := 50530703457870752715703756173180064142965797133229821646531296701006446759045181787453809963947421348941174726295508572278040228467138300411751756173844312759617723371676972924616579040066742722721125795879304485139250675241951721576900737088418043524022980131152628171657068456983263507443595857425725891537085750348612093959133642414822242047671158740434368301751527451153139370155589640006725511206395108284848347212128069501791721351790498814621345225916614625495311536375965601981618126988633390567865241968372996712604095682412743788071642985715129626606215058881862947507755005581096670846578884559981076432173713247872840498939509343017459999947719918598562070550687925411426557841568611313785041713256644069574334056018299833461491082614281861114878811241953662001721142069432934320292297607988350924408466509753861410266665235871751090882976380649221830074008359679964838445166239477055104933336926909019732575268975459866668749277310166063499987077621486209383718097574216359202896753947776818370700921481: d:=26182533308148547627499301174014710146976057452519725655111946755313505846910725026887093057699751669241155271196516699676242453998191539522623796619926562878072047462150949152816322422287083574663770914238967551793601144252904479404110371133301758103094168114644532248131386323627572733730583523382393488689273854039133334685566607695159166655062992764651260120968910842866605281477284095034712865971867945224895218512037979309729233808447951499241322864514617112632731127091632358897092884715372718116171057785558804979101029295805110873654076352961759619419801679412769630639335958256564370403967357017974567001151715105504723419177381755077121649491710350604599898438911350171456157562017232002127198989327961362000139049662743134100496381793225794591993519217047118597440590647769967832135017530211458492466997835872135172068751478339568569719455076461236845597780206784826218708763134282862397953131485741445366767724940880170488645699924110750671561395325199285049729883883329381091335133983750490621528369551: G:=table([(11)="A",(12)="B",(13)="C",(14)="D",(15)="E",(16)="F",(17)="G",(18)="H",(19)="I",(20)="J",(21)="K",(22)="L",(23)="M",(24)="N",(25)="O",(26)="P",(27)="Q",(28)="R",(29)="S",(30)="T",(31)="U",(32)="V",(33)="W",(34)="X",(35)="Y",(36)="Z",(41)="a",(42)="b",(43)="c",(44)="d",(45)="e",(46)="f",(47)="g",(48)="h",(49)="i",(50)="j",(51)="k",(52)="l",(53)="m",(54)="n",(55)="o",(56)="p",(57)="q",(58)="r",(59)="s",(60)="t",(61)="u",(62)="v",(63)="w",(64)="x",(65)="y",(66)="z",(70)="0",(71)="1",(72)="2",(73)="3",(74)="4",(75)="5",(76)="6",(77)="7",(78)="8",(79)="9",(81)=" ",(82)="*",(83)="@",(84)=",",(85)=".",(86)="!",(87)="'",(88)="(",(89)=")",(90)="{",(91)="}",(92)="?",(93)="`&`",(94)="^",(95)="#",(96)="$",(97)="%",(98)="-",(99)="+",(37)="~",(38)="<",(39)=">",(40)="=",(67)=";",(68)=":",(69)="[",(80)="]",(10)="/"]): print(``); decode:=code&^d mod N; dec:=decode: word2:="": while dec>0 do y:=dec-(100)*floor(dec/100); word2:= cat(word2,G[y]); dec:=floor(dec/100): end do; cat(`Decoded message: "`,word2,`"`);