自然语言模型的加密解密

时间:2014-04-30 09:18:23

标签: python encryption cryptography nlp aes

我想要自然语言模型的加密解密。我想使用自然语言字符作为我的分析工作的关键和文本,如下所示。我怎样才能实现

 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')

1 个答案:

答案 0 :(得分:0)

要使用UTF-8作为编码,请使用unicode.encode('utf-8')将unicode字符串转换为UTF-8编码字符串,并使用string.decode('utf-8')将UTF-8编码字符串转换为unicode字符串(是的,它们是不同的):

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

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

decoded = DecodeAES(obj, encoded).decode('utf-8')
相关问题