国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python基于pyDes庫實現des加密的方法

2019-11-25 16:12:57
字體:
來源:轉載
供稿:網友

本文實例講述了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" % dprint "Decrypted: %r" % k.decrypt(d)assert k.decrypt(d, padmode=PAD_PKCS5) == dat

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

import base64from pyDes import *Des_Key = "BHC#@*UM" # KeyDes_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.VeVB.COm/password/CreateMD5Password

迅雷、快車、旋風URL加密/解密工具:
http://tools.VeVB.COm/password/urlrethunder

在線散列/哈希算法加密工具:
http://tools.VeVB.COm/password/hash_encrypt

在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.VeVB.COm/password/hash_md5_sha

在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.VeVB.COm/password/sha_encode

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 保亭| 垦利县| 保康县| 镇江市| 米脂县| 小金县| 广灵县| 岳阳市| 北辰区| 文登市| 庄浪县| 吉林省| 房产| 西昌市| 陆丰市| 奉化市| 柳林县| 桑日县| 扬中市| 建宁县| 伽师县| 新田县| 呼图壁县| 恩平市| 会泽县| 河津市| 神池县| 秦安县| 剑川县| 西贡区| 策勒县| 青铜峡市| 杂多县| 犍为县| 海安县| 蓬溪县| 丹江口市| 凤庆县| 宝丰县| 子长县| 肇源县|