网上有很多算法的理论说明,我也就不重复了,直接看结果。
测试环境:window10
测试语言:python3.8
先上结论:
只是对一个文件的单次加解密的话,使用RC4效率会比较高。至少相对于2M的文件而言是这样的。
拿了一个2M左右的文件进行加解密测试。最终的测试效率如下:
算法 | 10000次 | 1000次 | 1次 |
RC4-加密 | 53.3s | 5.33s | 5.98ms |
RC4-解密 | 53.1s | 5.32s | 5.98ms |
AEs-CBC-加密 | 1min1s | 6.26s | 6.95ms |
AEs-CBC-解密 | 53.9s | 5.48s | 5.98ms |
AEs-ECB-加密 | 29.8s | 2.91s | 6.98ms |
AEs-ECB-解密 | 29.6s | 2.85s | 5.96ms |
下面是RC4算法:
from Crypto.Cipher import ARC4
# 使用rc4加密
def _rc4_encrypt(c_content: bytes) -> bytes:
p_rc4_object = ARC4.new(b"XXXXXXXXXXXX")
c_encrypt_data = p_rc4_object.encrypt(c_content)
return c_encrypt_data
# rc4解密代码内容
def _get_rc4_code(c_encrypt_data: bytes) -> bytes:
p_py_rc4_obj = ARC4.new(b"XXXXXXXXXXXX")
c_decrypt_data = p_py_rc4_obj.decrypt(c_encrypt_data)
return c_decrypt_data
下面是AES-CBC算法:
# 补位的倍数
BLOCK_SIZE = 16
# CBC的16位偏移量
CBC_VECTOR = b"aaaaaaaaaaaaaaaa"
# 加密的密钥
ENCRYPT_KEY = b"bbbbbbbbbbbbbbbb"
# 加解密的模式
s_ciphor_mode = AES.MODE_CBC
# CBC加密
def _aes_cbc_encrypt(c_content: bytes) -> bytes:
# 进行补位
c_content = pad(c_content, BLOCK_SIZE)
p_aes_cbc = AES.new(ENCRYPT_KEY, s_ciphor_mode, CBC_VECTOR)
c_encrypt_content = p_aes_cbc.encrypt(c_content)
return c_encrypt_content
# CBC解密
def _aes_cbc_decrypt(c_content: bytes) -> bytes:
p_aes_cbc = AES.new(ENCRYPT_KEY, s_ciphor_mode, CBC_VECTOR)
c_decrypt_content = p_aes_cbc.decrypt(c_content)
# 清除之前补位的空值
c_decrypt_content = c_decrypt_content.rstrip(b"\x04")
return c_decrypt_content
下面是AES-ECB算法:
# 加解密的模式
s_ciphor_mode = AES.MODE_ECB
# 加密
def _aes_encrypt(c_content: bytes) -> bytes:
p_aes = AES.new(ENCRYPT_KEY, s_ciphor_mode)
# 补位为 BLOCK_SIZE 的倍数
c_content = pad(c_content, BLOCK_SIZE)
c_encrypt_data = p_aes.encrypt(c_content)
return c_encrypt_data
# 解密
def _aes_decrypt(c_encrypt_text: bytes) -> bytes:
p_aes = AES.new(ENCRYPT_KEY, s_ciphor_mode)
c_decrypt_data = p_aes.decrypt(c_encrypt_text)
return c_decrypt_data.rstrip(b"\x04")