Question

I want encryption decryption of natural language model. I want to use natural language characters as key and text for my analysis work as below. How can i achieve that

 from Crypto.Cipher import AES
 import os

 BLOCK_SIZE = 32
 PADDING = '0'

 pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING

 EncodeAES = lambda c, s: c.encrypt(pad(s))

 DecodeAES = lambda c, e: c.decrypt(e.rstrip(PADDING))

 secret = u'ककककक..'

 obj = AES.new(secret)

 message = u'कककककककककककक'

 encoded = EncodeAES(obj, message)

 decoded = DecodeAES(obj, encoded)
 print 'Decrypted string: ', decoded.rstrip('0')
Was it helpful?

Solution

To use UTF-8 as your encoding, use unicode.encode('utf-8') to convert from a unicode string to a UTF-8 encoded string, and string.decode('utf-8') to convert from a UTF-8 encoded string to a unicode string (yes, they are different):

secret = u'ककककक.'.encode('utf-8')

message = u'कककककककककककक'.encode('utf-8')

decoded = DecodeAES(obj, encoded).decode('utf-8')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top