CryptoJS

Python  AES 代码参考

'''
data block size: 16 bytes
key length: 129, 192, 256 bits
mode: MODE_ECB, MODE_CBC, MODE_CFB, MODE_OFB, MODE_CTR, MODE_OPENPGP, MODE_CCM, MODE_EAX, MODE_GCM, MODE_SIV, MODE_OCB
iv: 16 bytes
'''

from Crypto.Cipher import AES

# 加密
obj = AES.new(b'This is a key123', AES.MODE_CBC, b'This is an IV456')
message = b"The answer is no"
ciphertext = obj.encrypt(message)
print(ciphertext)

# 解密
obj = AES.new(b'This is a key123', AES.MODE_CBC, b'This is an IV456')
plainttext = obj.decrypt(ciphertext)
print(plainttext)
    

Python  DES 代码参考

'''
data block size: 8 bytes
key length: 64 bits
mode: MODE_ECB, MODE_CBC, MODE_CFB, MODE_OFB, MODE_CTR, MODE_OPENPGP, MODE_EAX
iv: 8 bytes
'''

from Crypto.Cipher import DES

# 加密
obj = DES.new(b'key12345', DES.MODE_CBC, b'iv456789')
message = b"The answer is no"
ciphertext = obj.encrypt(message)
print(ciphertext)

# 解密
obj = DES.new(b'key12345', DES.MODE_CBC, b'iv456789')
plainttext = obj.decrypt(ciphertext)
print(plainttext)
    

Python  3DES 代码参考

'''
data block size: 8 bytes
key length: 128, 192 bits
mode: MODE_ECB, MODE_CBC, MODE_CFB, MODE_OFB, MODE_CTR, MODE_OPENPGP, MODE_EAX
iv: 8 bytes
'''

from Crypto.Cipher import DES3

# 加密
obj = DES3.new(b'Sixteen byte key', DES3.MODE_CBC, b'iv012345')
message = b'sona si latine loqueris '
ciphertext = obj.encrypt(message)
print(ciphertext)

# 解密
obj = DES3.new(b'Sixteen byte key', DES3.MODE_CBC, b'iv012345')
plainttext = obj.decrypt(ciphertext)
print(plainttext)
    

Python  RC4 代码参考

# 模块安装
pip install pycrypto
    
from Crypto.Cipher import ARC4
from Crypto.Hash import SHA
from Crypto import Random

key = b'Very long and confidential key'
nonce = Random.new().read(16)
tempkey = SHA.new(key+nonce).digest()

# 加密
cipher = ARC4.new(tempkey)
message = b'Open the pod bay doors, HAL'
ciphertext = cipher.encrypt(message)
print(ciphertext)

# 解密
cipher = ARC4.new(tempkey)
plainttext = cipher.decrypt(ciphertext)
print(plainttext)