如何使用Python模块的密码学

203 阅读8分钟

有没有想过你的密码是如何被隐藏的?你是否想过WhatsApp中的端到端加密这个词是什么意思?想到网上汇款的过程,你的好奇心是否有所动摇?

这种互联网隐私和安全的壮举是通过密码学实现的,它是一个保护你在线的概念。

什么是密码学?

密码学是一种锁定和钥匙技术,通过只有适当的接收者才能访问的代码,实现信息的安全通信。对数据进行加密和解密,以实现信息的保密性要求。

加密基本上是将普通文本转换为密码文本。解密是使用密匙从密码文本中检索出纯文本的过程。

密码学的类型

有三种广泛使用的密码学类型。

密码学的类型描述
对称密钥加密法这是一个加密系统,使用单一的共同密钥对信息进行加密和解密。这种系统更快、更简单,但密钥必须以安全的方式共享。
哈希函数一个被称为哈希值的压缩的固定长度的信息符号被生成,而不是钥匙。许多操作系统使用散列函数来加密密码,因为它们完全不受其他用户影响。
非对称密钥加密法在这个系统中使用一对独特的钥匙,公钥 --用于加密,私钥 --用于解密信息。

表:密码学的类型

用于密码学的Python模块

模块是包含Python语句和函数、类和变量定义的文件,可以在你的程序中使用。它本质上使代码可以被理解并有逻辑地组织。

在这篇文章中,我们将阐明一些可用于数据加密和解密的模块。

1.Fernet模块

Fernet模块隶属于密码学包。在这里,一个独特的密钥被生成,没有它,数据就不能被读取/修改。因此,它实现了对称密钥加密法。

它使用三种主要方法来生成密钥,加密和解密数据。

方法使用情况语法
generate_key()这个方法用来生成一个新的密钥。key = Fernet.generate_key(
)f = Fernet(key
)
encrypt(data)数据被加密成密码文本,作为 "Fernet token",其单位是字节。参数是纯文本。encrypted_data = f.encrypt(b "要加密的数据")
解密(encrypted_data)这个方法从Fernet Token中检索出原始的纯文本。decrypted_data = f.decrypt(encrypted_data)

表:Python中用于加密的Fernet模块的方法

要使用Fernet模块,你需要先通过运行以下命令来安装密码学包。

pip install cryptography

现在让我们看一个使用Fernet模块在Python中实现密码学的例子。

# Importing Fernet module from the cryptography package
from cryptography.fernet import Fernet

# Generating a key
key = Fernet.generate_key()

# Assigning the value of key to a variable
f = Fernet(key)

# Converting the plaintext to ciphertext
plainText = input("Enter your text to be encrypted: ").encode()
encryptedData = f.encrypt(plainText)

# Displaying the ciphertext
print("The encrypted data is: ", encryptedData)

# Decrypting the ciphertext
decryptedData = f.decrypt(encryptedData)

# Printing the decrypted data by converting it from byte to string
print("The decrypted data is: ", decryptedData.decode())

正如你所看到的,首先,该模块被从包中导入。然后Fernet密钥被生成并存储在一个变量f中。用户输入纯文本,它被加密。当调用时,它返回一个加密的密码文本。

加密文本随后被解密并打印成一个字符串,解密函数将解密文本从字节转换为一个字符串。

输出

cryptography using the fernet module of Python

2.Cryptocode模块

Cryptocode是一个库,它基本上是一个模块的集合。它是最简单的加密和解密方式,因为它更适合于希望获得单纯抽象的人。

加密有两个参数--要编码的字符串和解密编码字符串的密钥。这个库不是一个内置的模块,所以需要用命令将其安装在你的终端。

pip install cryptocode

让我们看一个使用Cryptocode模块在Python中实现加密的例子。

# Importing the cryptocode module
import cryptocode
 
# Entering the string data and password
plainText = input("Enter the text: ")
password = input("Enter the password: ")
 
# Encrypting the plain text with the password
encryptedData = cryptocode.encrypt(plainText , password)
 
# Displaying the ciphertext
print("Encrypted data: ",encryptedData)
 
# Decrypting the ciphertext
decryptedData = cryptocode.decrypt(encryptedData, password)
 
# Displaying the decrypted data
print("Decrypted data: ",decryptedData)

首先导入该模块,然后输入 plainText 和密码字符串。cryptocode.encrypt中的第一个参数是一个将被加密成密码文本的字符串,后者是用于解密的密钥。

使用密码对密码文本进行解密,并显示出纯文本。如果密码不正确,系统会抛出一个错误。

输出

Cryptography using cryptocode module in Python

3.RSA算法

RSA算法实现了非对称密钥加密技术。它使用两个密钥对文本进行加密和解密--一个公钥和一个私钥。

任何拥有公钥的人都可以加密和发送数据。但只有拥有私钥的用户才能解密和访问数据。

要安装RSA库,请在你的终端输入以下代码。

pip install rsa

让我们看看使用RSA算法的示例代码。

# Importing rsa
import rsa

# Generating public and private keys
public_key, private_key = rsa.newkeys(512)
 
# Reading the plain text
plainText = input("Enter the plain text to be encrypted: ")
  
# Encrypting the plain text using the public key
encryptedData = rsa.encrypt(plainText.encode(),public_key)
  
# Decrypting the cipher text using the private key
decryptedData = rsa.decrypt(encryptedData, private_key).decode()
 
# Displaying the plain text, encoded cipher text and decoded plain text
print("The primordial string: ", plainText)
print("The Encrypted message: ", encryptedData)  
print("The Decrypted message: ", decryptedData)

在上面给出的代码中,我们导入RSA 库并生成公钥和私钥。用户输入纯文本。然后用公钥将其加密成一个密码文本。

私钥被用来将密码文本解密为纯文本,并显示所有的属性。

输出

Cryptography using rsa algorithms in Python

4.Hashlib模块

Hashlib模块是一个散列算法的集合,用于将数据加密为散列。不管数据的大小,散列是一个固定长度的字符串。

这种编码技术被科技巨头广泛使用,因为加密后的数据几乎不可能解密。这是Python中的一个内置模块,所以你不需要安装它。在hashlib下的众多算法中,我们将看一看SHA256算法。

SHA256

SHA256--安全哈希算法将数据转换为一个长度为256字节的固定字符串。它提高了安全性,因为更长的哈希值会导致更高的安全级别。它是SHA1算法的继承者。

现在让我们来看看一个示例代码。

# Importing the hashlib module
import hashlib 
 
# Reading the text to be encoded
plainText = input("Enter the text to be encrypted: ").encode()
 
# Hashing and encrypting the text
encryptedData = hashlib.sha256(plainText)
 
# Converting the encrypted text into a hexadecimal
converted = encryptedData.hexdigest()
print(converted)

导入了hashlib模块。输入文本,并使用函数*encode()*将其转换为字节。然后对文本进行散列和加密。最后,它被转换成一个十六进制的字符串并显示出来。

输出

Cryptography using hashlib modules (SHA256) in Python

5.AES算法

AES算法使用单一的通用密匙来加密和解密数据,由于其数据完整性高,被政府所采用。加密和解密发生在块中,数据被修改并存储在大小为128比特的块中,而加密密钥的大小可以是128、192或256比特。

不同的模式被应用来促进数据在块中的适当流。这五种模式是:。

  • ECB模式。电子密码本模式
  • CBC模式。密码块链模式
  • CFB模式。密码反馈模式
  • OFB模式。输出回馈模式
  • CTR模式。计数器模式

在你的终端运行下面的代码来安装pyaes模块。

pip install pyaes

让我们看看一个例子代码。

# Importing the module pyaes
import pyaes
 
# The key and text are entered and are encoded
key = input("Enter a key of 32 bits: ").encode('utf-8')
plainText = input("Enter the text to be encrypted: ").encode('utf-8')
 
# AES is constructed using counter mode with key as parameter
aes = pyaes.AESModeOfOperationCTR(key)
 
# Encrypting the plain text
encryptedData = aes.encrypt(plainText)
print(encryptedData)
 
# Decrypting the encrypted text as plain text
aes = pyaes.AESModeOfOperationCTR(key)
decryptedData = aes.decrypt(encryptedData)
 
# Displaying the plain text
print(decryptedData.decode()) 

pyaes 模块被导入。输入密钥和文本,并将其编码为字节。密钥应该是32字节(256位),否则会出现错误。

使用计数器模式作为参数构建AES,并对文本进行加密。解密需要创建一个新的实例,因为计数器的运行模式保持其状态。最后,文本被解密并显示为纯文本。

输出

Cryptography using AES algorithms in Python

6.简单加密模块

Simple-crypt通过将其描述为仅用一行代码就能对文本进行加密和解密的最快和最简单的方法之一来证明其名称的正确性。它还使用了pycrypto 模块,该模块提供了必要的算法实现,并在密码文本被修改时进行检查以发出警告。

在你的终端输入以下命令来安装这些模块。

pip install simple-crypt --no-dependencies
pip install pycrypto

让我们来看看一个示例代码。

# Importing encrypt and decrypt from simple-crypt
from simplecrypt import encrypt, decrypt
 
# Entering the key and plain text
key = input("Enter the key: ")
plainText = input("Enter the plain text: ")
 
# Encrypting the plain text
encryptedData = encrypt(key, plainText)
 
# Displaying the cipher text
print("Encrypted data: ",encryptedData)
 
# Decrypting the cipher text and display it
print("Encrypted data: ",decrypt(key, encryptedData))

输出

Cryptography using simple-crypt module in Python

最后的思考

密码学是一个巨大的海洋,需要更多的探索。我已经介绍了一些用于密码学的著名Python模块的工作。每个模块都有一个独特的特性,必须在适当的情况下实现,才能有效地使用它们。

我希望你对Python中使用的密码学模块有了新的认识。祝你在今后的工作中一切顺利!