0

How can I encrypt and decrypt hex text using 8 byte hex key using DES algorithm in python?

I have a hex text 0x3a3456abcd4ff5cd to encrypt with hex keys so how can I convert these values to equivalent strings so that I can use existing libraries to work with them.

Here's what I've tried, but it represents 8 byte key to equivalent 13 byte string:

from Crypto.Cipher import DES
text =(unichr(4).decode('utf-8')*8).encode('utf-8')

key1=(unichr(0x46)+unichr(0xb2)+unichr(0xc8)+unichr(0xb6)+unichr(0x28)+unichr(0x18)+unichr(0xf8)+unichr(0x84)).encode('utf-8')

key2=(unichr(0x4a)+unichr(0x5a)+unichr(0xa8)+unichr(0xd0)+unichr(0xba)+unichr(0x30)+unichr(0x58)+unichr(0x5a)).encode('utf-8')

des = DES.new(key1,DES.MODE_ECB)
cipher_text = des.encrypt(plain_text)
print 'the cipher text is ', cipher_text

des = DES.new(key2,DES.MODE_ECB)
print 'the decrypted text is: ', des.decrypt(cipher_text)
Cœur
  • 37,241
  • 25
  • 195
  • 267
ceasif
  • 345
  • 2
  • 14

1 Answers1

0

To use the two keys in your example, assign them like this:

key1 = '\x46\xb2\xc8\xb6\x28\x18\xf8\x84'

key2 = '\x4a\x5a\xa8\xd0\xba\x30\x58\x5a'

This will keep the keys at 8 bytes each, allowing you to use them in DES.new.

Rob Watts
  • 6,866
  • 3
  • 39
  • 58