-1

For some reason I need to use 3DES, I know it's weak.

This is the original unencrypted text file:

# cat test.txt
This is the test text file. Used for testing the encryption and decryption differences.

In order to perform the test, I encrypted a text file using certain passphrase and salt:

# openssl enc -des3 -pass pass:cfe4ec9fcec928d51d243855a094b05ebb7bc870 -S 3AA6A64F87764632 -in test.txt -out test_encrypted
# cat test_encrypted
Salted__:▒▒O▒vF2^Y▒▒▒▒▒XDŽ▒▒%0-▒1▒▒!▒▒Tn▒▒▒▒9▒▒%w,56,m^δFߚ▒Ű▒>▒-▒J▒zdl▒▒-Y▒r▒(G▒▒▒_A▒▒oD

I cut the salted-head (16 bytes) as encrypted_content, then I use the following method to decrypt it:

...
password = 'cfe4ec9fcec928d51d243855a094b05ebb7bc870'.encode()
salt = bytes.fromhex('3AA6A64F87764632')
d1 = hashlib.md5(password+salt)
d2 = hashlib.md5(d1.digest()+password+salt)
keymatter = d1.digest() + d2.digest()
key = keymatter[:24].hex().upper()
iv = keymatter[24:32].hex().upper()

import binascii
import Crypto.Cipher
from Crypto.Cipher import DES3
from Crypto.Util.Padding import pad, unpad
k = Crypto.Cipher.DES3.new(key=binascii.unhexlify(key), mode=2, iv=binascii.unhexlify(iv))
decrypted = k.decrypt(encrypted_content)
print ('decrypted:', decrypted)

But I got this result:

decrypted: b'\xae\xbb\xef&\xe6|\xfd\x16\xb0\xe23\xad{~\xd2&the test text file. Used for testing the encryption and decryption differences.\x01'

Seems like the head and tail are not handled correctly? I'm thinking if it's padding issue but I can't use padding function in Crypto.Util.Padding, it will always call out error.


Update: The above code will work then "encrypted_content" is correctly loaded. I don't want to delete this post since I believe it might help others who has similar needs when decrypting 3DES contents.

  • Sorry. You are right, the "encrypted_content" was not correct and it resulted the wrong output. After correction, it worked now. – DanielLien Apr 08 '21 at 09:03

1 Answers1

0

Sorry, I made a mistake in calculating the file size. After adjustment, it works like a charm.

The padding also work, I used:

decrypted_content = unpad(decrypted_content, 8)