from Crypto.Cipher import DES
from Crypto.Util.Padding import pad, unpad
import binascii
def encrypt(plaintext, key):
cipher = DES.new(key, DES.MODE_ECB)
padded_plaintext = pad(plaintext.encode(), DES.block_size)
ciphertext = cipher.encrypt(padded_plaintext)
return ciphertext
def decrypt(ciphertext, key):
cipher = DES.new(key, DES.MODE_ECB)
decrypted_data = cipher.decrypt(ciphertext)
plaintext = unpad(decrypted_data, DES.block_size).decode()
return plaintext
plaintext = "Hello, world!"
key_str = '0b9dcbe0f7101691'
key = bytes.fromhex(key_str)
encrypted_data = encrypt(plaintext, key)
print("加密后的数据:", encrypted_data.hex())
ciphertext = binascii.unhexlify("2dc57d06632e63af482e84c849feceb0f4aaba5474f87ae163522d3937322106c994b4f398e7c5d7482e84c849feceb0f4aaba5474f87ae18141a5a4afd9a901a1e2cf5f3a38a84f5f33c385ae264a73a7b5f93e5d2a07380eb8d48d512759d146e4e49ed04fbd8241faee61d5d8318c0054d22bb89d13b688c8b84df3c7357ea00c320d2c5a428b08d4b63f0cb5c271db3305a8a53114744fb4b5afb3b0055da00c320d2c5a428b08d4b63f0cb5c271c734619da7b41b8a7cb8ade5b5c4b652c9f56998b95d3318e753dd1336c7269f86d390541f19fee5f6278cc971f23499bcaa607dc7dab6c8669a73f3db89b4555d25e57f0d19d8bca133e6688b1a8faad3d195ad951e5a464428242a371422f00bf122490e487233a133e6688b1a8faad3d195ad951e5a464abd5cb39b308a2900e7998af2b1411d5c4d302dbb225fa0093768ca083c109707c1b4aac74d34c8dbccc957129fb0fe2c8dcd688fcbc691608532e495a91097704a72253dab6f711f5e44ec3c1e7debb8bd7379806dcf0d95f7efd1b6add462704a72253dab6f711f5e44ec3c1e7deb6c3151187591f9091cf5a50d7ee9df14acde2e313b24c909d63fc2821d421445d455001188309c75ba0e86a3d8e795003977bf34b358eb3a2c5318e76636da925f59d248d7c2ec63482e84c849feceb0f4aaba5474f87ae163522d3937322106c994b4f398e7c5d7482e84c849feceb0f4aaba5474f87ae18141a5a4afd9a901a1e2cf5f3a38a84f5f33c385ae264a73a7b5f93e5d2a0738c0402b9b07ff236f46e4e49ed04fbd8214ce35f41fd98ebaf896f215f815437e834c83e356f92d5e")
decrypted_text = decrypt(ciphertext, key)
print("解密后的数据:", decrypted_text)
import pyDes
key = b'0b9dcbe0f7101691'
iv = b'42104202'
k = pyDes.triple_des(key, pyDes.CBC, iv, pad=None, padmode=pyDes.PAD_PKCS5)
plaintext = b'Hello, World!'
encrypted_data = k.encrypt(plaintext)
decrypted_data = k.decrypt(encrypted_data)
print("加密后数据:", encrypted_data)
print("解密后数据:", decrypted_data)
from pyDes import des, PAD_PKCS5, CBC, ECB
BLOCK_SIZE = 8
def init_cipher(secret_key, iv_key):
secret_bytes = secret_key.encode(encoding="utf-8")
key_bytes = []
if len(secret_bytes) >= BLOCK_SIZE:
key_bytes = secret_bytes[:BLOCK_SIZE]
else:
key_bytes.extend(secret_bytes)
key_bytes.extend([0 for x in range(0, BLOCK_SIZE - len(secret_bytes))])
if iv_key is None or len(iv_key) == 0:
cipher = des(key_bytes, ECB, None, padmode=PAD_PKCS5)
return cipher
else:
iv_bytes = iv_key.encode(encoding="utf-8")
iv_key_bytes = []
if len(iv_bytes) >= BLOCK_SIZE:
iv_key_bytes = iv_bytes[:BLOCK_SIZE]
else:
iv_key_bytes.extend(iv_bytes)
iv_key_bytes.extend([0 for x in range(0, BLOCK_SIZE - len(iv_bytes))])
cipher = des(key_bytes, CBC, iv_key_bytes, padmode=PAD_PKCS5)
return cipher
def des_encrypt(value, secret_key, iv_key):
cipher = init_cipher(secret_key, iv_key)
buffer = value.encode(encoding="utf-8")
buffer = cipher.encrypt(buffer)
return buffer.hex()
def des_decrypt(value, secret_key, iv_key):
cipher = init_cipher(secret_key, iv_key)
buffer = bytes.fromhex(value)
buffer = cipher.decrypt(buffer)
return buffer.decode("utf-8")
text = "hello word"
key = "0b9dcbe0f7101691"
iv = "abcdefg"
encryptText1 = des_encrypt(text, key, iv)
print("【", text, "】经过【DES-CBC】加密后:", encryptText1)
decryptText1 = des_decrypt(encryptText1, key, iv)
print("【", encryptText1, "】经过【DES-CBC】解密后:", decryptText1)
encryptText2 = des_encrypt(text, key, None)
print("【", text, "】经过【DES-ECB】加密后:", encryptText2)
decryptText2 = des_decrypt(encryptText2, key, None)
print("【", encryptText2, "】经过【DES-ECB】解密后:", decryptText2)
from Crypto.Cipher import DES
from Crypto.Util.Padding import pad, unpad
import binascii
def encrypt_des(key, plaintext):
des = DES.new(key, DES.MODE_ECB)
padded_data = pad(plaintext.encode(), DES.block_size)
encrypted_data = des.encrypt(padded_data)
return binascii.hexlify(encrypted_data).decode()
def decrypt_des(key, ciphertext):
des = DES.new(key, DES.MODE_ECB)
encrypted_data = binascii.unhexlify(ciphertext)
decrypted_data = des.decrypt(encrypted_data)
return unpad(decrypted_data, DES.block_size).decode()
if __name__ == "__main__":
key = b"0b9dcbe0"
ori_str = "2dc57d06632e63af482e84c849feceb0f4aaba5474f87ae163522d3937322106c994b4f398e7c5d7482e84c849feceb0f4aaba5474f87ae18141a5a4afd9a901a1e2cf5f3a38a84f5f33c385ae264a73a7b5f93e5d2a07380eb8d48d512759d146e4e49ed04fbd8241faee61d5d8318c0054d22bb89d13b688c8b84df3c7357ea00c320d2c5a428b08d4b63f0cb5c271db3305a8a53114744fb4b5afb3b0055da00c320d2c5a428b08d4b63f0cb5c271c734619da7b41b8a7cb8ade5b5c4b652c9f56998b95d3318e753dd1336c7269f86d390541f19fee5f6278cc971f23499bcaa607dc7dab6c8669a73f3db89b4555d25e57f0d19d8bca133e6688b1a8faad3d195ad951e5a464428242a371422f00bf122490e487233a133e6688b1a8faad3d195ad951e5a464abd5cb39b308a2900e7998af2b1411d5c4d302dbb225fa0093768ca083c109707c1b4aac74d34c8dbccc957129fb0fe2c8dcd688fcbc691608532e495a91097704a72253dab6f711f5e44ec3c1e7debb8bd7379806dcf0d95f7efd1b6add462704a72253dab6f711f5e44ec3c1e7deb6c3151187591f9091cf5a50d7ee9df14acde2e313b24c909d63fc2821d421445d455001188309c75ba0e86a3d8e795003977bf34b358eb3a2c5318e76636da925f59d248d7c2ec63482e84c849feceb0f4aaba5474f87ae163522d3937322106c994b4f398e7c5d7482e84c849feceb0f4aaba5474f87ae18141a5a4afd9a901a1e2cf5f3a38a84f5f33c385ae264a73a7b5f93e5d2a0738c0402b9b07ff236f46e4e49ed04fbd8214ce35f41fd98ebaf896f215f815437e834c83e356f92d5e"
dec_str = encrypt_des(key, 'hello word')
print("decStr:", dec_str)
dec_str = decrypt_des(key, ori_str)
print("decStr:", dec_str)
from Crypto.Cipher import DES
import binascii
def des_encrypt(des_key, plaintext):
"""
DES 加密
:param des_key: 密钥,字节形式
:param plaintext: 原始字符串
:return: 加密后字符串,16进制
"""
des_cipher = DES.new(des_key, DES.MODE_ECB)
plaintext = plaintext + '\0' * (8 - len(plaintext) % 8)
encrypted_data = des_cipher.encrypt(plaintext.encode())
return binascii.hexlify(encrypted_data).decode()
def des_decrypt(des_key, ciphertext):
"""
DES 解密
:param des_key: 密钥,字节形式
:param ciphertext: 加密后的字符串,16进制
:return: 解密后的字符串
"""
des_cipher = DES.new(des_key, DES.MODE_ECB)
decrypted_data = des_cipher.decrypt(binascii.unhexlify(ciphertext))
return decrypted_data.rstrip(b'\0').decode()
def main():
key = b'0b9dcbe0'
print("Key:", key)
ori_str = 'hello world'
enc_str = des_encrypt(key, ori_str)
print("Encrypted data:", enc_str)
enc_str = "2dc57d06632e63af482e84c849feceb0f4aaba5474f87ae163522d3937322106c994b4f398e7c5d7482e84c849feceb0f4aaba5474f87ae18141a5a4afd9a901a1e2cf5f3a38a84f5f33c385ae264a73a7b5f93e5d2a07380eb8d48d512759d146e4e49ed04fbd8241faee61d5d8318c0054d22bb89d13b688c8b84df3c7357ea00c320d2c5a428b08d4b63f0cb5c271db3305a8a53114744fb4b5afb3b0055da00c320d2c5a428b08d4b63f0cb5c271c734619da7b41b8a7cb8ade5b5c4b652c9f56998b95d3318e753dd1336c7269f86d390541f19fee5f6278cc971f23499bcaa607dc7dab6c8669a73f3db89b4555d25e57f0d19d8bca133e6688b1a8faad3d195ad951e5a464428242a371422f00bf122490e487233a133e6688b1a8faad3d195ad951e5a464abd5cb39b308a2900e7998af2b1411d5c4d302dbb225fa0093768ca083c109707c1b4aac74d34c8dbccc957129fb0fe2c8dcd688fcbc691608532e495a91097704a72253dab6f711f5e44ec3c1e7debb8bd7379806dcf0d95f7efd1b6add462704a72253dab6f711f5e44ec3c1e7deb6c3151187591f9091cf5a50d7ee9df14acde2e313b24c909d63fc2821d421445d455001188309c75ba0e86a3d8e795003977bf34b358eb3a2c5318e76636da925f59d248d7c2ec63482e84c849feceb0f4aaba5474f87ae163522d3937322106c994b4f398e7c5d7482e84c849feceb0f4aaba5474f87ae18141a5a4afd9a901a1e2cf5f3a38a84f5f33c385ae264a73a7b5f93e5d2a0738c0402b9b07ff236f46e4e49ed04fbd8214ce35f41fd98ebaf896f215f815437e834c83e356f92d5e"
dec_str = des_decrypt(key, enc_str)
print("Decrypted data:", dec_str)
if __name__ == "__main__":
main()