Encrypted Password
Encrypted Password
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"]]];
In[]:=
passwdfile=<||>;
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!
Rainbow Table Attack
Rainbow Table Attack
Common passwords:
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:
ByteCount[book]
ByteCount[Hash[book]]
Caesar Cipher
Caesar Cipher
In[]:=
key=2
In[]:=
letters=CharacterRange["a","z"]
In[]:=
shiftedLetters=Join[letters[[key+1;;]],letters[[;;key]]]
In[]:=
Grid[{letters,shiftedLetters},Frame->All]
encryptWithShiftedLetters=Thread[letters->shiftedLetters]
decryptFromShifterdLetters=Thread[shiftedLetters->letters]
StringReplace["test",encryptWithShiftedLetters]
StringReplace[" ",decryptFromShifterdLetters]
Break the Caesar Cipher
Break the Caesar Cipher
In[]:=
ResourceFunction["CrackCaesarCipher"]["Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald"]
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[]=
;KK;X'Nm+J<cA;>PLb/,/Eu9T/p\5tZQmui5MPOGV@c*Qpl.Qt-D1P>4$T?H
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 the exam"]
Out[]=
EncryptedObject
In[]:=
BaseEncode[message["Data"],"Base85ASCII"]
Out[]=
*F/LKK]Ga5Y63K6h7)&=%ec8pQ\Z\O5j&R<ie(Tt[2!Z!dZ;?KO2K@>]MFZ`p8R,`H;@Y(7_Ffq@E,"+j:mV5">4Q^9%3CtM[>cJHRh"5:(WRY94'/j'%R`#-RaJX.CTsDon4*M7H9?AVn3s<@!2P:!15.!GnL]ZF!X'.,kmac^PC'$Jb`ad]KaZT@H^f\5K$=oiWKQiU*`Cq84:S5K?RhP=G=P*Nug4=[@^u-0KuA;Q4rH<jPb(,!WiQ&YkF$M=>rE)U8UP+CID=Ir(D]I;ZIkedUfQ&=G6a0COY/5QorbKX\!\CA#KXM*-=WtY52'?
In[]:=
Decrypt[hidePrivateKey,message]
Out[]=
Good luck for the exam