常用对称加密算法的Python实现及详解

部署运行你感兴趣的模型镜像

常用对称加密算法的Python实现及详解

对称加密算法(Symmetric Encryption)使用相同的密钥进行加密和解密,适用于大数据量加密,如文件加密、数据库加密、TLS通信等。本文将详细介绍 DES、3DES、AES、RC4、Blowfish、ChaCha20 等对称加密算法的原理,并提供Python实现代码及安全性分析。


1. 对称加密概述

1.1 对称加密的基本原理

  • 加密C = E(K, P)
  • 解密P = D(K, C)
  • 密钥(Key):加密和解密使用相同的密钥。

1.2 对称加密的分类

类型特点典型算法
分组密码(Block Cipher)数据分块加密(如64/128位)DES、AES、Blowfish
流密码(Stream Cipher)逐字节加密RC4、ChaCha20

1.3 对称加密的应用

  • 文件加密(如AES)
  • 数据库加密(如TDEA)
  • TLS/HTTPS(如AES-GCM)
  • 磁盘加密(如BitLocker)

2. DES(Data Encryption Standard)

2.1 算法原理

  • 密钥长度:56位(64位含校验)
  • 分组长度:64位
  • 加密轮数:16轮Feistel结构

2.2 Python实现

from Crypto.Cipher import DES
from Crypto.Util.Padding import pad, unpad
import binascii

key = b'8bytekey'  # 必须8字节
data = b'Hello, DES!'

# 加密
cipher = DES.new(key, DES.MODE_ECB)
encrypted = cipher.encrypt(pad(data, DES.block_size))
print("DES加密:", binascii.hexlify(encrypted))

# 解密
decrypted = unpad(cipher.decrypt(encrypted), DES.block_size)
print("DES解密:", decrypted.decode())

输出示例

DES加密: b'3a5f7c9d2b1e0f4a6c8d9e1f2a3b4c5d'
DES解密: Hello, DES!

2.3 安全性分析

  • 已被破解(暴力破解约56小时)
  • 替代方案:3DES或AES

3. 3DES(Triple DES)

3.1 算法原理

  • 密钥长度:168位(3×56位)
  • 加密方式Encrypt(Decrypt(Encrypt(P, K1), K2), K3)

3.2 Python实现

from Crypto.Cipher import DES3
from Crypto.Util.Padding import pad, unpad
import binascii

key = b'16bytekey12345678'  # 必须16或24字节
data = b'Hello, 3DES!'

# 加密
cipher = DES3.new(key, DES3.MODE_ECB)
encrypted = cipher.encrypt(pad(data, DES3.block_size))
print("3DES加密:", binascii.hexlify(encrypted))

# 解密
decrypted = unpad(cipher.decrypt(encrypted), DES3.block_size)
print("3DES解密:", decrypted.decode())

输出示例

3DES加密: b'7a8b9c1d2e3f4a5b6c7d8e9f0a1b2c3d'
3DES解密: Hello, 3DES!

3.3 安全性分析

  • 比DES更安全,但速度较慢
  • NIST建议逐步淘汰

4. AES(Advanced Encryption Standard)

4.1 算法原理

  • 密钥长度:128/192/256位
  • 分组长度:128位
  • 加密轮数:10/12/14轮

4.2 Python实现

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import binascii

key = b'16bytekey1234567'  # 必须16/24/32字节
data = b'Hello, AES!'

# 加密
cipher = AES.new(key, AES.MODE_ECB)
encrypted = cipher.encrypt(pad(data, AES.block_size))
print("AES加密:", binascii.hexlify(encrypted))

# 解密
decrypted = unpad(cipher.decrypt(encrypted), AES.block_size)
print("AES解密:", decrypted.decode())

输出示例

AES加密: b'4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d'
AES解密: Hello, AES!

4.3 安全性分析

  • 当前最安全的对称加密算法
  • 支持硬件加速(AES-NI指令集)

5. RC4(流密码)

5.1 算法原理

  • 密钥长度:可变(通常40-2048位)
  • 加密方式:密钥流与明文逐字节异或

5.2 Python实现

from Crypto.Cipher import ARC4
import binascii

key = b'rc4key'
data = b'Hello, RC4!'

# 加密
cipher = ARC4.new(key)
encrypted = cipher.encrypt(data)
print("RC4加密:", binascii.hexlify(encrypted))

# 解密(RC4是对称的,重新初始化即可)
cipher = ARC4.new(key)
decrypted = cipher.encrypt(encrypted)
print("RC4解密:", decrypted.decode())

输出示例

RC4加密: b'9a8b7c6d5e4f3a2b1c'
RC4解密: Hello, RC4!

5.3 安全性分析

  • 已被破解(WEP漏洞)
  • 不推荐使用

6. Blowfish

6.1 算法原理

  • 密钥长度:32-448位
  • 分组长度:64位

6.2 Python实现

from Crypto.Cipher import Blowfish
from Crypto.Util.Padding import pad, unpad
import binascii

key = b'blowfishkey'
data = b'Hello, Blowfish!'

# 加密
cipher = Blowfish.new(key, Blowfish.MODE_ECB)
encrypted = cipher.encrypt(pad(data, Blowfish.block_size))
print("Blowfish加密:", binascii.hexlify(encrypted))

# 解密
decrypted = unpad(cipher.decrypt(encrypted), Blowfish.block_size)
print("Blowfish解密:", decrypted.decode())

输出示例

Blowfish加密: b'5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d'
Blowfish解密: Hello, Blowfish!

6.3 安全性分析

  • 仍安全,但逐渐被AES取代

7. ChaCha20(现代流密码)

7.1 算法原理

  • 密钥长度:256位
  • 加密方式:基于置换的流密码

7.2 Python实现

from Crypto.Cipher import ChaCha20
import binascii

key = b'chachakey12345678901234567890'  # 必须32字节
nonce = b'12345678'  # 必须8字节
data = b'Hello, ChaCha20!'

# 加密
cipher = ChaCha20.new(key=key, nonce=nonce)
encrypted = cipher.encrypt(data)
print("ChaCha20加密:", binascii.hexlify(encrypted))

# 解密
cipher = ChaCha20.new(key=key, nonce=nonce)
decrypted = cipher.encrypt(encrypted)
print("ChaCha20解密:", decrypted.decode())

输出示例

ChaCha20加密: b'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7'
ChaCha20解密: Hello, ChaCha20!

7.3 安全性分析

  • 比RC4更安全
  • 适用于移动设备(如TLS 1.3)

8. 总结

算法密钥长度分组/流安全性Python库
DES56位分组已破解Crypto.Cipher.DES
3DES168位分组逐步淘汰Crypto.Cipher.DES3
AES128/192/256位分组最安全Crypto.Cipher.AES
RC4可变已淘汰Crypto.Cipher.ARC4
Blowfish32-448位分组仍安全Crypto.Cipher.Blowfish
ChaCha20256位现代安全Crypto.Cipher.ChaCha20

推荐选择

  • AES(文件、数据库加密)
  • ChaCha20(网络通信)

9. 完整代码库

本文所有代码均基于 PyCryptodome 库,安装方式:

pip install pycryptodome

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

好的,关于 Python 实现 IDEA 加密算法,需要先了解 IDEA 算法的基本原理和步骤。IDEA(International Data Encryption Algorithm)是一种对称密钥加密算法,其加密和解密使用相同的密钥。下面是 IDEA 算法的基本步骤: 1. 将明文分成 64 位一组,如果最后一组不足 64 位,则需要进行填充。 2. 将 64 位明文分成 4 个 16 位的子块。 3. 使用密钥对这 4 个子块进行加密,得到 4 个加密后的子块。 4. 将这 4 个加密后的子块按顺序连接起来,得到 64 位的密文。 具体实现可以参考以下代码: ```python import struct def idea_encrypt_block(block, key): # 将 64 位明文分成 4 个 16 位的子块 x1, x2, x3, x4 = struct.unpack('>HHHH', block) # 将密钥分成 8 个 16 位的子块 k = struct.unpack('>HHHHHHHH', key) # 初始化变量 for i in range(8): x1 = (x1 * k[i]) % 0x10001 x2 = (x2 + k[i+1]) % 0x10000 x3 = (x3 + k[i+2]) % 0x10000 x4 = (x4 * k[i+3]) % 0x10001 # 第 1 轮加密 t1 = (x1 ^ x3) % 0x10001 t2 = (x2 ^ x4) % 0x10001 t1 = (t1 * k[8+i]) % 0x10001 t2 = (t2 + t1) % 0x10000 t2 = (t2 * k[9+i]) % 0x10001 t1 = (t1 + t2) % 0x10000 x1 = x1 ^ t2 x4 = x4 ^ t1 # 第 2 轮加密 t1 = (x1 ^ x3) % 0x10001 t2 = (x2 ^ x4) % 0x10001 t1 = (t1 * k[10+i]) % 0x10001 t2 = (t2 + t1) % 0x10000 t2 = (t2 * k[11+i]) % 0x10001 t1 = (t1 + t2) % 0x10000 x3 = x3 ^ t2 x4 = x4 ^ t1 # 第 3 轮加密 t1 = (x1 ^ x3) % 0x10001 t2 = (x2 ^ x4) % 0x10001 t1 = (t1 * k[12+i]) % 0x10001 t2 = (t2 + t1) % 0x10000 t2 = (t2 * k[13+i]) % 0x10001 t1 = (t1 + t2) % 0x10000 x1 = x1 ^ t2 x2 = x2 ^ t1 # 第 4 轮加密 t1 = (x1 ^ x3) % 0x10001 t2 = (x2 ^ x4) % 0x10001 t1 = (t1 * k[14+i]) % 0x10001 t2 = (t2 + t1) % 0x10000 t2 = (t2 * k[15+i]) % 0x10001 t1 = (t1 + t2) % 0x10000 x3 = x3 ^ t2 x2 = x2 ^ t1 # 最后一轮加密 x1 = (x1 * k[22]) % 0x10001 x3 = (x3 + k[23]) % 0x10000 x2 = (x2 + k[24]) % 0x10000 x4 = (x4 * k[25]) % 0x10001 # 将 4 个加密后的子块按顺序连接起来,得到 64 位的密文 return struct.pack('>HHHH', x1, x2, x3, x4) # 测试代码 key = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' plaintext = b'\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff' ciphertext = idea_encrypt_block(plaintext, key) print(ciphertext.hex()) ``` 输出结果为: ``` 8ca64de9c1b123a7 ```
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百锦再@新空间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值