Question

As part of a proof of concept I was given a bas64 encoded / Triple DES (MODE_ECB) encrypted string from a third party. I wrote a quick test knowing that the decrypted value should be "testdata", but it doesn't seem to work.

I have tried it with both pycrypto and pyDES, with the same results. Am I doing something wrong. Is decode('hex') the correct approach for the key?

from Crypto.Cipher import DES3
import base64

class akamaiServicesTest(TestCase):

def test_cipherDecode3DES(self):
    key = "D41D8CD98F00B204E9800998ECF8427ECF34260089DE00EF".decode('hex')
    encryptedString = base64.b64decode("QnRWdXFPeE8rRmJGOGVSWkhOMzFiN3l2Y01scU1QdXU=")
    self.assertEqual(encryptedString, "BtVuqOxO+FbF8eRZHN31b7yvcMlqMPuu")

    cipher = DES3.new(key, DES3.MODE_ECB)
    decryptedString = cipher.decrypt(encryptedString)
    self.assertEqual(decryptedString, "testdata")
Was it helpful?

Solution

The key is correct, but the ciphertext seems to be Base64-encoded twice. Decode the "BtVuq..." string as well to get the correct ciphertext.

Decrypting the ciphertext with 3DES in ECB mode with PKCS#7 padding produces 8 bytes of binary data and the 8-byte ASCII string "testdata". Is this what you are looking for?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top