使用与python2和python3相同的代码编码+ encrypt + pad的麻烦 [英] Trouble with encode + encrypt + pad using same code for python2 and python3

查看:1453
本文介绍了使用与python2和python3相同的代码编码+ encrypt + pad的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我了解以下是不适合在生产环境中给予安全性的。它简单地意味着比在我的系统上存储的敏感数据上使用XOR或rot13更好一点。



我把以下代码放在一起,让我为这些敏感值使用AES加密。 AES需要16字节的块;所以我需要填充。而且我想把数据保存在文本文件中;所以我添加了base64编码:

  from __future__ import print_function 
from Crypto.Cipher import AES
import base64

crypto = AES.new('This is a key123',AES.MODE_CBC,'This is a IV456')

BS = 16
pad = lambda s:s +(BS - len(s)%BS)* chr(BS - len(s)%BS)
unpad = lambda s:s [0:-ord(s [-1])]


def scramble(data):
return base64.b64encode(crypto.encrypt(pad(data)))


def unscramble (数据):
返回unpad(crypto.decrypt(base64.b64decode(data)))

incoming =abc
print(in:{}格式(传入))
加扰=加扰(传入)
打印(加扰:{}格式(加密))
和back = unscramble(加扰)
打印:{}。format(andback))

对于python2;打印:


in:abc



加扰:asEkqlUDiqlUpW1lw09UlQ ==

颠倒:


对于python3;我遇到

  unpad = lambda s:s [0:-ord(s [-1])] 
TypeError:ord()预期的字符串长度为1,但是int found

两个问题:




  • 我的反向路径与python2有什么问题,为什么不打印abc?

  • 我明白错误信息使用python3;但是我想知道:以python2和python3的方式解决这个问题的正确的,规范的方法是什么?


解决方案

您的代码的一个问题是您正在使用相同的密码对象进行加密和解密。这将不起作用,因为密码对象是有状态的: PyCrypto文档



您可以创建另一个解密对象,如:
crypto2 = AES.new('This is a key123',AES .MODE_CBC,'这是一个IV456'),然后使用此对象进行解密。


Disclaimer: I understand that the following is not suited to give "security" in a production environment. It is simply meant as "a little bit better" than using XOR or rot13 on sensitive data that is stored on my system.

I put together the following code to allow me to use AES encryption for those sensitive values. AES requires 16 byte chunks; so I need padding. And I want to save that data in text files; so I added base64 encoding:

from __future__ import print_function
from Crypto.Cipher import AES
import base64

crypto = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')

BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s: s[0:-ord(s[-1])]


def scramble(data):
    return base64.b64encode(crypto.encrypt(pad(data)))


def unscramble(data):
    return unpad(crypto.decrypt(base64.b64decode(data)))

incoming = "abc"
print("in: {}".format(incoming))
scrambled = scramble(incoming)
print("scrambled: {}".format(scrambled))
andback= unscramble(scrambled)
print("reversed : {}".format(andback))

For python2; that prints:

in: abc

scrambled: asEkqlUDiqlUpW1lw09UlQ==

reversed :

For python3; I run into

unpad = lambda s: s[0:-ord(s[-1])]
TypeError: ord() expected string of length 1, but int found

Two questions:

  • What is wrong with my "reverse" path with python2, why doesn't it print "abc"?
  • I understand that error message using python3; but I am wondering: what is the correct, canonical way to solve this problem in a way that works for both python2 and python3?

解决方案

One problem with your code is that you are using the same cipher object for both encryption and decryption. This won't work, as the cipher objects are stateful:PyCrypto Documentation

You can create another object for decrypting, as in: crypto2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456'), and then use this object to decrypt.

这篇关于使用与python2和python3相同的代码编码+ encrypt + pad的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆