python加密文件时正确的padding方法

来源:互联网 发布:ubuntu 17.04 openjdk 编辑:程序博客网 时间:2024/05/01 22:20
之前写过的一段python程序代码用来对tar 文件加密,然后发送到客户端,客户端是运行openWRT的路由器,在路由器端用openssl进行解密,先附上代码。
用来加密文件的python code:
# Encrypte file
defaes_encrypt_file(in_filename, out_filename, key, iv):
    block_size = AES.block_size
    pad =lambda s: s + (block_size - len(s) % block_size) \
                        * chr(block_size - len(s) % block_size)
    cipher = aes_build_cipher(key, iv)
    with open(in_filename, 'rb')as infile:
        with open(out_filename, 'wb')as outfile:
            whileTrue:
                buf = infile.read(1024)
                if len(buf) == 0:
                    break
                elif len(buf) % block_size != 0:
                    buf = pad(buf)
                outfile.write(cipher.encrypt(buf))
用来揭秘的openssl 命令:
openssl aes-256-cbc -d -nosalt -K $sum256 -iv $iv -in ${PACKAGE} | tar xzf -
需要说明的是每隔一小时就要执行一遍程序,加密tar文件然后发送到客户端解密解压。而且每次tar 文件都会发生变化,但基本上大小都在200KB左右。
一开始做了几次测试之后,程序运行的都很愉快、很正常。于是写了个脚本让程序每隔2分钟运行一次,跑了一晚上,第二天打开log立马傻眼,居然出现错误,平均每10次会出现一次,错误信息如下:
bad decrypt
2011837512:error:06065064:lib(6):func(101):reason(100):NA:0:
tar: short read

很明显解密出现问题,开始解决问题。首先排除了key和iv出错的情况,将问题定位到了在网上找的padding的这段代码,于是查找资料。
在google了一通错误码之后毫无帮助,于是直接查找openssl是如何处理padding的。
google之后找到这里:http://https://www.openssl.org/docs/apps/enc.html 在这里面讲到了openssl关于padding的参数nopad,但也只有这一个参数。同时还讲到了这样一句话:All the block ciphers normally use PKCS#5 padding also known as standard block padding: this allows a rudimentary integrity or password check to be performed. However since the chance of random data passing the test is better than 1 in 256 it isn't a very good test.意思就是说他在处理padding时通常是使用PKCS#5的方式,这个就很有用了。于是再去查看
PKCS#5的标准,找到了这个http://tweetyf.org/2012/04/the_difference_between_pkcs7_pkcs5.html 里面介绍了PKCS#5和PKCS#7的区别,也讲了原理。大概讲一下就是对于block_size是16的情况无论如何都有做填充,即使文件大小正好是16的倍数那也要在最后填充上16个16。看到这里就可以发现代码中是有问题的,因为如果文件大小正好是16的倍数就不会去padding,所以修改之后如下:
# Encrypte file
defaes_encrypt_file(in_filename, out_filename, key, iv):
    block_size = AES.block_size
    pad =lambda s: s + (block_size - len(s) % block_size) \
                        * chr(block_size - len(s) % block_size)
    cipher = aes_build_cipher(key, iv)
    with open(in_filename, 'rb')as infile:
        with open(out_filename, 'wb')as outfile:
            buf = infile.read()
            buf = pad(buf)
            outfile.write(cipher.encrypt(buf))
这样就可以保证无论文件多大最后都要padding了,这样openssl就可以成功揭秘了。

在解决之前将问题发到了stackoverflow上,可以看一下:http://stackoverflow.com/questions/24531307/how-does-openssl-command-handle-pkcs7-padding-added-with-python
0 0
原创粉丝点击