Cryptography With Python 简明教程
Symmetric and Asymmetric Cryptography
在本章中,让我们详细讨论对称和非对称密码术。
In this chapter, let us discuss in detail about symmetric and asymmetric cryptography.
Symmetric Cryptography
在此类型中,加密和解密过程使用相同的密钥。它也被称为 secret key cryptography 。对称密码术的主要功能如下:
In this type, the encryption and decryption process uses the same key. It is also called as secret key cryptography. The main features of symmetric cryptography are as follows −
-
It is simpler and faster.
-
The two parties exchange the key in a secure way.
Data Encryption Standard (DES)
最流行的对称密钥算法是数据加密标准 (DES),且 Python 包含一个软件包,该软件包包括 DES 算法背后的逻辑。
The most popular symmetric key algorithm is Data Encryption Standard (DES) and Python includes a package which includes the logic behind DES algorithm.
Installation
Python 中安装 DES 包 pyDES 的命令为:
The command for installation of DES package pyDES in Python is −
pip install pyDES

DES 算法的简单程序实现如下:
Simple program implementation of DES algorithm is as follows −
import pyDes
data = "DES Algorithm Implementation"
k = pyDes.des("DESCRYPT", pyDes.CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
d = k.encrypt(data)
print "Encrypted: %r" % d
print "Decrypted: %r" % k.decrypt(d)
assert k.decrypt(d) == data
它调用变量 padmode ,它获取所有 DES 算法实现所需的包,并以特定方式执行加密和解密。
It calls for the variable padmode which fetches all the packages as per DES algorithm implementation and follows encryption and decryption in a specified manner.
Asymmetric Cryptography
它也被称为 public key cryptography. 。它的工作方式与对称加密相反。这意味着它需要两个密钥:一个用于加密,另一个用于解密。公钥用于加密,而私钥用于解密。
It is also called as public key cryptography. It works in the reverse way of symmetric cryptography. This implies that it requires two keys: one for encryption and other for decryption. The public key is used for encrypting and the private key is used for decrypting.
Drawback
-
Due to its key length, it contributes lower encryption speed.
-
Key management is crucial.
以下 Python 程序代码演示了如何使用 RSA 算法和实现执行非对称加密:
The following program code in Python illustrates the working of asymmetric cryptography using RSA algorithm and its implementation −
from Crypto import Random
from Crypto.PublicKey import RSA
import base64
def generate_keys():
# key length must be a multiple of 256 and >= 1024
modulus_length = 256*4
privatekey = RSA.generate(modulus_length, Random.new().read)
publickey = privatekey.publickey()
return privatekey, publickey
def encrypt_message(a_message , publickey):
encrypted_msg = publickey.encrypt(a_message, 32)[0]
encoded_encrypted_msg = base64.b64encode(encrypted_msg)
return encoded_encrypted_msg
def decrypt_message(encoded_encrypted_msg, privatekey):
decoded_encrypted_msg = base64.b64decode(encoded_encrypted_msg)
decoded_decrypted_msg = privatekey.decrypt(decoded_encrypted_msg)
return decoded_decrypted_msg
a_message = "This is the illustration of RSA algorithm of asymmetric cryptography"
privatekey , publickey = generate_keys()
encrypted_msg = encrypt_message(a_message , publickey)
decrypted_msg = decrypt_message(encrypted_msg, privatekey)
print "%s - (%d)" % (privatekey.exportKey() , len(privatekey.exportKey()))
print "%s - (%d)" % (publickey.exportKey() , len(publickey.exportKey()))
print " Original content: %s - (%d)" % (a_message, len(a_message))
print "Encrypted message: %s - (%d)" % (encrypted_msg, len(encrypted_msg))
print "Decrypted message: %s - (%d)" % (decrypted_msg, len(decrypted_msg))