欢迎访问宙启技术站
智能推送

Python函数如何实现字符串加密和解密功能?

发布时间:2023-06-25 10:05:14

Python函数可以通过多种方式实现字符串加密和解密功能。其中,对称密钥加密和非对称密钥加密是两种最为常见的方法。

对称密钥加密指的是同一密钥可以被用于加密和解密操作的加密方式。其中,最常用的对称密钥加密算法是AES(Advanced Encryption Standard)算法。Python标准库中的cryptography模块提供了对该算法的支持。

以下是一个使用AES加密字符串的Python函数示例:

from cryptography.fernet import Fernet

def encrypt_string(message, key):
    cipher_suite = Fernet(key)
    cipher_text = cipher_suite.encrypt(message.encode())
    return cipher_text.decode()

def decrypt_string(cipher_text, key):
    cipher_suite = Fernet(key)
    plain_text = cipher_suite.decrypt(cipher_text.encode())
    return plain_text.decode()

在该示例中,我们使用了cryptography.fernet模块中的Fernet类。该类是基于AES算法实现的对称密钥加密方法。

encrypt_string函数中,我们首先使用给定的密钥创建了一个Fernet对象。然后,我们将明文字符串编码为字节序列,并将其作为输入参数传递给Fernet对象的encrypt方法。该方法将使用密钥对输入数据进行加密,并返回一个字节序列。最后,我们将该字节序列解码为字符串,并将其作为结果返回。

decrypt_string函数中,我们执行了与encrypt_string函数类似的操作,但是我们将输入的密文字符串传递给了Fernet对象的decrypt方法。该方法将使用密钥对输入数据进行解密,并返回一个字节序列。最后,我们将该字节序列解码为字符串,并将其作为结果返回。

如果我们需要使用不同的密钥进行加密和解密操作,我们可以将密钥作为参数传递给这两个函数。例如,我们可以使用以下代码进行加密和解密操作:

key = Fernet.generate_key()
message = "Hello, world!"
cipher_text = encrypt_string(message, key)
plain_text = decrypt_string(cipher_text, key)
print("cipher text:", cipher_text)
print("plain text:", plain_text)

在该示例中,我们首先使用Fernet.generate_key方法生成了一个新的密钥。然后,我们调用了encrypt_string函数并将明文字符串和该密钥作为输入参数传递。接着,我们调用了decrypt_string函数并将密文字符串和该密钥作为输入参数传递。最后,我们将加密后的密文字符串和解密后的明文字符串打印到控制台上。

除了对称密钥加密以外,还有一种常见的加密方式是非对称密钥加密。在这种加密方式中,使用一对密钥进行加密和解密操作。公钥被用于加密数据,而私钥被用于解密数据。在Python中,我们可以使用RSA算法来实现非对称密钥加密功能。Python标准库中的cryptography模块也支持该算法。

以下是一个使用RSA算法加密字符串的Python函数示例:

from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import padding, rsa

def generate_rsa_key_pair():
    private_key = rsa.generate_private_key(
        public_exponent=65537,
        key_size=2048
    )
    public_key = private_key.public_key()
    return private_key, public_key

def encrypt_string(message, public_key):
    cipher_text = public_key.encrypt(
        message.encode(),
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None
        )
    )
    return cipher_text.decode()

def decrypt_string(cipher_text, private_key):
    plain_text = private_key.decrypt(
        cipher_text.encode(),
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None
        )
    )
    return plain_text.decode()

在该示例中,我们使用了cryptography.hazmat模块中的类。该模块提供了底层加密原语的实现。

generate_rsa_key_pair函数中,我们使用了rsa.generate_private_key方法生成了一个新的RSA私钥。然后,我们使用私钥的public_key属性生成了一个RSA公钥。最后,我们将私钥和公钥一起返回。

encrypt_string函数中,我们首先将明文字符串编码为字节序列。然后,我们使用公钥的encrypt方法对该字节序列进行加密。在加密操作中,我们使用了padding.OAEP选项来提高安全性。最后,我们将加密后的字节序列解码为字符串,并将其作为结果返回。

decrypt_string函数中,我们执行了类似的操作,但是我们使用私钥的decrypt方法来解密输入的密文字符串。

如果我们需要使用不同的密钥进行加密和解密操作,我们可以将私钥和公钥作为参数分别传递给这两个函数。例如,我们可以使用以下代码进行加密和解密操作:

private_key, public_key = generate_rsa_key_pair()
message = "Hello, world!"
cipher_text = encrypt_string(message, public_key)
plain_text = decrypt_string(cipher_text, private_key)
print("cipher text:", cipher_text)
print("plain text:", plain_text)

在该示例中,我们调用了generate_rsa_key_pair函数生成了一个新的RSA密钥对。然后,我们调用了encrypt_string函数,并将明文字符串和该RSA公钥作为输入参数传递。接着,我们调用了decrypt_string函数,并将密文字符串和该RSA私钥作为输入参数传递。最后,我们将加密后的密文字符串和解密后的明文字符串打印到控制台上。