Pydes Library implements Python's DES encryption

Source: Internet
Author: User
Tags decrypt

Download and introduction Address: https://twhiteman.netfirms.com/des.html

If you need to use DES encryption in Python, you can use Pydes library encryption directly, which provides CBC and ECB two encryption methods.

1. Install under Windows

After download Pydes-x.x.x.zip and unzip, there is setup.py file, use the command setup.py--HELP can see the detailed use.

You can use the command Python setup.py install command, or you can directly copy the pydes.py in the compressed package to the local Python Lib library and start using it directly.

2. Use

Use the following parameters (copy from the address provided above):

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, bytes to 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 Supplie   D if using CBC mode. Length must be 8 bytes.pad-Optional argument, set the pad character (Pad_normal) to use during all Encrypt/dec RPT operations done with this instance.padmode-Optional argument, set the padding mode (pad_normal or PAD_PKCS5) t o use during all ENCRYPT/DECRPT operations do 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 Remov Ed unambiguously upon Decryptingdata that is encrypted using PAD_PKCS5 Padmode.common methods--------------ENcrypt (data, [pad], [Padmode]) decrypt (data, [pad], [padmode]) data, Bytes to be Encrypted/decryptedpad, Op tional argument. Only when using Padmode of Pad_normal. For encryption, adds this characters to the end of the data block if data is not a multiple of 8 bytes. For decryption, would remove the trailing characters that match this pad character from the last 8 bytes of the Unencry pted 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 ("descry PT ", 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
    

Here is an example of how to use CBC encryption:
ImportBase64 fromPydesImport*Des_key="Bhc#@*um" #KeyDes_iv ="\x22\x33\x35\x81\xbc\x38\x5a\xe7" #self-setting IV vectordefdesencrypt (str): K= des (Des_key, CBC, Des_iv, Pad=none, padmode=PAD_PKCS5) Encryptstr=k.encrypt (str)returnBase64.b64encode (ENCRYPTSTR)#turn base64 encoding back

Pydes Library implements Python's DES encryption

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.