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

Python中使用Crypto.Cipher.Blowfish模块加密字符串和文件

发布时间:2023-12-19 02:36:05

Blowfish算法是一种对称密钥加密算法,可以在Python中使用Crypto模块的Cipher子模块来实现。在使用之前,需要先安装pycryptodome模块。

1. 安装pycryptodome模块

可以使用以下命令来安装pycryptodome模块:

pip install pycryptodomex

2. 使用Blowfish算法加密字符串

下面是一个使用Blowfish算法加密字符串的例子:

from Crypto.Cipher import Blowfish
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes

def encrypt_string(key, plaintext):
    cipher = Blowfish.new(key, Blowfish.MODE_CBC)

    # 将明文填充至16字节的倍数
    padded_plaintext = pad(plaintext.encode(), Blowfish.block_size)

    # 生成初始向量(iv)
    iv = get_random_bytes(Blowfish.block_size)

    # 执行加密
    ciphertext = iv + cipher.encrypt(padded_plaintext)

    return ciphertext

def decrypt_string(key, ciphertext):
    cipher = Blowfish.new(key, Blowfish.MODE_CBC)

    # 分离初始向量(iv)
    iv = ciphertext[:Blowfish.block_size]
    ciphertext = ciphertext[Blowfish.block_size:]

    # 执行解密
    padded_plaintext = cipher.decrypt(ciphertext)

    # 去除填充
    plaintext = unpad(padded_plaintext, Blowfish.block_size).decode()

    return plaintext

key = b'p@ssw0rd'
plaintext = 'Hello, world!'

ciphertext = encrypt_string(key, plaintext)
print('加密后的字符串:', ciphertext)

decrypted_text = decrypt_string(key, ciphertext)
print('解密后的字符串:', decrypted_text)

在上面的例子中,我们使用Blowfish算法加密了字符串"Hello, world!"。首先,我们使用Crypto.Util.Padding子模块中的pad函数将明文填充至16字节的倍数。然后,使用get_random_bytes函数生成一个16字节的随机初始向量(iv)。接下来,我们使用Blowfish.new函数创建一个Blowfish对象,并设置加密模式为Blowfish.MODE_CBC。然后,使用该对象的encrypt方法对填充后的明文进行加密。加密结果为初始向量和密文的拼接。最后,我们使用解密过程的逆操作,将密文解密成明文并使用unpad函数去除填充。

3. 使用Blowfish算法加密文件

下面是一个使用Blowfish算法加密文件的例子:

from Crypto.Cipher import Blowfish
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes

def encrypt_file(key, input_file_path, output_file_path):
    cipher = Blowfish.new(key, Blowfish.MODE_CBC)

    # 生成初始向量(iv)
    iv = get_random_bytes(Blowfish.block_size)

    # 打开输入文件和输出文件
    with open(input_file_path, 'rb') as input_file, open(output_file_path, 'wb') as output_file:
        # 写入初始向量
        output_file.write(iv)

        # 逐块加密并写入输出文件
        while True:
            chunk = input_file.read(Blowfish.block_size)
            if len(chunk) == 0:
                break
            elif len(chunk) % Blowfish.block_size != 0:
                chunk = pad(chunk, Blowfish.block_size)
            output_file.write(cipher.encrypt(chunk))

def decrypt_file(key, input_file_path, output_file_path):
    cipher = Blowfish.new(key, Blowfish.MODE_CBC)

    # 打开输入文件和输出文件
    with open(input_file_path, 'rb') as input_file, open(output_file_path, 'wb') as output_file:
        # 读取初始向量
        iv = input_file.read(Blowfish.block_size)

        # 逐块解密并写入输出文件
        while True:
            chunk = input_file.read(Blowfish.block_size)
            if len(chunk) == 0:
                break
            output_file.write(cipher.decrypt(chunk))

# 示例使用
key = b'p@ssw0rd'
input_file_path = 'input.txt'
encrypted_file_path = 'encrypted.bin'
decrypted_file_path = 'decrypted.txt'

# 加密文件
encrypt_file(key, input_file_path, encrypted_file_path)
print('加密完成')

# 解密文件
decrypt_file(key, encrypted_file_path, decrypted_file_path)
print('解密完成')

在上面的例子中,我们使用Blowfish算法加密了一个文件。首先,我们使用Crypto.Util.Padding子模块中的pad函数对文件内容进行填充。然后,使用get_random_bytes函数生成一个16字节的随机初始向量(iv)。接着,我们使用Blowfish.new函数创建一个Blowfish对象,并设置加密模式为Blowfish.MODE_CBC。然后,打开输入文件和输出文件,将初始向量写入输出文件,并逐块加密文件内容并写入输出文件。解密文件的过程与加密文件的过程相似,但是需要先读取初始向量。

以上是Python中使用Crypto.Cipher.Blowfish模块加密字符串和文件的例子。