Project 1 - Secret Key Encryption - Solution
Project 1 - Secret Key Encryption - Solution
Objective: The objective of this project is to get familiar with the concepts of the secret-key
(Symmetric-Key) encryption such as encryption algorithms, encryption modes, paddings, and
initial vector.
To apply this, we ran a python program (freq.py) over a given text file (ciphertext.txt) and we got below
results:
After analysing above counts and the ciphertext.txt file, we concluded some common substitutions of the
ciphers directly after seeing the top 20 trigrams and bigrams, such as ‘ytn’ is ‘THE’, ‘v’ is ‘A’ and so, ‘vu’ is
‘AN’, ‘vy’ is ‘AT’ and so on.
Hence, after few hits and trial, we were able to conclude the encryption key and converted it into a
plaintext, as shown below.
Here is the command used to convert ciphertext into plaintext using encryption key:
After that, we used AES-128 cipher with CBC mode first, using OPENSSL ENC command line tool, to
encrypt and decrypt the above text file.
AES-128-CBC
The encrypted file looked like below:
AES-128-CFB
Encrypted File:
Decrypted File:
After AES, we tried BF (Blowfish) with CBC mode first:
BF-CBC
Encrypted file:
Decrypted file:
DES-CBC
Encrypted file:
Decrypted file:
ARIA-128-CBC
Encrypted file:
Decrypted file:
To show the difference, we encrypted a given picture using AES-128 cipher with CBC and ECB mode, as
you can see in the below snapshot:
And since it was a ‘.bmp’ picture, we had to concatenate original picture header with encrypted picture
body.
I again tried the same cipher with ECB and CBC mode on a custom image and got the same result, as you
can see the below snapshot:
Commands to encrypt the image with AES cipher using CBC and ECB mode:
Below is the combined snapshot:
i.) Original picture (on top left),
ii.) Encrypted picture with AES using ECB (on top right),
iii.) Encrypted picture with AES using CBC (bottom one).
Our Observations:
i.) ECB mode leaks data about the underlying message being encrypted. The reason this happens
is because it produces identical ciphertext blocks after encrypting identical plaintext blocks.
ii.) CBC mode eliminates the ECB issue. The plaintext of a block is combined with the ciphertext
of the previous block via XOR operation and the result is encrypted.
Task 4: Padding:
We encrypted an 8 byte “test” file using these modes ECB, CBC, CFB, OFB:
ECB and CBC mode add padding to the plaintext because the block size on which encryption will be done
should be 16 bytes (128 bits). If it is shy of this size, then padding is done. CFB and OCB do not need
padding because when you encrypt using Cipher-Feedback (CFB) or Output-Feedback (OFB), then the
ciphertext is of the same size as the plaintext and this is Why padding is not required in these modes.
iii.) The size of the encrypted files is 16, 16, and 32 bytes for f1, f2 and f3 respectively, which in
bits is 128 bits, 128 bits and 2 blocks of 128 bits for f3. The size of a block in CBC mode is 128
bits.
iv.) Decrypting the above encrypting files with “-nopad” command.
v.) The paddings which were added during the encryption process in all three files are shown
below:
It depends on several factors such as the encryption mode used, the percentage of corruption in the file,
and many more.
• If a file has been encrypted using ECB mode, each block of data in the file is encrypted
independently of the other blocks. This means that if some blocks of the file are corrupted, it may
still be possible to recover the remaining uncorrupted blocks.
• If a file has been encrypted using CBC mode, each block of data is XORed with the previous block
before encryption, which introduces some randomness into the encryption process. This means
that if some blocks of the file are corrupted, it may still be possible to recover the remaining
uncorrupted blocks, but only if the previous blocks are also uncorrupted. If a corrupted block is
encountered, it will typically cause the decryption process to fail for the remaining blocks.
• If a file has been encrypted using CFB mode, each block of data is encrypted using the previous
block as input to a feedback function, which generates a pseudo-random stream of data. This
stream is then XORed with the plaintext to produce the ciphertext. If some blocks of the file are
corrupted, it may still be possible to recover the remaining uncorrupted blocks, but only if the
feedback function can be reconstructed based on the available information. If a corrupted block is
encountered, it will typically cause the decryption process to fail for the remaining blocks.
• If a file has been encrypted using OFB mode, each block of data is encrypted using a pseudo-
random stream generated from an IV (initialization vector) and a secret key. This stream is then
XORed with the plaintext to produce the ciphertext. If some blocks of the file are corrupted, it
may still be possible to recover the remaining uncorrupted blocks, but only if the IV and secret
key are available, and the feedback function can be reconstructed based on the available
information. If a corrupted block is encountered, it will typically cause the decryption process to
fail for the remaining blocks.
To accomplish Task-5, I created a dummy text file (task5_file.txt) of 1173 bytes and encrypted it with AES-
128 cipher with CBC, CFB, ECB and OFB modes, as you can see in the below snapshots:
Now, I have corrupted the 55th byte of all the ciphertext files using the hex editor. After that, I decrypted
all the different corrupted ciphertexts one by one, shown in the below snapshots:
ECB:
Result:
Observation:
Surprisingly, we found our assumption partial true as it was able to recover few portion of the plaintext
but there were many uncorrupted blocks and since it works independently, it should have recovered
more portion of the plaintext as per our assumption.
CBC:
Result:
Observation:
We found our assumption true as it was able to recover some portion of the plaintext but not all as it
works sequentially and depends on the previous ciphertext block. So, once it encountered the corrupted
ciphertext, the decryption process failed.
CFB:
Result:
Observation:
We found our assumption true as it is similar to CBC mode and it was able to recover a portion of the
plaintext until it encountered the corrupted ciphertexts.
OFB:
Result:
Observation:
We found our assumption true as it was able to recover some portion of the plaintext and since this mode
is similar to CFB, it didn’t able to recover the remaining portion of plaintext after it encountered
corrupted ciphertext.
Conclusion:
After successfully doing each task, I learned how to use different secret key encryptions, their algorithms,
used different ciphers to encrypt and decrypt texts and pictures, paddings, and error propagation. I also
learned about pros and cons of different modes.