关键词:DES、3DES、ECB、CBC、CFB、OFB、CTR等等。
1 基本概念
DES介绍
DES是对称性加密里常见的一种,是一种使用秘钥加密的块算法。秘钥长度是64位(bit), 超过位数秘钥被忽略。所谓对称性加密,加密和解密秘钥相同。对称性加密一般会按照固定长度,把待加密字符串分成块。不足一整块或者刚好最后有特殊填充字符。
常见的填充模式有:'pkcs5'、'pkcs7'、'iso10126'、'ansix923'、'zero' 类型,包括DES-ECB、DES-CBC、DES-CTR、DES-OFB、DES-CFB。
DES基本原则
DES算法原理
DES算法的入口参数:Key、Data、Mode。
Key为8个字节共64位,是DES算法的工作秘钥;
Data也为8个字节64位,是要被加密或解密的数据;
Mode为DES的工作方式,有两种:加密或解密。
DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位,其算法主要分为两步:
(1)初始置换(2)逆置换
经过16次迭代运算后,得到L16、R16,将此作为输入,进行逆置换,逆置换正好是初始置换的逆运算,由此即得到密文输出。
3DES介绍
DES和3DES对比
2 OpenSSL关于DES/3DES用法
OpenSSL通过openssl enc进行加密解密,
openssl enc -des-cbc -e -k 1234567812345678 -iv 1234567812345678 -pbkdf2 -in infile -out cipherfile openssl enc -des-cbc -d -k 1234567812345678 -iv 1234567812345678 -pbkdf2 -in cipherfile -out replain
其中-des-cbc是算法和模式组合,-e表示Encrypt,-d表示Decrypt。
DES/3DES支持的算法和模式组合包括:
des-cbc DES in CBC mode des Alias for des-cbc des-cfb DES in CFB mode des-ofb DES in OFB mode des-ecb DES in ECB mode des-ede-cbc Two key triple DES EDE in CBC mode des-ede Two key triple DES EDE in ECB mode des-ede-cfb Two key triple DES EDE in CFB mode des-ede-ofb Two key triple DES EDE in OFB mode des-ede3-cbc Three key triple DES EDE in CBC mode des-ede3 Three key triple DES EDE in ECB mode des3 Alias for des-ede3-cbc des-ede3-cfb Three key triple DES EDE CFB mode des-ede3-ofb Three key triple DES EDE in OFB mode
更多参考:
3 PyCryptodome关于DES/3DES用法
DES的密文或明文需要满足63bit,即8Byte对齐。
DES的秘钥固定为64bit。
from Crypto.Cipher import DES from Crypto import Random def des_encrypt_decrypt(mode): key = b"Eightkey" des_encrypt = DES.new(key, mode) plaintext = "Some data to be encrypted with 3DES.PPPP" ciphertext = des_encrypt.encrypt(plaintext.encode()) des_decrypt = DES.new(key, mode) replaintext = des_decrypt.decrypt(ciphertext) print("Plain text:",plaintext.encode('utf-8').hex()) print("Cipher text:", ciphertext.hex()) print("Replain text:", replaintext.hex()) def des_encrypt_decrypt_iv(mode, iv): key = b"Eightkey" des_encrypt = DES.new(key, mode, iv) plaintext = "Some data to be encrypted with 3DES.PPPP" ciphertext = des_encrypt.encrypt(plaintext.encode('utf-8')) des_decrypt = DES.new(key, mode, iv) replaintext = des_decrypt.decrypt(ciphertext) print("Plain text:",plaintext.encode('utf-8').hex()) print("Cipher text:", ciphertext.hex()) print("Replain text:", replaintext.hex()) if __name__ == '__main__': iv = Random.new().read(DES.block_size) nonce = Random.new().read(8) des_encrypt_decrypt(DES.MODE_ECB) des_encrypt_decrypt_iv(DES.MODE_CBC, iv) des_encrypt_decrypt_iv(DES.MODE_CFB, iv) des_encrypt_decrypt_iv(DES.MODE_OFB, iv)
DES3的密文或明文输入需要满足64bit,即8Byte对齐。
秘钥输入需满足128bit或192bit,即16Byte或24Byte。
from Crypto.Cipher import DES3 from Crypto import Random def des3_encrypt_decrypt(mode): key = "Sixteen byte key" des3_encrypt = DES3.new(key, mode) plaintext = "Some data to be encrypted with 3DES.PPPP" ciphertext = des3_encrypt.encrypt(plaintext.encode('utf-8')) des3_decrypt = DES3.new(key, mode) replaintext = des3_decrypt.decrypt(ciphertext) print("Plain text:",plaintext.encode('utf-8').hex()) print("Cipher text:", ciphertext.hex()) print("Replain text:", replaintext.hex()) def des3_encrypt_decrypt_iv(mode, iv): key = "Sixteen byte key" des3_encrypt = DES3.new(key, mode, iv) plaintext = "Some data to be encrypted with 3DES.PPPP" ciphertext = des3_encrypt.encrypt(plaintext.encode('utf-8')) des3_decrypt = DES3.new(key, mode, iv) replaintext = des3_decrypt.decrypt(ciphertext) print("Plain text:",plaintext.encode('utf-8').hex()) print("Cipher text:", ciphertext.hex()) print("Replain text:", replaintext.hex()) if __name__ == '__main__': iv = Random.new().read(DES3.block_size) des3_encrypt_decrypt(DES3.MODE_ECB) des3_encrypt_decrypt_iv(DES3.MODE_CBC, iv) des3_encrypt_decrypt_iv(DES3.MODE_CFB, iv) des3_encrypt_decrypt_iv(DES3.MODE_OFB, iv)
更多参考:
《Single DES — PyCryptodome 3.14.1 documentation》-PyCryptodome关于DES介绍。
《Triple DES — PyCryptodome 3.14.1 documentation》-PyCryptodome关于3DES介绍。