SHA256 and Symmetric Encryption Examples
Summary
TLDRThis video tutorial covers two main questions. The first involves using Python's hash library to find an English word with a specific SHA-256 hash value. It guides viewers to use a provided dictionary file, strip newline characters, and loop through words to match the hash. The second question tackles decrypting cipher text generated by a symmetric encryption algorithm. The video explains the decryption process, which involves XORing each block of the cipher text with the key, and emphasizes the importance of key length for security. Additionally, it suggests an exhaustive search method to find both the key and the encrypted word from a given ciphertext.
Takeaways
- 🔐 **Hashlib Usage**: The video discusses using Python's hashlib library to find an English word with a specific SHA-256 hash digest.
- 📄 **Dictionary Utilization**: A 'dictionary.txt' file is provided, containing a list of meaningful English words to be used for hash matching.
- 🔍 **Word Processing**: The script explains how to process each word in the dictionary by removing newline characters.
- 🔢 **Hash Calculation**: It details the process of calculating the SHA-256 hash of each word and converting it to a hexadecimal digest.
- 🔎 **Target Matching**: The goal is to find a word whose hash matches a given target hash value.
- 🔄 **Decryption Function**: The video explains how to write a Python function to decrypt a cipher text generated by a symmetric encryption algorithm.
- 🔑 **Key Manipulation**: It describes how the encryption algorithm uses an 8-bit key to create a 32-bit extended key by repeating the key four times.
- 🔡 **Block Conversion**: The script mentions converting blocks of text into 32-bit integers for encryption and decryption processes.
- 🔐 **XOR Operation**: The exclusive OR (XOR) operation is central to both the encryption and decryption processes.
- 🔍 **Exhaustive Search**: For the security attack example, the script suggests an exhaustive search through all possible keys (0-255) to find the correct decryption.
Q & A
What is the task described in the first part of the video?
-The task is to use Python's hash library to find an English word whose SHA-256 hash digest matches a given decimal value.
What hint is provided for finding the word in the first task?
-The hint is to use the 'hashlib.sha256()' function in Python to calculate the hash digest of words from a provided dictionary.
How is the dictionary of words accessed according to the video?
-The dictionary is accessed by reading a file named 'dictionary.txt' which contains a list of meaningful English words.
What preprocessing is done to each word read from the dictionary?
-Each word is stripped of newline characters, particularly by removing the last character which is a newline.
What is the formula used to calculate the hash digest of a word?
-The formula used is 'hashlib.sha256(word.encode('ascii')).hexdigest()' which calculates the SHA-256 hash digest of the word in ASCII encoding.
How is the matching word identified in the first task?
-A loop is used to iterate over each word, calculate its hash digest, and compare it to the target hash digest to find a match.
What is the second task discussed in the video?
-The second task is to write a Python function for decrypting a cipher text generated by a specified symmetric encryption algorithm.
How is the encryption algorithm described in the video?
-The algorithm breaks the message into four-character blocks, converts each block to a 32-bit integer, and then XORs it with an extended key to produce the cipher text.
What is the decryption process as described in the video?
-Decryption involves XORing the cipher text with the same key used for encryption to retrieve the original message.
What is the significance of the exclusive OR (XOR) operation in the encryption and decryption process?
-The XOR operation is used because it is reversible; applying it twice with the same key returns the original value, which is essential for both encryption and decryption.
What is the approach to find the key and word if only the cipher text is known?
-An exhaustive search is performed over all possible keys (0 to 255) to decrypt the cipher text and match the result with a list of English words to find the original word and key.
Outlines
🔐 Hashing Words with Python
The speaker introduces a Python programming exercise involving the use of the hashlib library to find an English word with a specific SHA-256 hash digest. They provide a dictionary file 'dictionary.txt' containing a list of meaningful English words and instruct the audience to read this file to create a list of words. Each word must be stripped of newline characters, particularly the last character which is a newline. The target is to find a word whose SHA-256 hash digest matches a given long decimal number. The formula for hashing involves converting the word to its ASCII representation, hashing it with hashlib.sha256(), and then ignoring the 'ignore' part of the hash to get the hexadecimal digest. The process involves looping through each word, hashing it, and comparing it to the target hash value. Upon finding a match, the word is returned as the result.
🔄 Decrypting Symmetric Encryption
The second paragraph discusses a Python function for decrypting cipher text generated by a symmetric encryption algorithm. The encryption process involves taking a message, breaking it into four-character blocks, and converting each block into a 32-bit integer. An extended key is created by repeating an 8-bit key four times, shifting it by 8, 16, and 24 bits respectively, and then XORing each block with this extended key. The result is converted to hexadecimal, and any '0x' prefix is removed. To decrypt, the process is reversed by XORing the encrypted block with the key again, which yields the original block due to the properties of XOR. The function takes the cipher text and the key as inputs and returns the decrypted plain text. The speaker also touches on the security implications of short keys and the potential for exhaustive key searches.
🔍 Finding the Key and Decrypting
The final paragraph addresses a more complex challenge where the goal is to find both the encryption key and the original word from a given cipher text. The speaker suggests an exhaustive search method, iterating through all possible keys (0 to 255) and using the previously mentioned decryption function to test each key. If the decrypted text matches any word in the 'stripped Words' list (a list of English words without newlines), the correct key and word are found. The speaker reassures the audience that if the decryption function is implemented correctly, this process will yield the correct results. They also recap the steps for the first question, emphasizing the need to read the dictionary, loop through each word, and compare their hash values to the target.
Mindmap
Keywords
💡hashlib
💡SHA-256
💡hexadecimal digest
💡dictionary.txt
💡strip
💡loop
💡hash value
💡encryption algorithm
💡decrypt function
💡XOR
💡ciphertext
💡exhaustive search
Highlights
Introduction to the video's content about questions and homework.
Explanation of using Python's hashlib library to find a meaningful English word with a given hash.
Use of hint 'hashlib.sha256' to find the hash of a word.
Requirement to use a dictionary of meaningful English words for the task.
Instructions on how to read and process the 'dictionary.txt' file.
Details on creating a list of words and removing newline characters.
Description of the target hash value and the process of matching it with word hashes.
Formula for calculating the hash value of a word using hashlib.sha256.
Process of looping over words to find a match for the target hash value.
Method to return the word that matches the target hash as the result.
Introduction to the second question about decrypting cipher text.
Explanation of the symmetric encryption function and its process.
Details on breaking the message into four-character blocks for encryption.
Process of creating an extended key by repeating the original key four times.
Description of the XOR operation used for both encryption and decryption.
Instructions on implementing the decrypt function for the given encryption algorithm.
Explanation of the decryption process by XORing the key with each piece of cipher text.
Description of how to handle the leading zeros in the encrypted blocks.
Final step of returning the decrypted plain text.
Introduction to the third question about finding the key and word from a ciphertext.
Explanation of the exhaustive search method to find the key and word.
Details on the creation of the decrypt function and its usage in the exhaustive search.
Final advice on the importance of secure encryption and the weakness of short keys.
Transcripts
hello in this video I'm going to briefly
talk about
uh the questions and the homework
the first question I want to talk about
is
uh this one it says using the Python's
hash lib Library find a meaningful
English word who's asking coding has the
following shot 256 has a decimal digest
and it says basically as a hint use
hashlip.shaw uh word that in kodaski and
ignore that hex digest to get the
hexadecimal
Digestive and asking encoding of a given
word
so in order to
find a meaningful word you need to have
a list of all words I have posted a
dictionary.txt which is the list of all
the meaning rewards in English assuming
you have that one and you have it in
your
python project then you have it
available you can open the
actual file read that
read all the lines
and then in each line you're going to
see one word so basically you can create
and list of a strict words and for every
word in words you can strip them new
line
um out of the world and you can drop the
last letter by doing word called a
negative one that's going to drop the
last character of the award which is the
new line because in the dictionary.txt
as you saw every
line
belongs to one
word and there's a new line at the end
this operation is going to drop the last
character
and after that what you're gonna do is
you're going to
use the Target that is given which is
this pretty long
uh 256 uh
number in his decimal and uh
try to find out which meaningful word
has this hash digest or this hash value
in order to do that the best way is to
Loop over every single word in the strip
words array this one is gonna made of
is stewards array is made of
90 some thousand meaningful words
without a new line at their end that's
what I call the surfboard and uh
when you look over every word in this
list
try to find out if the target matches
any of the awards hash value calculated
by this formula the formula should be
SDR which means the string
representation a hash leap.shot 256 in
parentheses word that encode ASCII and
ignore dot hash dot hex Digest
and uh
basically in this
formula hex digest Returns the hex value
so you first calculate the shot 56 hash
value and then you convert it to hex and
see if that's equals the target when you
find the word whose hash is equal to
Target you return that as the result and
you submit your assignment this way
another question is that is tricky is
question three which is asking you to
write down a function a python function
for the decrypting
a cipher text
that is generated by the following
encryption algorithm the symmetric
encryption function we have
you can I mean the the encrypted
function we have here basically gets a
key which is a 8-bit integer it's going
to be a integer from two to zero to 255.
and uh as you see I have it here it
basically
gets a message breaks it into four
character pieces or four character
blocks then it's gonna call the make
block on every four characters this
Matlock is going to concatenate all the
four characters into one 32-bit integer
because every character is eight bits
long when you concatenate eight four
eight bits it gives you a 32-bit integer
and uh you don't need to be worried
about this part this is already done you
need to take care of the decryption
decryption is the reverse operation and
basically uh after it breaks the message
into four bits four character pieces or
four character blocks
and it applies the make lock on it to
convert every four character into one
32-bit integer it's going to basically
create an extended key using the same
Key by repeating that key four times
because the key is a bit long when you
repeat that
G four times you know
line you're going to get a 32-bit number
there is the way it's
repeats the same key four times is to
First add the key by the key after if
after it shifts at 8 bit to the left and
then adds it by the same key after it
shifts at 16 bit and added by the same
key after it shifts at 24 bits this way
is going to create the 32-bit integer
which is made of four identical quarters
and each quarter would be equal to the
key and after extended key is created
and the number is created all it does it
um
converts
the block which is uh going to be the
um
one of the blocks that we created before
it was an integer uh by it's gonna
cover the existing more the converts the
following to his decimal the block
exclusive or this carrot sign is
exclusive or with the extended key so in
other words it xores every block with X
and the key
um so basically we xor 2 hexadecimal two
32-bit numbers and then it's going to
convert it to hexadecimal and then
converts it to a string when you convert
his decimal to a string there would be a
zero X this two the colon is going to
drop that 0x because we don't like the
zero X we know what it's already X so we
don't want to keep the zero X which
emphasizes this is a hexadecimal so we
already know it so leave that
out of the encrypted parts and this is
going to be the the encrypted version of
that block
then we make sure that the length of the
encrypted block is not less than eight
uh hexadecimal digits because
uh the encrypted block is the encryption
of 32 bits which means eight hexadecimal
digits if the result of encryption is
less than eight digits less than here's
the small digit we're going to pair it
with leading zeros to a Shore
that the the overall length uh would be
the same the overall length of the
decrypted
ciphers to text is the same as the
actual
um
message blocks uh
uh that we created in this for Loop so
um anyways after it's
checks whether or not the length is less
than eight or not it's going to add
leading zeros if the length is less than
eight
um and may call the
um
encrypted P says eight digit longer it
takes this decimal digit launch and at
the end every encrypted block is going
to be added with the return value and at
the end we're going to return the return
value
the first part of this question says
implement the decrypt function for this
uh encryption so that it gets a
ciphertext and it returns to playing
text as output so basically we need to
create a function like this the cipher
is the first parameter the key is the
second parameter this is an 8-bit number
this can be any string what we're gonna
do is we're gonna break the cipher into
multiple eight bits pieces or epic
blocks you can also break it into 32-bit
pieces but because key is eight bits you
don't need to break it into 32-bit
pieces you can just break it in the same
size as the key and uh basically
exclusive or the key with each piece
and this is going to give you the
original value you may say how is it
possible well I'm just going to briefly
mention you know so you understand if a
exclusive or B is equal to C
imagine this is the plain text
this is the key and this is uh the the
cipher text
then I can say
be exclusive C is equal to a
and you can prove that because because
of the properties of exclusive
so because of this
um implication
I can say when I want to do the
decryption I do the same xor that I did
before so for encryption we do xor for
decryption we do X1 as well so that's
how the that's the nature of exclusive
and that's pretty much it after you do
the exclusive or for each piece
uh you exclusive the key with each piece
you get the result of exclusive or and
append the result of exclusive or with
the return value return value is an
empty string at the beginning but as you
go forward you're going to build that
return value gradually at the end you're
going to return the whole return value
return value basically begins with an
empty string and a
we gradually
decrypt every uh block and appended to
the return value at the end you get the
whole decrypted plain text and that's
the question that's the answer to the
question 3.8 for 2. B uh the question
says if we know that the following
ciphertext is the result of encrypting a
single meaningful English word with some
key find the key and find the word in
this case we have two unknown things one
is the key one is the
uh the word itself but there's nothing
to be worried about and then the
the ciphertext is given right so this is
kind of an a security attack that you
should always be aware and you know
avoid uh the security attack basically
does an exhaustive search on all the
possible keys because the key is pretty
short it's a very very you know weak key
and therefore it's a very weak
encryption algorithm
um
if you want to make it stronger you get
a elongate the length of key and make it
longer and longer but eight bit is
pretty simple I'll show you how to break
it uh basically you go every key
possible so you go for key from 0 all
the way to 255 for key in range 256 you
get the following you say if the Crypt
of ciphertext and key
the decrypt is the same function we call
there we created their uh I I didn't
create it but I'm going to show you the
creation of it but I just told you that
what you need to do is these three steps
should you follow it so if you've called
this function if you created correctly
create that function and then you call
it on ciphertext and key for every key
from 0 to 255 and see if the result is
in their stripped Words which is the
same list that I showed you this is a
list of all English
words
you get a print the key followed by the
decrypted
ciphertext with the key and that's gonna
generate the output if you make the
decrypt function correctly this uh for
Loop is going to answer the question
trippy hope you understand
um
how to uh do the rest basically you need
to implement the decrypt function and
um yeah that's pretty much all you need
to do
for this question for the first question
again as I said
uh first part you need to read the
dictionary and then you need to go over
every word in the dictionary to see if
the target matches any of the words
hash value calculated by the following
formula the formula was given as well
all right
have a good day
تصفح المزيد من مقاطع الفيديو ذات الصلة
5.0 / 5 (0 votes)