Encrypted Password
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[]=
alicey4gsO7/JshJC23bqGnXlvg==
In[]:=
SignUp["bob","next_pass"];passwdfile
Out[]=
alicey4gsO7/JshJC23bqGnXlvg==,bobcCsp3junfaqBvMkZoQHU9Q==
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
Rainbow Table Attack
Hash Functions
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
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
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[]=
{ak,bl,cm,dn,eo,fp,gq,hr,is,jt,ku,lv,mw,nx,oy,pz,qa,rb,sc,td,ue,vf,wg,xh,yi,zj}
In[]:=
decryptFromShifterdLetters=Thread[shiftedLetters->letters]
Out[]=
{ka,lb,mc,nd,oe,pf,qg,rh,si,tj,uk,vl,wm,xn,yo,zp,aq,br,cs,dt,eu,fv,gw,hx,iy,jz}
In[]:=
StringReplace["hakuna matata",encryptWithShiftedLetters]
Out[]=
rkuexk wkdkdk
In[]:=
StringReplace["rkfo k qyyn nki",decryptFromShifterdLetters]
Out[]=
have a good day
Break the Caesar Cipher
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
Symmetric Key Cryptography
In[]:=
key=GenerateSymmetricKey[Method->"AES256"]
Out[]=
SymmetricKey
In[]:=
encryptedMessage=Encrypt[key,"Dear Bob, I hope this message finds you well..."]
Out[]=
EncryptedObject
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
Asymmetric Key Cryptography
In[]:=
keysAbrita=GenerateAsymmetricKeyPair[]
Out[]=
PrivateKeyPrivateKey,PublicKeyPublicKey
In[]:=
sharePublicKey=keysAbrita["PublicKey"]
Out[]=
PublicKey
In[]:=
hidePrivateKey=keysAbrita["PrivateKey"]
Out[]=
PrivateKey
In[]:=
message=Encrypt[sharePublicKey,"Good luck for ECE101 exam 2"]
Out[]=
EncryptedObject