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


Posted in Python onApril 29, 2017

本文实例讲述了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编码返回
Python 相关文章推荐
Python 字符串操作方法大全
Mar 11 Python
python判断、获取一张图片主色调的2个实例
Apr 10 Python
Python多线程实例教程
Sep 06 Python
用Python的Django框架编写从Google Adsense中获得报表的应用
Apr 17 Python
总结用Pdb库调试Python的方式及常用的命令
Aug 18 Python
Python 实现数据库(SQL)更新脚本的生成方法
Jul 09 Python
python验证码识别教程之灰度处理、二值化、降噪与tesserocr识别
Jun 04 Python
浅谈python函数调用返回两个或多个变量的方法
Jan 23 Python
python爬虫开发之使用python爬虫库requests,urllib与今日头条搜索功能爬取搜索内容实例
Mar 10 Python
python多进程 主进程和子进程间共享和不共享全局变量实例
Apr 25 Python
Python Json数据文件操作原理解析
May 09 Python
Python3如何在服务器打印资产信息
Aug 27 Python
Python简单实现Base64编码和解码的方法
Apr 29 #Python
Python变量和字符串详解
Apr 29 #Python
python实现unicode转中文及转换默认编码的方法
Apr 29 #Python
Python 正则表达式实现计算器功能
Apr 29 #Python
python中类变量与成员变量的使用注意点总结
Apr 29 #Python
Python urls.py的三种配置写法实例详解
Apr 28 #Python
Python HTTP客户端自定义Cookie实现实例
Apr 28 #Python
You might like
PHP无限分类代码,支持数组格式化、直接输出菜单两种方式
2011/05/18 PHP
PHP SESSION的增加、删除、修改、查看操作
2015/03/20 PHP
php数据访问之查询关键字
2016/05/09 PHP
thinkPHP实现多字段模糊匹配查询的方法
2016/12/01 PHP
PHP封装的XML简单操作类完整实例
2017/11/13 PHP
PHP设计模式(四)原型模式Prototype实例详解【创建型】
2020/05/02 PHP
javascript document.referrer 用法
2009/04/30 Javascript
顶部缓冲下拉菜单导航特效的JS代码
2013/08/27 Javascript
JS获取月的最后一天与JS得到一个月份最大天数的实例代码
2013/12/16 Javascript
js实现ArrayList功能附实例代码
2014/10/29 Javascript
Python脚本后台运行的几种方式
2015/03/09 Javascript
使用AngularJS 应用访问 Android 手机的图片库
2015/03/24 Javascript
Bootstrap3学习笔记(三)之表格
2016/05/20 Javascript
JS实现放大、缩小及拖拽图片的方法【可兼容IE、火狐】
2016/08/23 Javascript
javascript self对象使用详解
2016/10/18 Javascript
模板视图和AngularJS之间冲突的解决方法
2016/11/22 Javascript
利用jQuery插件imgAreaSelect实现图片上传裁剪(放大缩小)
2016/12/02 Javascript
javaScript实现滚动条事件详解
2020/03/24 Javascript
浅析Node.js非对称加密方法
2018/01/29 Javascript
前端开发之便利店收银系统代码
2019/12/27 Javascript
[07:38]2014DOTA2国际邀请赛 Newbee顺利挺进胜者组赛后专访
2014/07/15 DOTA
python中的colorlog库使用详解
2019/07/05 Python
Pytorch实现GoogLeNet的方法
2019/08/18 Python
Tensorflow 1.0之后模型文件、权重数值的读取方式
2020/02/12 Python
python绘制玫瑰的实现代码
2020/03/02 Python
世界最大的海报和艺术印刷商店:AllPosters.com
2017/02/01 全球购物
皇家阿尔伯特英国官方商店:Royal Albert骨瓷
2019/03/25 全球购物
Liu Jo西班牙官网:意大利服装品牌
2019/09/11 全球购物
美国农场商店:Blain’s Farm & Fleet
2020/01/17 全球购物
Java里面StringBuilder和StringBuffer有什么区别
2016/06/06 面试题
文员个人的求职信范文
2013/09/26 职场文书
商业活动邀请函
2014/02/04 职场文书
股东协议书
2014/04/14 职场文书
雏鹰争章活动总结
2014/05/09 职场文书
活动倡议书范文
2014/05/13 职场文书
检察院院长群众路线教育实践活动个人整改措施
2014/10/04 职场文书