python3 对称加密算法AES、DES3

python3.6此库安装方式,需要pip3 install pycryptodome。

如有site-packages中存在crypto、pycrypto,在pip之前,需要pip3 uninstall crypto、pip3 uninstall pycrypto,否则无法安装成功。

C:\WINDOWS\system32>pip3 install pycryptodome
Collecting pycryptodome
  Downloading https://files.pythonhosted.org/packages/0f/5d/a429a53eacae3e13143248c3868c76985bcd0d75858bd4c25b574e51bd4d/pycryptodome-3.6.3-cp36-cp36m-win_amd64.whl (7.9MB)
    100% |████████████████████████████████| 7.9MB 111kB/s
Installing collected packages: pycryptodome
Successfully installed pycryptodome-3.6.3

这里顺带说一下pycrypto,这个库已经有很久没有人维护了,如果需要安装此库,需要先安装 VC++ build tools

然后将 ~\BuildTools\VC\Tools\MSVC\14.15.26726\include 目录下的 stdint.h 拷贝到 C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt 下。(Win10 需管理员权限)

接着将同目录下的 inttypes.h 中的 #include <stdint.h> (第十四行),改成 #include "stdint.h"。

然后使用 pip3 install pycrypto,就能直接安装了。

注:如果不是业务需要,请尽可能使用 pycryptodome。

 

 

AES:

点击查看AES的工厂模式(已封装)

import Crypto.Cipher.AES
import Crypto.Random
import base64
import binascii

def auto_fill(x):
    if len(x) <= 32:
        while len(x) not in [16, 24, 32]:
            x += " "
        return x.encode()
    else:
        raise "密钥长度不能大于32位!"

key = "asd"
content = "abcdefg1234567"
x = Crypto.Cipher.AES.new(auto_fill(key), Crypto.Cipher.AES.MODE_ECB)
a = base64.encodebytes(x.encrypt(auto_fill(content)))
b = x.decrypt(base64.decodebytes(a))
print(a)
print(b)
a = binascii.b2a_base64(x.encrypt(auto_fill(content)))
b = x.decrypt(binascii.a2b_base64(a))
print(a)
print(b)

key = "dsa"
iv = Crypto.Random.new().read(16)   # 向量,必须为16字节
content = "1234567abcdefg"
y = Crypto.Cipher.AES.new(auto_fill(key), Crypto.Cipher.AES.MODE_CBC, iv)
c = binascii.b2a_base64(y.encrypt(auto_fill(content)))
z = Crypto.Cipher.AES.new(auto_fill(key), Crypto.Cipher.AES.MODE_CBC, iv)
d = z.decrypt(binascii.a2b_base64(c))
print(c)
print(d)

运行结果:

b'jr/EIUp32kLHc3ypZZ1cyg==\n'
b'abcdefg1234567  '
b'jr/EIUp32kLHc3ypZZ1cyg==\n'
b'abcdefg1234567  '
b'j+Ul9KQd0HnuiHW3z9tD7A==\n'
b'1234567abcdefg  '

 

 

 

DES3:

import Crypto.Cipher.DES3
import base64
import binascii

def auto_fill(x):
    if len(x) > 24:
        raise "密钥长度不能大于等于24位!"
    else:
        while len(x) < 16:
            x += " "
        return x.encode()

key = "asd"
content = "abcdefg1234567"
x = Crypto.Cipher.DES3.new(auto_fill(key), Crypto.Cipher.DES3.MODE_ECB)
a = base64.encodebytes(x.encrypt(auto_fill(content)))
print(a)
b = x.decrypt(base64.decodebytes(a))
print(b)
a = binascii.b2a_base64(x.encrypt(auto_fill(content)))
b = x.decrypt(binascii.a2b_base64(a))
print(a)
print(b)

运行结果:

b'/ee3NFKeNqEk/qMNd1mjog==\n'
b'abcdefg1234567  '
b'/ee3NFKeNqEk/qMNd1mjog==\n'
b'abcdefg1234567  '

 

 

 

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用Python实现AESDES的示例代码: AES加密解密示例代码: ```python from Crypto.Cipher import AES import base64 # 加密函数 def aes_encrypt(key, data): cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB) data = data.encode('utf-8') # 补全16位 length = 16 - (len(data) % 16) data += bytes([length]) * length ciphertext = cipher.encrypt(data) # 转换为base64编码 return base64.b64encode(ciphertext).decode('utf-8') # 解密函数 def aes_decrypt(key, ciphertext): cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB) # 解码base64 ciphertext = base64.b64decode(ciphertext) data = cipher.decrypt(ciphertext) # 去除补位 length = data[-1] data = data[:-length] return data.decode('utf-8') # 测试 key = '0123456789abcdef' data = 'hello world' ciphertext = aes_encrypt(key, data) print(ciphertext) plaintext = aes_decrypt(key, ciphertext) print(plaintext) ``` DES加密解密示例代码: ```python from Crypto.Cipher import DES import base64 # 加密函数 def des_encrypt(key, data): cipher = DES.new(key.encode('utf-8'), DES.MODE_ECB) data = data.encode('utf-8') # 补全8位 length = 8 - (len(data) % 8) data += bytes([length]) * length ciphertext = cipher.encrypt(data) # 转换为base64编码 return base64.b64encode(ciphertext).decode('utf-8') # 解密函数 def des_decrypt(key, ciphertext): cipher = DES.new(key.encode('utf-8'), DES.MODE_ECB) # 解码base64 ciphertext = base64.b64decode(ciphertext) data = cipher.decrypt(ciphertext) # 去除补位 length = data[-1] data = data[:-length] return data.decode('utf-8') # 测试 key = '01234567' data = 'hello world' ciphertext = des_encrypt(key, data) print(ciphertext) plaintext = des_decrypt(key, ciphertext) print(plaintext) ``` 注意:这里仅提供示例代码,实际使用需要根据具体情况进行调整和优化。同时,使用对称密码算法时,需要注意密钥的安全性和保密性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值