Python implements des encryption based on the pyDes library, pydesdes

Source: Internet
Author: User

Python implements des encryption based on the pyDes library, pydesdes

This article describes how python implements des encryption based on the pyDes library. We will share this with you for your reference. The details are as follows:

Download and overview address: https://twhiteman.netfirms.com/des.html

If you need to use des encryption in python, you can directly use the pyDes library for encryption. This Library providesCBCAndECBTwo encryption methods.

1. Installation in Windows

Download pydes-x.x.x.zip and decompress it. The setup. py file is in it. Run the following command:setup.py --helpYou can view the usage details.

You can use the commandpython setup.py installCommand installation, you can also directly copy the pyDes. py in the compressed package to the local python lib library to start using

2. Use

The usage parameters are as follows (copied 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, 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
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.
Encryption, adds this characters to the end of the data block when
Data is not a multiple of 8 bytes. For decryption, will remove
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

Here is my example using CBC encryption:

Import base64from pyDes import * Des_Key = "BHC # @ * UM" # KeyDes_IV = "\ x22 \ x33 \ x35 \ x81 \ xBC \ x38 \ x5A \ xE7" # custom IV vector def desEncrypt (str): k = des (Des_Key, CBC, Des_IV, pad = None, padmode = PAD_PKCS5) EncryptStr = k. encrypt (str) return base64.b64encode (EncryptStr) # convert to base64 encoding and return

PS: if you are interested in encryption and decryption, refer to the online tools on this site:

MD5 online encryption tool:
Http://tools.jb51.net/password/CreateMD5Password

Thunder, express, and Tornado URL encryption/Decryption tools:
Http://tools.jb51.net/password/urlrethunder

Online hash/hash algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt

Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tools:
Http://tools.jb51.net/password/hash_md5_sha

Online sha1/shaloud/sha256/sha384/sha512 encryption tool:
Http://tools.jb51.net/password/sha_encode

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.