【问题标题】:Pycrypto AES encrypted data has strange symbols (python)Pycrypto AES加密数据有奇怪的符号(python)
【发布时间】:2013-12-16 05:41:28
【问题描述】:

这里有一个新手程序员尝试使用 pycrypto AES 加密数据字符串。经过仔细分析并在 python shell 中尝试后,我编写了 crypt.py 并尝试运行它。当我运行文件时,在 shell 中完美运行的 AES 函数开始发出奇怪的符号作为输出。有人可以阐明我在哪里做错了。

这是我的 crypt.py:

#!/usr/bin/env python

from Crypto.Cipher import AES
from Crypto import Random
import os
import md5
import sys
import binascii
import base64

password = "fault_tolerant_system"
md5obj = md5.new()
md5obj.update(password)
key = md5obj.digest()

def pad(data, width):
  needed = width - ((len(data) + 1) % width)
  return data + ('\x00' * needed) + chr(needed + 1)

def unpad(data):
  padding = ord(data[-1])
  return data[:-padding]

def main() :
  val = "Hi I am ked"
  crcval = binascii.crc32(val) & 0xffffffff
  lencrc = len(str(crcval))
  finalval = str(lencrc) + "|" + val + str(crcval)
  print 'finalval after crc appending: ', finalval
  padded_finalval = pad(finalval, 16)
  print "padded final val : ", padded_finalval

  #encrypt using AES
  print Random.new().read(16)
  ivp = binascii.hexlify(Random.new().read(16))
  aes_encryptor = AES.new(key, AES.MODE_CBC, ivp[:16])
  cipher2 = aes_encryptor.encrypt(padded_finalval)
  print "ivp is:", ivp
  print "cipher2: ", cipher2
  cipher = ivp[:16] + aes_encryptor.encrypt(padded_finalval)
  print "cipher: ", (cipher)
  print "\n\n******"

  res_enc_iv_aes = cipher
  ivg = res_enc_iv_aes[:16]
  print ivg
  aes_decryptor = AES.new(key, AES.MODE_CBC, ivg)
  print binascii.hexlify(aes_decryptor.decrypt(res_enc_iv_aes[16:]))
  resvalue = unpad(aes_decryptor.decrypt(res_enc_iv_aes[16:]))

  print "\ndecrpted: ", resvalue      
  crclen = int(resvalue.split("|", 1)[0])
  print "crclen: ", crclen
  crc_cksum = resvalue[-crclen:]
  print crc_cksum
  recv_data = finalval[len(str(crclen))+1 : -crclen]
  print "recv_data: ", recv_data
#compute checksum of recv_data
  crc_recv_data = binascii.crc32(recv_data) & 0xffffffff
  print crc_recv_data      
  if str(crc_recv_data) == crc_cksum:
        #no error in data
    print "recv_data: ", recv_data
  else:
    print "error :("
    print "recdata:", recv_data

if __name__ == "__main__":
  main() 

输出是: crc附加后的finalval:10|Hi1293356558

填充的最终值:10|Hi1293356558

A0>�̔��_Y/�u

ivp 是:95f9a5238701703b8a58933bb48e9015

mqMher2: ����]6� �75����+�2=��P�H�� T...u'5/...Q...9a5238701703b5...U...T...fQ...


95f9a5238701703b

edd08277e52fc138369bb557e24d88d000000000000000000000000000000011

解密:C���)�{��I3,p

Traceback (most recent call last):
  File "cry1.py.py", line 70, in <module>
    main()
  File "cry1.py.py", line 53, in main
  crclen = int(resvalue.split("|", 1)[0])

ValueError: invalid literal for int() with base 10: 'C\xe0\xe9\x1a'

【问题讨论】:

  • 按预期工作。获得加密文本后,您需要对其进行 ASCII 保护,或者将其编码为保留二进制数据的格式。
  • 检查这个问题和答案更具体:stackoverflow.com/questions/16220016/…
  • 我尝试了 base64 编码,但解密后的数据没有按预期输出。如果这不是处理此问题的正确方法,您能否建议使用它将起作用的方法。另外,知道为什么我会收到无效的文字错误吗?谢谢@rdodev
  • unicode_key = key.decode('iso-8859-1') bytes_key = unicode_key.encode('iso-8859-1')
  • FWIW,那些奇怪的符号是Unicode replacement character,“用于替换未知或无法表示的字符”。

标签: python encryption aes pycrypto undefined-symbol


【解决方案1】:

感谢凯尔。这解释并回答了你想知道的关于 AES pycrypto 的一切。 http://kyleisom.net/downloads/crypto_intro.pdf

【讨论】:

  • 链接断开。伤心:(
猜你喜欢
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
相关资源
最近更新 更多