python基于pyDes庫實(shí)現(xiàn)des加密的方法
本文實(shí)例講述了python基于pyDes庫實(shí)現(xiàn)des加密的方法。分享給大家供大家參考,具體如下:
下載及簡(jiǎn)介地址:https://twhiteman.netfirms.com/des.html
如需要在python中使用des加密,可以直接使用pyDes庫加密,該庫提供了CBC和ECB兩種加密方式。
1、Windows下安裝
下載后pyDes-x.x.x.zip并解壓后,里面有setup.py文件,使用命令 setup.py --help可查看詳細(xì)使用。
你可以使用命令python setup.py install命令安裝,也可以直接將壓縮包內(nèi)的pyDes.py拷貝到本地的python lib庫下直接開始使用
2、 使用
使用參數(shù)如下(拷貝自上述提供的地址):
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) #轉(zhuǎn)base64編碼返回
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
迅雷、快車、旋風(fēng)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
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python DES加密實(shí)現(xiàn)原理及實(shí)例解析
- Python實(shí)現(xiàn)常見的幾種加密算法(MD5,SHA-1,HMAC,DES/AES,RSA和ECC)
- python DES加密與解密及hex輸出和bs64格式輸出的實(shí)現(xiàn)代碼
- Python Des加密解密如何實(shí)現(xiàn)軟件注冊(cè)碼機(jī)器碼
- DES加密解密算法之python實(shí)現(xiàn)版(圖文并茂)
- Python3對(duì)稱加密算法AES、DES3實(shí)例詳解
- Python和Java進(jìn)行DES加密和解密的實(shí)例
- python實(shí)現(xiàn)DES加密解密方法實(shí)例詳解
- python實(shí)現(xiàn)的DES加密算法和3DES加密算法實(shí)例
- Python基于DES算法加密解密實(shí)例
- python如何實(shí)現(xiàn)DES加密
相關(guān)文章
Python的面向?qū)ο缶幊谭绞綄W(xué)習(xí)筆記
Python深度具備面向?qū)ο缶幊陶Z言所應(yīng)有的特性,這里我們以類和方法為主,來整理一下Python的面向?qū)ο缶幊谭绞綄W(xué)習(xí)筆記:2016-07-07
pandas探索你的數(shù)據(jù)實(shí)現(xiàn)可視化示例詳解
這篇文章主要為大家介紹了pandas探索你的數(shù)據(jù)實(shí)現(xiàn)可視化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
python中parser.add_argument()用法實(shí)例(命令行選項(xiàng)、參數(shù)和子命令解析器)
最近開始讀論文代碼了,遇到一個(gè)名字叫option的py文件,打開一看清一色的parser.add_argument(),看得是一臉懵逼,這篇文章主要給大家介紹了關(guān)于python中parser.add_argument()用法的相關(guān)資料,需要的朋友可以參考下2022-03-03
使用OpenCV circle函數(shù)圖像上畫圓的示例代碼
這篇文章主要介紹了使用OpenCV circle函數(shù)圖像上畫圓的示例代碼,本文內(nèi)容簡(jiǎn)短,給大家突出重點(diǎn)內(nèi)容,需要的朋友可以參考下2019-12-12
Python 數(shù)據(jù)結(jié)構(gòu)之堆棧實(shí)例代碼
這篇文章主要介紹了Python 數(shù)據(jù)結(jié)構(gòu)之堆棧實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Opencv實(shí)現(xiàn)計(jì)算兩條直線或線段角度方法詳解
這篇文章主要介紹了Opencv實(shí)現(xiàn)計(jì)算兩條直線或線段角度方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
完美解決torch.cuda.is_available()一直返回False的玄學(xué)方法
這篇文章主要介紹了完美解決torch.cuda.is_available()一直返回False的玄學(xué)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Python Paramiko創(chuàng)建文件目錄并上傳文件詳解
Paramiko是一個(gè)用于進(jìn)行SSH2會(huì)話的Python庫,它支持加密、認(rèn)證和文件傳輸?shù)裙δ?本文旨在詳細(xì)指導(dǎo)新手朋友如何使用Python的Paramiko庫來創(chuàng)建遠(yuǎn)程文件目錄并上傳文件,希望對(duì)大家有所幫助2024-10-10
python實(shí)現(xiàn)爬蟲統(tǒng)計(jì)學(xué)校BBS男女比例之多線程爬蟲(二)
這篇文章主要介紹了python實(shí)現(xiàn)爬蟲統(tǒng)計(jì)學(xué)校BBS男女比例之多線程爬蟲,感興趣的小伙伴們可以參考一下2015-12-12

