Encrypted Password

Set up my empty password file:
In[]:=
passwdfile=<||>;
Crypt 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]];
In[]:=
SignUp["alice","my_pass"];​​passwdfile
Out[]=
alicey4gsO7/JshJC23bqGnXlvg==
In[]:=
SignUp["bob","next_pass"];​​passwdfile
Out[]=
alicey4gsO7/JshJC23bqGnXlvg==,bobcCsp3junfaqBvMkZoQHU9Q==
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"]
Out[]=
Login failed.
In[]:=
LogIn["alice","my_pass"]
Out[]=
Login as alice successful!
In[]:=
LogIn["alice","y4gsO7/JshJC23bqGnXlvg=="]
Out[]=
Login failed.

Rainbow Table Attack


Hash Functions

In[]:=
book=ExampleData[{"Text","AliceInWonderland"}];​​Snippet[book,3]
Out[]=
I--DOWN THE RABBIT-HOLE Alice was beginning to get very tired of sitting by hersister on the bank, and of having nothing to do. Once or twice she had peepedinto the book her sister was reading, but it had no pictures or conversations in
In[]:=
StringLength[book]
Out[]=
51722
In[]:=
Hash[book,"SHA","HexString"]
Out[]=
e34ff81046172fd124023ebcd8d34af0a1ce62fa
A digest is also very compact:
In[]:=
ByteCount[book]
Out[]=
53248
In[]:=
ByteCount[Hash[book]]
Out[]=
16

Verifying data integrity

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

Caesar Cipher

In[]:=
key=10
Out[]=
10
In[]:=
letters=CharacterRange["a","z"]
Out[]=
{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}
In[]:=
shiftedLetters=Join[letters[[key+1;;]],letters[[;;key]]];​​Grid[{letters,shiftedLetters},Frame->All]
Out[]=
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
a
b
c
d
e
f
g
h
i
j
In[]:=
encryptWithShiftedLetters=Thread[letters->shiftedLetters]
Out[]=
{ak,bl,cm,dn,eo,fp,gq,hr,is,jt,ku,lv,mw,nx,oy,pz,qa,rb,sc,td,ue,vf,wg,xh,yi,zj}
In[]:=
decryptFromShifterdLetters=Thread[shiftedLetters->letters]
Out[]=
{ka,lb,mc,nd,oe,pf,qg,rh,si,tj,uk,vl,wm,xn,yo,zp,aq,br,cs,dt,eu,fv,gw,hx,iy,jz}
In[]:=
StringReplace["hakuna matata",encryptWithShiftedLetters]
Out[]=
rkuexk wkdkdk
In[]:=
StringReplace["rkfo k qyyn nki",decryptFromShifterdLetters]
Out[]=
have a good day

Break the Caesar Cipher

In[]:=
ResourceFunction["CrackCaesarCipher"]["Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald"]
Out[]=
The quick brown fox jumps over the lazy dog
ResourceFunction["CrackCaesarCipher"][" "]

Symmetric Key Cryptography

In[]:=
key=GenerateSymmetricKey[Method->"AES256"]
Out[]=
SymmetricKey
Cipher: AES256
Key size:
256
b

In[]:=
encryptedMessage=Encrypt[key,"Dear Bob, I hope this message finds you well..."]
Out[]=
EncryptedObject
Data length:
48
B
Original form: String

In[]:=
BaseEncode[encryptedMessage["Data"],"Base85ASCII"]
Out[]=
d>@^akE0/%f-NfIJMfR<n8_&hYd.1aO@PWjP4h"B"iF:]1)Ci!1f&J,-3@6_
In[]:=
Decrypt[key,encryptedMessage]
Out[]=
Dear Bob, I hope this message finds you well...

Asymmetric Key Cryptography

In[]:=
keysAbrita=GenerateAsymmetricKeyPair[]
Out[]=
PrivateKeyPrivateKey
Type: RSA
Public modulus size:
2048
b
Private exponent size:
2048
b
,PublicKeyPublicKey
Type: RSA
Public modulus size:
2048
b

In[]:=
sharePublicKey=keysAbrita["PublicKey"]
Out[]=
PublicKey
Type: RSA
Public modulus size:
2048
b

In[]:=
hidePrivateKey=keysAbrita["PrivateKey"]
Out[]=
PrivateKey
Type: RSA
Public modulus size:
2048
b
Private exponent size:
2048
b

In[]:=
message=Encrypt[sharePublicKey,"Good luck for ECE101 exam 2"]
Out[]=
EncryptedObject
Data length:
256
B
Original form: String
