Question

Here I want to get the texts in this format : \x10\x11\x12\x13\x15\x14\x16\x17 , when displayed console how can i get that

   from pyDes import *
   import base64,string,random
   letters = [chr(i) for i in range(0,128)]
   key = '12345678'
   obj1= des(key, ECB)
   obj2 = des(key, ECB)
   text = '\x10\x11\x12\x13\x15\x14\x16\x17'
   print len(text)
   cipher_text = obj1.encrypt(text)
   decoded_text= obj2.decrypt(cipher_text)

   print; print 'plain text: ', text
   print; print 'Actual key: ', key 
   print; print 'Cipher text: ',cipher_text
Was it helpful?

Solution

Print the representation with repr() or the %r string formatting placeholder:

print '\nplain text: %r' % text
print '\nActual key: %r' % key 
print '\nCipher text: %r' % cipher_text

or you can explicitly encode the text to the string_escape encoding:

print '\nplain text:', text.encode('string_escape')
print '\nActual key:', key.encode('string_escape')
print '\nCipher text:', cipher_text.encode('string_escape')

Neither method will turn all bytes into \xhh escape sequences, only those outside of the printable ASCII character range.

To turn all characters into escape sequences, you'd have to use something like:

def tohex(string):
    return ''.join('\\x{:02x}'.format(ord(c)) for c in string)

and then

print '\nplain text:', tohex(text)
print '\nActual key:', tohex(key)
print '\nCipher text:', tohex(cipher_text)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top