Encrypted Password

Set up my empty password file:
In[]:=
passwdfile=<||>;
Encryption algorithm:
In[]:=
crypt[password_]:=With[{enc=Encrypt[password,ByteArray[{0,0,0,0,0,0,0,0}],Method-><|"InitializationVector"->ByteArray[{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}]|>]},BaseEncode[enc["Data"]]];
To add a new user:
In[]:=
SignUp[user_,password_]:=AssociateTo[passwdfile,user->crypt[password]];
SignUp["alice","unicorn"];
passwdfile
SignUp["bob","dragon"];
passwdfile
To log in, the system takes the password that you typed, uses it to encrypt a block of 0 bytes, and compares if it matches the value stored in the /etc/passwd file:
In[]:=
LogIn[user_,password_]:=​​If[passwdfile[user]===crypt[password],(*storedin/etc/passwdfile=encryptedblock*)​​"Login as "<>user<>" successful!",​​"Login failed."];
In[]:=
LogIn["alice","12345678"]
LogIn["alice","unicorn"]
LogIn["alice",""]

Rainbow Table Attack


Hash Functions

In[]:=
book=ExampleData[{"Text","AliceInWonderland"}];​​Snippet[book,3]
In[]:=
StringLength[book]
In[]:=
Hash[book,"SHA","HexString"]
A digest is also very compact:
In[]:=
ByteCount[book]
In[]:=
ByteCount[Hash[book]]

Verifying data integrity

“Dear Bob, please find attached the molecule plot. The hash checksum is 2654733945256440784.”
In[]:=
molecule=
;
In[]:=
Hash[molecule]

Caesar Cipher

In[]:=
key=10
In[]:=
letters=CharacterRange["a","z"]
In[]:=
shiftedLetters=Join[letters[[key+1;;]],letters[[;;key]]];​​Grid[{letters,shiftedLetters},Frame->All]
In[]:=
encryptWithShiftedLetters=Thread[letters->shiftedLetters]
In[]:=
decryptFromShifterdLetters=Thread[shiftedLetters->letters]
In[]:=
StringReplace["hakuna matata",encryptWithShiftedLetters]
In[]:=
StringReplace["rkfo k qyyn nki",decryptFromShifterdLetters]

Break the Caesar Cipher

Give me an encrypted phrase:
ResourceFunction["CrackCaesarCipher"][" "]
"Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald"
ResourceFunction["CrackCaesarCipher"][" "]

Symmetric Key Cryptography

In[]:=
key=GenerateSymmetricKey[Method->"AES256"]
In[]:=
encryptedMessage=Encrypt[key,"Dear Bob, I hope this message finds you well..."]
In[]:=
BaseEncode[encryptedMessage["Data"],"Base85ASCII"]
In[]:=
Decrypt[key,encryptedMessage]

Asymmetric Key Cryptography

In[]:=
keysAbrita=GenerateAsymmetricKeyPair[]
In[]:=
sharePublicKey=keysAbrita["PublicKey"]
In[]:=
hidePrivateKey=keysAbrita["PrivateKey"]
In[]:=
message=Encrypt[sharePublicKey,"Good luck for ECE101 exam 2"]
In[]:=
BaseEncode[message["Data"],"Base85ASCII"]
In[]:=
Decrypt[hidePrivateKey,message]