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使用urllib2获取网络资源实例讲解
Dec 02 Python
python实现获取客户机上指定文件并传输到服务器的方法
Mar 16 Python
Python的Django框架中的Context使用
Jul 15 Python
在Mac OS系统上安装Python的Pillow库的教程
Nov 20 Python
python魔法方法-属性访问控制详解
Jul 25 Python
Python基于pygame模块播放MP3的方法示例
Sep 30 Python
Python中turtle库的使用实例
Sep 09 Python
Python GUI自动化实现绕过验证码登录
Jan 10 Python
python GUI库图形界面开发之PyQt5信号与槽的高级使用技巧(自定义信号与槽)详解与实例
Mar 06 Python
tensorflow2.0的函数签名与图结构(推荐)
Apr 28 Python
Pycharm自带Git实现版本管理的方法步骤
Sep 18 Python
Python操作Excel的学习笔记
Feb 18 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/06/09 PHP
比较简单实用的PHP无限分类源码分享(思路不错)
2011/10/13 PHP
php去除换行(回车换行)的三种方法
2014/03/26 PHP
浅谈PHP正则表达式中修饰符/i, /is, /s, /isU
2014/10/21 PHP
PHP使用xpath解析XML的方法详解
2017/05/20 PHP
文本框倒叙输入让输入框的焦点始终在最开始的位置
2014/09/01 Javascript
Javascript中数组sort和reverse用法分析
2014/12/30 Javascript
JS中frameset框架弹出层实例代码
2016/04/01 Javascript
jquery实现焦点轮播效果
2017/02/23 Javascript
js控制文本框禁止输入特殊字符详解
2017/04/07 Javascript
seajs和requirejs模块化简单案例分析
2019/08/26 Javascript
从零开始在vue-cli4配置自适应vw布局的实现
2020/06/08 Javascript
jQuery实现简单日历效果
2020/07/05 jQuery
Openlayers3实现车辆轨迹回放功能
2020/09/29 Javascript
[02:40]DOTA2英雄基础教程 炼金术士
2013/12/23 DOTA
Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法
2018/01/11 Python
在unittest中使用 logging 模块记录测试数据的方法
2018/11/30 Python
Python GUI库PyQt5样式QSS子控件介绍
2020/02/25 Python
Python文本文件的合并操作方法代码实例
2020/03/31 Python
如何在windows下安装Pycham2020软件(方法步骤详解)
2020/05/03 Python
用pushplus+python监控亚马逊到货动态推送微信
2021/01/29 Python
纯CSS3实现带动画效果导航菜单无需js
2013/09/27 HTML / CSS
波兰香水和化妆品购物网站:Notino.pl
2017/11/07 全球购物
定义一结构体变量,用其表示点坐标,并输入两点坐标,求两点之间的距离
2015/08/17 面试题
介绍一下EJB的分类及其各自的功能及应用
2016/08/23 面试题
汽车维修专业毕业生的求职信分享
2013/12/04 职场文书
中式婚礼主持词
2014/03/13 职场文书
学校献爱心活动总结
2014/07/08 职场文书
2015年银行大堂经理工作总结
2015/04/24 职场文书
2015年保育员个人工作总结
2015/05/13 职场文书
机关干部作风整顿心得体会
2016/01/22 职场文书
职场新人刚入职工作总结该怎么写?
2019/05/15 职场文书
2019大学生实习报告
2019/06/21 职场文书
如何让2019年上半年的工作总结更出色!
2019/07/01 职场文书
解决Go gorm踩过的坑
2021/04/30 Golang
pytorch分类模型绘制混淆矩阵以及可视化详解
2022/04/07 Python