python基于pyDes库实现des加密的方法

yipeiwu_com5年前Python基础

本文实例讲述了python基于pyDes库实现des加密的方法。分享给大家供大家参考,具体如下:

下载及简介地址:https://twhiteman.netfirms.com/des.html

如需要在python中使用des加密,可以直接使用pyDes库加密,该库提供了CBCECB两种加密方式。

1、Windows下安装

下载后pyDes-x.x.x.zip并解压后,里面有setup.py文件,使用命令 setup.py --help可查看详细使用。

你可以使用命令python setup.py install命令安装,也可以直接将压缩包内的pyDes.py拷贝到本地的python lib库下直接开始使用

2、 使用

使用参数如下(拷贝自上述提供的地址):

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:

from pyDes import *
# 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)
data = "Please encrypt my data"
k = des("DESCRYPT", CBC, "\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) == dat

以下是本人使用的例子,使用CBC加密的方式:

import base64
from pyDes import *
Des_Key = "BHC#@*UM" # Key
Des_IV = "\x22\x33\x35\x81\xBC\x38\x5A\xE7" # 自定IV向量
def DesEncrypt(str):
  k = des(Des_Key, CBC, Des_IV, pad=None, padmode=PAD_PKCS5)
  EncryptStr = k.encrypt(str)
  return base64.b64encode(EncryptStr) #转base64编码返回

PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:

MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password

迅雷、快车、旋风URL加密/解密工具:
http://tools.jb51.net/password/urlrethunder

在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

Django 路由层URLconf的实现

分组 分组的目的:让服务端获得url中的具体数据,通过分组,把需要的数据按函数传参的方式传递给服务器后台 1-无名分组 若要从URL 中捕获一个值,只需要在它周围放置一对圆括号 #...

python机器人运动范围问题的解答

机器人的运动范围Python实现: 问题:地上有个 m 行 n 列的方格。一个机器人从坐标(0,0)的格子开始移动,它每一次可以向左、右、上、下移动一格,但不能进入行坐标和列坐标的数位之...

Django实现全文检索的方法(支持中文)

PS: 我的检索是在文章模块下 forum/article 第一步:先安装需要的包: pip install django-haystack pip install whoosh p...

Python文件与文件夹常见基本操作总结

本文实例讲述了Python文件与文件夹常见基本操作。分享给大家供大家参考,具体如下: 1、判断文件(夹)是否存在。 os.path.exists(pathname) 2、判断路...

关于Django ForeignKey 反向查询中filter和_set的效率对比详解

前言 大家使用 Django 创建模型的时候一定会经常使用 ForeignKey 来创建两个表格之间多对一的外键关系,例如B中有一个 models.ForeignKey(A) 。而当我们...