Putting it all together
Math 202 Cryptography
October 2, 2024

T. Ratliff, Wheaton College
ratliff_thomas@wheatoncollege.edu

Using Unicode (UTF-16) to encode and decode strings

Mathematica has some built in functionality to work with Unicode, but for our purposes, I think the website 
	 https://dencode.com/en/string/hex
is a little more user friendly. Be sure that you select the button “UTF-16”.

1. Encode the string “Destroyed by Hippie Powers”

	encoded = 
	
2. Decode the string “00490074002700730020006a00750073 007400200068006f00740020006f0063 00650061006e0020006d0069006c006b 00200077006900740068002000640065 0061006400200061006e0069006d0061 006c002000630072006f00750074006f 006e0073”

	string =

Using AES-128 to encrypt and decrypt

We’ll use the website 
	https://testprotect.com/appendix/AEScalc 
to encrypt and decrypt using AES-128. 

Since both the key and data are 128-bits, we can use 32-digit hexadecimal values. 
For these questions, use an AES key k = 616e6420696e20393837363534333231

3. Encrypt plaintext x = 00000000000000000000000000000001

	ciphertext y = 
	
4. Decrypt y = 0c7b51a599a4fe4805839f2b7ba4c9fa  a669e0f60def803580c6017979bc0418

Notice this is broken into two blocks, each a 32-digit hex value. You will need to decrypt each block separately.

	plaintext x = 
	
5. The plaintext x is actually a string encoded using UTF-16. Use the dencode.com website from #1 & #2 to decode the plain text. 

	string =

RSA with a 2048-bit key

Use the following RSA values:

p = 
q = 
n= p * q
e = 3
d =

Verify that these are valid parameters by checking that:
6 (a) p and q are prime
		The PrimeQ[ m ] command returns True if m is prime.
   (b) n is a 2048-bit number.  
   		The IntegerLength[ m ] command will tell you how many digits are in the base-10 expansion for m
   		The IntegerLength[ m, 2 ] command will tell you how many digits are in the binary expansion of m
   (c) d ≡(e^(-1)) mod ∅(n)

PrimeQ[ ]

IntegerLength[]

7. Alice uses your public RSA key to encrypt a message and sends you the ciphertext

y = 31006276680299820175476315067101395202225288565884935341984742766426424310079383827605936867721624925678054349696678077367589570042084385771058543751

Use your private key to decrypt the message

Mod[ ]

If something goes awry, the PowerMod[ ] function may be handy to know. 
PowerMod[ a, b, m] uses the square-and-multiply algorithm (I think) to compute (a^b) mod m.

What is the plaintext of Alice’s message?

	x =

Putting it all together

Alice sends you two messages: 
	y1 is encrypted using your RSA public key. After you decrypt y1, you will get an AES-128 key k
	y2 is encrypted using the AES-128 key k
	
	y1 = 14909639780808434924734302136901655814856858724571322675686892627403675116349093199375377724367270855567893021768000
	y2 = 3cd8270a7f4c7e521ac4a19c4e46ae6c  be76fa8a5f4a99cecccb5d5ae157df69  f82b092693b55852a6d19c4a8bfb5728  b12b44b5c703a00dfec18c94191b48bb  1a02fef3461c9dbae811834f8ec3bcdb  fe79ff685d5dd7a60f5fb7c9eb3cf2de
	
8. Decrypt y1 using your RSA keys to find the AES-128 key k.
	After you’ve found the plaintext, the command BaseForm[k, 16] will give you the hex expansion for k.
	This is the form you need for k in order to use the testprotect.com site. 

9. Decrypt y2 using the AES-128 key k from #8 to find the plain text. 

10. The plaintext in #10 is actually a string encoded using UTF-16. Use the dencode.com website from #1 & #2 to decode the plain text.

Generating your own RSA keys

Mathematica has the capability to generate random prime values in a particular range (we’ll see how it does this next semester). 
	RandomPrime[{a,b}] generates a random prime between a and b. 
	
For example, to generate a random three digit prime, we can use

RandomPrime[{100,1000}]

Or, if we want a random 8-bit prime,

RandomPrime[{2^7, 2^8}]

11. Generate two random 1024-bit primes p and q. 

12. Let n = p * q. Find a small value e that is relatively prime to ∅(n).
		The command GCD[a,b] is handy. 
		
13. Now we want to find d ≡ (e^(-1)) mod  ∅(n)
		Look at the example from class on Tuesday with the Extended Euclidean Algorithm, and try the command below. 
		You can modify this to find d for this problem.

ExtendedGCD[17, 3828]