解密见这个——>利用pyDes库实现Des解密
先贴上代码:
from pyDes import CBC,des,PAD_PKCS5
import binascii
KEY="hellomcs"
str="123"
secret_key=KEY
iv=secret_key
d=des(secret_key,CBC,iv,pad=None,padmode=PAD_PKCS5)
k=d.encrypt(str,padmode=PAD_PKCS5)
print(binascii.b2a_hex(k))
CBC——加密模式(相对于EBC模式更安全)
KEY——密钥(必须8字节)
str——待加密字符串
pad——可选空
padmode——空或PAD_PKCS5模式
des的加密格式:des(key, [mode], [IV], [pad], [padmode])
三重加密:triple_des(key, [mode], [IV], [pad], [padmode])
methods:
可直接crypt数据或字符串
encrypt(str, [pad], [padmode])——加密
decrypt(str, [pad], [padmode])——解密
以下是pyDes说明文档:
Class initialization
--------------------
pyDes.des(key, [mode], [IV], [pad], [padmode])
pyDes.triple_des(key, [mode], [IV], [pad], [padmode])
key -> Bytes containing the encryption key. 8 bytes for DES, 16 or 24 bytes
for Triple DES
mode -> Optional argument for encryption type, can be either
pyDes.ECB (Electronic Code Book) or pyDes.CBC (Cypher Block Chaining)
IV -> Optional Initial Value bytes, must be supplied if using CBC mode.
Length must be 8 bytes.
pad -> Optional argument, set the pad character (PAD_NORMAL) to use during
all encrypt/decrpt operations done with this instance.
padmode -> Optional argument, set the padding mode (PAD_NORMAL or PAD_PKCS5)
to use during all encrypt/decrpt operations done with this instance.
I recommend to use PAD_PKCS5 padding, as then you never need to worry about any
padding issues, as the padding can be removed unambiguously upon decrypting
data that was encrypted using PAD_PKCS5 padmode.
Common methods
--------------
encrypt(data, [pad], [padmode])
decrypt(data, [pad], [padmode])
data -> Bytes to be encrypted/decrypted
pad -> Optional argument. Only when using padmode of PAD_NORMAL. For
encryption, adds this characters to the end of the data block when
data is not a multiple of 8 bytes. For decryption, will remove the
trailing characters that match this pad character from the last 8
bytes of the unencrypted data block.
padmode -> Optional argument, set the padding mode, must be one of PAD_NORMAL
or PAD_PKCS5). Defaults to PAD_NORMAL
Example
-------
以下为上文翻译(英语能力有限,不太准确请见谅):
类的初始化
--------------------
pyDes.des(key, [mode], [IV], [pad], [padmode])
pyDes.triple_des(key, [mode], [IV], [pad], [padmode])
key -> 含有加密密钥的字节。 DES为8字节,16或24字节用于三重DES
mode -> 加密类型的可选参数可以是pyDes.ECB(电子密码本)或pyDes.CBC(密文分组链接)
IV -> 使用CBC模式,必须提供可选的初始值字节。长度必须是8个字节。
pad -> 可选参数,完成的所有加密/解密操作期间使用的填充字符(PAD NORMAL)。
padmode -> 可选参数,设置填充模式(PAD_NORMAL或PAD_PKCS5)在使用此实例完成的所有encrypt / decrpt操作期间使用。
建议使用PAD_PKCS5填充,因为不需要担心填充出现问题,填充可以在解密时明确地删除使用PAD_PKCS5 padmode加密的数据。
常用方法
--------------
encrypt(data, [pad], [padmode])
decrypt(data, [pad], [padmode])
data -> 要加密/解密的字节
pad -> 可选参数。 仅在使用padmode为PAD_NORMAL时, 加密时,将此字符添加到数据块的末尾。 对于解密,将删除尾随与最后8个垫填充字符匹配的字符未加密数据块的字节数。
padmode -> 可选参数,设置填充模式,必须是PAD_NORMAL之一或PAD_PKCS5。 默认为PAD_NORMAL
例子
-------
|