python implements the des encryption method based on the pyDes library

  • 2020-05-30 20:31:08
  • OfStack

This article illustrates an example of how python implements des encryption based on the pyDes library. I will share it with you for your reference as follows:

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

If you need to use des encryption in python, you can do it directly using the pyDes library, which provides CBC and ECB Two types of encryption.

1. Install under Windows

After downloading pyDes - x. x. x. After zip and decompression, there is setup. py file, use the command setup.py --help You can view the use in detail.

You can use commands python setup.py install Command installation. You can also copy pyDes.py from the compressed package to the local python lib library and start using it directly

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 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

Here is an example I used to encrypt using CBC:


import base64
from pyDes import *
Des_Key = "BHC#@*UM" # Key
Des_IV = "\x22\x33\x35\x81\xBC\x38\x5A\xE7" #  Since the set 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) # turn base64 Code returned 

PS: about encryption and decryption interested friends can also refer to the website online tools:

MD5 online encryption tool:
http://tools.ofstack.com/password/CreateMD5Password

Thunderbolt, express, whirlwind URL encryption/decryption tools:
http://tools.ofstack.com/password/urlrethunder

Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt

MD5/hash/ SHA-1 / SHA-2 / SHA-256 / SHA-512 / SHA-3 / RIPEMD-160 encryption tools:
http://tools.ofstack.com/password/hash_md5_sha

Online sha1 sha224 / sha256 sha384 / sha512 encryption tools:
http://tools.ofstack.com/password/sha_encode

More about Python related topics: interested readers to view this site "Python coding skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article is helpful to you Python programming.


Related articles: