python的des和3des加解密

Posted 一天不进步,就是退步!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python的des和3des加解密相关的知识,希望对你有一定的参考价值。

1.加密:

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.

实例:

Example
-------
from pyDes import *

data = "Please encrypt my data"
k = des("DESCRYPT", CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
# For Python3, you‘ll need to use bytes, i.e.:
#   data = b"Please encrypt my data"
#   k = des(b"DESCRYPT", CBC, b"\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
print "Encrypted: %r" % d
print "Decrypted: %r" % k.decrypt(d)
assert k.decrypt(d, padmode=PAD_PKCS5) == data

 

以上是关于python的des和3des加解密的主要内容,如果未能解决你的问题,请参考以下文章

3DES加解密示例

DES3DES 加解密;MAC算法

PHP 3DES 加解密(CBC模式,pkcs5padding填充)

3DES 加解密

java 与 c# 3des 加解密

java 与 c# 3des 加解密