Math_105_RSA_AES

756 days ago by jimlb

This worksheet, along with the links contained in it, will show how RSA can be combined with AES to send a secure message. 

We begin by picking some very large primes for our RSA system. 

p=next_prime(10^(200)+2^(150));p 
       
100000000000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000000000000000000000000000000000000000000000000000\
000000000001427247692705959881058285969449495136382746737
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001427247692705959881058285969449495136382746737
q=next_prime(20^(175));q 
       
478904856520590268236983445984471619880855975682375680000000000000000000\
000000000000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000000000000000000000000000000000000000000000000000\
000000000233
478904856520590268236983445984471619880855975682375680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000233
n=p*q;n 
       
478904856520590268236983445984471619880855975682375680000000000000000000\
000000000000000000000000000000000000000000000000000000000000000000000000\
000000000006835158514946912263664069459742566766728655012703776732097536\
252822397317950980225440731199281561600000000000000000000000000000000000\
000000000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000332548712400488652286580630881732366777179989721
47890485652059026823698344598447161988085597568237568000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000683515851494691226366406945974256676672865501270377673209753625282239731795098022544073119928156160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000332548712400488652286580630881732366777179989721

We now pick $e$.  Anything relatively prime to $\varphi(n)$ will work. 

e=next_prime(17^9);e 
       
118587876541
118587876541
gcd(e,(p-1)*(q-1)) 
       
1
1

We can now publish our public key $(e,n)$. 

Our next step is to calculate $d$, our decryption key.  Recall this is the inverse of $e$ modulo $\varphi(n)$. 

d=e.inverse_mod((p-1)*(q-1));d 
       
621182827906874183252867312024277899306208332028992330753411509473399906\
200281812498670095400135831663993333262665120209237662430200070581091795\
721422133538861967946401500432100637029285402652922123879502406558591383\
306497972017228709798703574568226066476097015283683086891002668704240526\
502101067754711471050388778037812829041168251371059011194846553652651764\
1138576055987631937313595237541580372184032925288677625906404962325
6211828279068741832528673120242778993062083320289923307534115094733999062002818124986700954001358316639933332626651202092376624302000705810917957214221335388619679464015004321006370292854026529221238795024065585913833064979720172287097987035745682260664760970152836830868910026687042405265021010677547114710503887780378128290411682513710590111948465536526517641138576055987631937313595237541580372184032925288677625906404962325

At this point, our RSA system is all set up. We have a public and private key.  Now someone can send us a message. 

Suppose we receive the following message:  "The secret message is 6UwxxHpLNfr2Boals3Q15SdE61h9N5r/FZ5eZycelgQ=  encrypted with AES.  The key, encrypted with your RSA, is 

464630443330994842561786542766816865683131475589134548065288755320147826\ 
982385094937022148886150120185978155089133672946591521924199865334265776\ 
224962217492339351611008020076112183377713450219818569359628015542557951\ 
188273455550926695684408597703239663077462549157881941862258910626158755\ 
328417447715699638704582111366459223021847360937319750649109865113262834\ 
56450033188802508366047581185187112392852797496662061603196914884815
k=46463044333099484256178654276681686568313147558913454806528875532014782698238509493702214888615012018597815508913367294659152192419986533426577622496221749233935161100802007611218337771345021981856935962801554255795118827345555092669568440859770323966307746254915788194186225891062615875532841744771569963870458211136645922302184736093731975064910986511326283456450033188802508366047581185187112392852797496662061603196914884815 
       

We now have to use our RSA private key to decrypt this.

key=power_mod(k,d,n);key 
       
143629089211703175079869649160695546662
143629089211703175079869649160695546662

We have the key for the AES encryption is given by 143629089211703175079869649160695546662.  We convert this to hexidecimal using any number of webpages.  For instance, we can go to https://www.rapidtables.com/convert/number/decimal-to-hex.html for this. 

Our output is 6C0DF4D54E4A175747FF7BB444B58326.

We now go to an AES encryption website (one such website is https://aesencryption.net/)and enter the secret message and the key.  We recover the original message "To be or not to be".