0

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')
Wooble
  • 87,717
  • 12
  • 108
  • 131
ceasif
  • 345
  • 2
  • 14
  • What is not working? Did you try encryption/decrpytption with another type of characters, like is typically shown in tutorials? Did it work? How does it differ from using natural language characters? – Jan Vlcinsky Apr 30 '14 at 09:22
  • Have you tried it ? Show output/traceback. – Nishant Nawarkhede Apr 30 '14 at 09:22
  • I tried many the problem is these libraries take ascii encoding and not utf-8 encoding standard so they dont accept all text as they are not ascii decodable – ceasif Apr 30 '14 at 09:47
  • if that's the case, just convert back and forth from utf-8 – Dan Getz Apr 30 '14 at 11:03

1 Answers1

0

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')
Dan Getz
  • 8,774
  • 6
  • 30
  • 64
  • I tried it but it doesnot support key size as the text chaaracter are 3 bytes we cant get 16 byte key size. – ceasif Apr 30 '14 at 17:57
  • Is there a requirement that you use UTF-8? Could you use UTF-16 instead? – Dan Getz May 01 '14 at 14:13
  • You know what, you should read [this answer](http://stackoverflow.com/questions/3451670/java-aes-and-using-my-own-key#3452620), although it's for Java. You shouldn't be converting your key string directly into the AES key. You should be hashing it first. – Dan Getz May 01 '14 at 14:25