1

I'm trying to encrypt a plain text using DES algorithm implemented in PyCrypto. However, when I print the encrypted text and then decrypt it using the generated encrypted text an additional b seems to get added every time. Is this an error or just something else that I'm being ignorant about?

Here's the code sample:

des = DES.new('01234567', DES.MODE_ECB)
text = input('Enter plain text: ')
cipher_text = des.encrypt(text)
print('Cipher Text:' + str(cipher_text))
decipher_text = des.decrypt(ciphertext=cipher_text)
print('Deciphered text is: ' + str(decipher_text))

And the resultant output:

Enter plain text: abcdefgh
Cipher Text:b'\xec\xc2\x9e\xd9] a\xd0'
Deciphered text is: b'abcdefgh'
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Vivek Giri
  • 35
  • 7
  • 1
    DES should not be used for new work, it is not secure and has been superseded by AES. Do not use ECB mode, it is insecure, see [ECB mode](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29), scroll down to the Penguin. Instead use CBC mode with a random IV, just prefix the encrypted data with the IV for use in decryption, it does not need to be secret. – zaph Apr 15 '17 at 12:01
  • No, I'm just trying to mess around it's not used anywhere. – Vivek Giri Apr 15 '17 at 12:03
  • Why not "just mess around" with a current secure encryption algorithm, it is no harder. Since you are just messing around it is a good time to learn about CBC mode and padding. – zaph Apr 15 '17 at 12:05
  • Thanks. Definitely, could you direct me to some resources that I could refer? – Vivek Giri Apr 15 '17 at 12:11
  • [Package Crypto :: Package Cipher :: Module AES](https://www.dlitz.net/software/pycrypto/api/current/) – zaph Apr 15 '17 at 12:21
  • Thanks a lot mate! – Vivek Giri Apr 15 '17 at 12:23
  • **Never use [ECB mode](http://crypto.stackexchange.com/q/14487/13022)**. It's deterministic and therefore not semantically secure. You should at the very least use a randomized mode like [CBC](http://crypto.stackexchange.com/q/22260/13022) or [CTR](http://crypto.stackexchange.com/a/2378/13022). It is better to authenticate your ciphertexts so that attacks like a [padding oracle attack](http://crypto.stackexchange.com/q/18185/13022) are not possible. This can be done with authenticated modes like GCM or EAX, or with an [encrypt-then-MAC](http://crypto.stackexchange.com/q/202/13022) scheme. – Artjom B. Apr 15 '17 at 16:09

1 Answers1

4

The b indictes this is a binary string.

CodeCupboard
  • 1,507
  • 3
  • 17
  • 26