SHA256 and Symmetric Encryption Examples

Kianoosh Boroojeni
12 Sept 202313:59

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

00:00

๐Ÿ” 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.

05:04

๐Ÿ”„ 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.

10:04

๐Ÿ” 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

hashlib is a Python library used for generating hash values. In the video, it is used to find an English word that matches a given SHA-256 hash digest. The script mentions using hashlib to compute the hash of each word in a dictionary to find a match for the target hash value.

๐Ÿ’กSHA-256

SHA-256 is a cryptographic hash function from the SHA-2 family. The video discusses using SHA-256 to generate a hash digest of a word in hexadecimal format. The task is to find a word whose SHA-256 hash matches a given target hash.

๐Ÿ’กhexadecimal digest

A hexadecimal digest is a hash value represented in hexadecimal format. The video script instructs to ignore the hexadecimal digest and instead focus on the decimal digest when comparing hash values.

๐Ÿ’กdictionary.txt

dictionary.txt is a file mentioned in the video that contains a list of meaningful English words. It is used to search for a word whose hash matches a given target hash value.

๐Ÿ’กstrip

In the context of the video, 'strip' refers to the action of removing newline characters from each word read from the dictionary.txt file. This is necessary to clean the data before processing.

๐Ÿ’กloop

A loop is a programming construct used to iterate over a collection of items. The video describes using a loop to go through each word in the list to find a match for the target hash value.

๐Ÿ’กhash value

A hash value is the result produced by a hash function. In the video, the hash value is used to identify a word that matches a specific SHA-256 hash digest.

๐Ÿ’กencryption algorithm

The encryption algorithm discussed in the video is a symmetric encryption function that encrypts messages using a key. The script describes how the algorithm breaks messages into blocks and applies the encryption process.

๐Ÿ’กdecrypt function

The decrypt function is a part of the video's discussion on reversing the encryption process. It is used to convert encrypted text back into its original form using the same key that was used for encryption.

๐Ÿ’กXOR

XOR, or exclusive or, is a bitwise operation used in both the encryption and decryption processes described in the video. It is used to combine the message block with the extended key to produce the encrypted text, and then again in reverse to decrypt it.

๐Ÿ’กciphertext

Ciphertext is the encrypted form of plaintext. The video script includes a task to implement a function that takes ciphertext and a key to return the original plaintext.

๐Ÿ’กexhaustive search

Exhaustive search is a method of trying all possible keys to decrypt a ciphertext when the key size is small. The video mentions using exhaustive search to find the key and the original word from a given ciphertext.

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

play00:01

hello in this video I'm going to briefly

play00:04

talk about

play00:05

uh the questions and the homework

play00:09

the first question I want to talk about

play00:10

is

play00:12

uh this one it says using the Python's

play00:14

hash lib Library find a meaningful

play00:17

English word who's asking coding has the

play00:20

following shot 256 has a decimal digest

play00:23

and it says basically as a hint use

play00:27

hashlip.shaw uh word that in kodaski and

play00:32

ignore that hex digest to get the

play00:36

hexadecimal

play00:37

Digestive and asking encoding of a given

play00:40

word

play00:41

so in order to

play00:44

find a meaningful word you need to have

play00:47

a list of all words I have posted a

play00:50

dictionary.txt which is the list of all

play00:53

the meaning rewards in English assuming

play00:56

you have that one and you have it in

play00:58

your

play00:59

python project then you have it

play01:02

available you can open the

play01:08

actual file read that

play01:12

read all the lines

play01:14

and then in each line you're going to

play01:18

see one word so basically you can create

play01:23

and list of a strict words and for every

play01:26

word in words you can strip them new

play01:29

line

play01:31

um out of the world and you can drop the

play01:33

last letter by doing word called a

play01:36

negative one that's going to drop the

play01:38

last character of the award which is the

play01:40

new line because in the dictionary.txt

play01:44

as you saw every

play01:46

line

play01:48

belongs to one

play01:51

word and there's a new line at the end

play01:53

this operation is going to drop the last

play01:56

character

play01:58

and after that what you're gonna do is

play02:01

you're going to

play02:04

use the Target that is given which is

play02:07

this pretty long

play02:09

uh 256 uh

play02:13

number in his decimal and uh

play02:17

try to find out which meaningful word

play02:20

has this hash digest or this hash value

play02:26

in order to do that the best way is to

play02:28

Loop over every single word in the strip

play02:30

words array this one is gonna made of

play02:34

is stewards array is made of

play02:38

90 some thousand meaningful words

play02:42

without a new line at their end that's

play02:45

what I call the surfboard and uh

play02:49

when you look over every word in this

play02:51

list

play02:52

try to find out if the target matches

play02:55

any of the awards hash value calculated

play02:59

by this formula the formula should be

play03:02

SDR which means the string

play03:04

representation a hash leap.shot 256 in

play03:08

parentheses word that encode ASCII and

play03:12

ignore dot hash dot hex Digest

play03:16

and uh

play03:19

basically in this

play03:22

formula hex digest Returns the hex value

play03:24

so you first calculate the shot 56 hash

play03:28

value and then you convert it to hex and

play03:31

see if that's equals the target when you

play03:33

find the word whose hash is equal to

play03:36

Target you return that as the result and

play03:38

you submit your assignment this way

play03:41

another question is that is tricky is

play03:44

question three which is asking you to

play03:46

write down a function a python function

play03:49

for the decrypting

play03:52

a cipher text

play03:55

that is generated by the following

play03:57

encryption algorithm the symmetric

play03:59

encryption function we have

play04:02

you can I mean the the encrypted

play04:06

function we have here basically gets a

play04:09

key which is a 8-bit integer it's going

play04:12

to be a integer from two to zero to 255.

play04:16

and uh as you see I have it here it

play04:20

basically

play04:23

gets a message breaks it into four

play04:27

character pieces or four character

play04:29

blocks then it's gonna call the make

play04:32

block on every four characters this

play04:35

Matlock is going to concatenate all the

play04:38

four characters into one 32-bit integer

play04:41

because every character is eight bits

play04:43

long when you concatenate eight four

play04:45

eight bits it gives you a 32-bit integer

play04:48

and uh you don't need to be worried

play04:50

about this part this is already done you

play04:52

need to take care of the decryption

play04:53

decryption is the reverse operation and

play04:55

basically uh after it breaks the message

play04:59

into four bits four character pieces or

play05:03

four character blocks

play05:05

and it applies the make lock on it to

play05:07

convert every four character into one

play05:09

32-bit integer it's going to basically

play05:13

create an extended key using the same

play05:17

Key by repeating that key four times

play05:19

because the key is a bit long when you

play05:21

repeat that

play05:23

G four times you know

play05:27

line you're going to get a 32-bit number

play05:29

there is the way it's

play05:32

repeats the same key four times is to

play05:36

First add the key by the key after if

play05:40

after it shifts at 8 bit to the left and

play05:43

then adds it by the same key after it

play05:45

shifts at 16 bit and added by the same

play05:48

key after it shifts at 24 bits this way

play05:51

is going to create the 32-bit integer

play05:54

which is made of four identical quarters

play05:58

and each quarter would be equal to the

play06:00

key and after extended key is created

play06:03

and the number is created all it does it

play06:09

um

play06:10

converts

play06:12

the block which is uh going to be the

play06:20

um

play06:21

one of the blocks that we created before

play06:23

it was an integer uh by it's gonna

play06:27

cover the existing more the converts the

play06:30

following to his decimal the block

play06:32

exclusive or this carrot sign is

play06:35

exclusive or with the extended key so in

play06:38

other words it xores every block with X

play06:41

and the key

play06:42

um so basically we xor 2 hexadecimal two

play06:46

32-bit numbers and then it's going to

play06:49

convert it to hexadecimal and then

play06:50

converts it to a string when you convert

play06:53

his decimal to a string there would be a

play06:55

zero X this two the colon is going to

play06:59

drop that 0x because we don't like the

play07:01

zero X we know what it's already X so we

play07:03

don't want to keep the zero X which

play07:06

emphasizes this is a hexadecimal so we

play07:08

already know it so leave that

play07:10

out of the encrypted parts and this is

play07:14

going to be the the encrypted version of

play07:17

that block

play07:18

then we make sure that the length of the

play07:21

encrypted block is not less than eight

play07:25

uh hexadecimal digits because

play07:27

uh the encrypted block is the encryption

play07:31

of 32 bits which means eight hexadecimal

play07:35

digits if the result of encryption is

play07:38

less than eight digits less than here's

play07:40

the small digit we're going to pair it

play07:42

with leading zeros to a Shore

play07:46

that the the overall length uh would be

play07:51

the same the overall length of the

play07:53

decrypted

play07:55

ciphers to text is the same as the

play07:57

actual

play08:01

um

play08:02

message blocks uh

play08:06

uh that we created in this for Loop so

play08:13

um anyways after it's

play08:20

checks whether or not the length is less

play08:22

than eight or not it's going to add

play08:25

leading zeros if the length is less than

play08:27

eight

play08:29

um and may call the

play08:32

um

play08:34

encrypted P says eight digit longer it

play08:37

takes this decimal digit launch and at

play08:40

the end every encrypted block is going

play08:42

to be added with the return value and at

play08:43

the end we're going to return the return

play08:45

value

play08:46

the first part of this question says

play08:48

implement the decrypt function for this

play08:52

uh encryption so that it gets a

play08:54

ciphertext and it returns to playing

play08:57

text as output so basically we need to

play09:00

create a function like this the cipher

play09:02

is the first parameter the key is the

play09:04

second parameter this is an 8-bit number

play09:06

this can be any string what we're gonna

play09:09

do is we're gonna break the cipher into

play09:11

multiple eight bits pieces or epic

play09:14

blocks you can also break it into 32-bit

play09:17

pieces but because key is eight bits you

play09:20

don't need to break it into 32-bit

play09:22

pieces you can just break it in the same

play09:24

size as the key and uh basically

play09:27

exclusive or the key with each piece

play09:30

and this is going to give you the

play09:33

original value you may say how is it

play09:35

possible well I'm just going to briefly

play09:38

mention you know so you understand if a

play09:41

exclusive or B is equal to C

play09:46

imagine this is the plain text

play09:49

this is the key and this is uh the the

play09:55

cipher text

play10:01

then I can say

play10:04

be exclusive C is equal to a

play10:08

and you can prove that because because

play10:11

of the properties of exclusive

play10:13

so because of this

play10:17

um implication

play10:19

I can say when I want to do the

play10:21

decryption I do the same xor that I did

play10:24

before so for encryption we do xor for

play10:26

decryption we do X1 as well so that's

play10:28

how the that's the nature of exclusive

play10:30

and that's pretty much it after you do

play10:34

the exclusive or for each piece

play10:38

uh you exclusive the key with each piece

play10:41

you get the result of exclusive or and

play10:43

append the result of exclusive or with

play10:45

the return value return value is an

play10:47

empty string at the beginning but as you

play10:49

go forward you're going to build that

play10:51

return value gradually at the end you're

play10:54

going to return the whole return value

play10:56

return value basically begins with an

play10:58

empty string and a

play11:00

we gradually

play11:02

decrypt every uh block and appended to

play11:06

the return value at the end you get the

play11:08

whole decrypted plain text and that's

play11:12

the question that's the answer to the

play11:14

question 3.8 for 2. B uh the question

play11:17

says if we know that the following

play11:19

ciphertext is the result of encrypting a

play11:21

single meaningful English word with some

play11:24

key find the key and find the word in

play11:26

this case we have two unknown things one

play11:28

is the key one is the

play11:32

uh the word itself but there's nothing

play11:35

to be worried about and then the

play11:39

the ciphertext is given right so this is

play11:42

kind of an a security attack that you

play11:45

should always be aware and you know

play11:47

avoid uh the security attack basically

play11:50

does an exhaustive search on all the

play11:52

possible keys because the key is pretty

play11:55

short it's a very very you know weak key

play11:59

and therefore it's a very weak

play12:02

encryption algorithm

play12:04

um

play12:06

if you want to make it stronger you get

play12:08

a elongate the length of key and make it

play12:10

longer and longer but eight bit is

play12:12

pretty simple I'll show you how to break

play12:15

it uh basically you go every key

play12:18

possible so you go for key from 0 all

play12:21

the way to 255 for key in range 256 you

play12:24

get the following you say if the Crypt

play12:26

of ciphertext and key

play12:30

the decrypt is the same function we call

play12:33

there we created their uh I I didn't

play12:36

create it but I'm going to show you the

play12:37

creation of it but I just told you that

play12:39

what you need to do is these three steps

play12:41

should you follow it so if you've called

play12:43

this function if you created correctly

play12:45

create that function and then you call

play12:47

it on ciphertext and key for every key

play12:50

from 0 to 255 and see if the result is

play12:53

in their stripped Words which is the

play12:55

same list that I showed you this is a

play12:57

list of all English

play12:59

words

play13:01

you get a print the key followed by the

play13:04

decrypted

play13:05

ciphertext with the key and that's gonna

play13:09

generate the output if you make the

play13:11

decrypt function correctly this uh for

play13:16

Loop is going to answer the question

play13:17

trippy hope you understand

play13:20

um

play13:21

how to uh do the rest basically you need

play13:26

to implement the decrypt function and

play13:29

um yeah that's pretty much all you need

play13:32

to do

play13:33

for this question for the first question

play13:36

again as I said

play13:38

uh first part you need to read the

play13:40

dictionary and then you need to go over

play13:42

every word in the dictionary to see if

play13:45

the target matches any of the words

play13:49

hash value calculated by the following

play13:51

formula the formula was given as well

play13:54

all right

play13:55

have a good day

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
Python HashingDecryption AlgorithmCipher TextHash LibraryCode TutorialEnglish WordsSecurity AttackExclusive ORHash DigestProgramming Tips