pyDes庫 實現python的des加密

來源:互聯網
上載者:User

標籤:

下載及簡介地址:https://twhiteman.netfirms.com/des.html

如需要在python中使用des加密,可以直接使用pyDes庫加密,該庫提供了CBC和ECB兩種加密方式。

 

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 DESmode    -> 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 anypadding issues, as the padding can be removed unambiguously upon decryptingdata that was encrypted using PAD_PKCS5 padmode.Common methods--------------encrypt(data, [pad], [padmode])decrypt(data, [pad], [padmode])data    -> Bytes to be encrypted/decryptedpad     -> 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編碼返回

pyDes庫 實現python的des加密

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.