Mathematica’s built-in Hash[ ] and Encrypt[ ] functions

The purpose of this notebook is to go through an example of encrypting a long message using AES128 and creating an RSA digitial signature of a SHA256 hash of the encrypted message.  We’ll assume that the AES128 key has already been securely transmitted the two parties.

Math 202 Cryptography, October 21, 2016
T. Ratliff, Wheaton College
ratliff_thomas@wheatoncollege.edu

Hash[ ]

Mathematica’s built-in Hash[ ] function implements many of the standard hash protocols. 
A new standard, called SHA-3, was announced in 2015, but it doesn’t seem to be an option in Mathematica yet.
Our long message for today is a work from the most recent Nobel Laureate in Literature.

m="Johnny's in the basement
Mixing up the medicine
I'm on the pavement
Thinking about the government
The man in the trench coat
Badge out, laid off
Says he's got a bad cough
Wants to get it paid off
Look out kid
It's somethin' you did
God knows when
But you're doing it again
You better duck down the alleyway
Lookin' for a new friend
The man in the coonskin cap, in the big pen
Wants eleven dollar bills but you only got ten
Maggie comes fleet foot
Face full of black soot
Talkin' that the heat put
Plants in the bed but
The phone's tapped anyway
Maggie says that many say
They must bust in early May
Orders from the D.A. look out kid
Don't matter what you did
Walk on your tip toes
Don't try No Doz
Better stay away from those
That carry around a fire hose
Keep a clean nose
Watch the plain clothes
You don't need a weather man
To know which way the wind blows
Get sick, get well
Hang around a ink well
Ring bell, hard to tell
If anything is goin' to sell
Try hard, get barred
Get back, write braille
Get jailed, jump bail
Join the army, if you fail
Look out kid
You're gonna get hit
But losers, cheaters
Six-time users
Hang around the theaters
Girl by the whirlpool
Lookin' for a new fool
Don't follow leaders, watch the parkin' meters
Ah get born, keep warm
Short pants, romance, learn to dance
Get dressed, get blessed
Try to be a success
Please her, please him, buy gifts
Don't steal, don't lift
Twenty years of schoolin'
And they put you on the day shift
Look out kid
They keep it all hid
Better jump down a manhole
Light yourself a candle
Don't wear sandals
Try to avoid the scandals
Don't want to be a bum
You better chew gum
The pump don't work
'Cause the vandals took the handles
-- Bob Dylan, Subterranean Homesick Blues";

We can now hash this very long message.

Hash[m, "SHA256"]

Establish our RSA creditials to sign out message

Notice that we won’t want to sign the hash of the plaintext, but instead we’ll want to sign the hash of the ciphertext. 

The values of p and q below were generated using the RandomPrime[{2^1023,2^1024-1}] command.
We can use ExtendedGCD[ ] to find the multiplicative inverse of e mod ϕ(n)

p=;
q=;
n=p*q;
e=2^16+1;
ExtendedGCD[e,(p-1)*(q-1)]

d=

Thus, we have d. Our public key is (Subscript[k, pub])=(n, e) and our private values are (Subscript[k, pr])=(p, q, d)

Use Mathematica’s built-in Encrypt[ ] for AES

Mathematica’s Encrypt[ ] function implements AES, but first we need to specify the key to use. 

It’s important to note that this encryption will NOT correspond to the encryption from the website used in Problem Set 4, even if we were to use the same key. This is because Mathematica’s encryption uses an Initialization Vector and Cipher Block Chaining (CBC).

key=SymmetricKey[<|"Cipher"->"AES128","BlockMode"->"CBC","Key"->ByteArray["Jb77WnvMATI5wsE9mvsljA=="],"InitializationVector"->None|>]

y=Encrypt[key,m]

Let’s take a look at the components of the encrypted object.

y//InputForm

The encryption of m is contained in y[“Data”]
The initial output isn’t very interesting :(

y["Data"]

y["Data"]//InputForm

Let’s hash the ciphertext

h = Hash[y["Data"],"SHA256"]

And now sign the hashed value

s=PowerMod[h, d, n]

Therefore, we would send (y, s)

To verify the signature, Bob would calculate  x’ = s^e mod n   and compare x’ to the hashed value of y[“Data”]

xprime = PowerMod[s,e,n]

Hash[y["Data"],"SHA256"]