使用 DES/3DES 和 python

2022-01-10 00:00:00 python cryptography 3des

问题描述

在 python 中使用 des/3des 进行加密/解密的最佳模块/package 是什么.有人可以提供在 python 上使用 des/3des 加密数据的示例吗?

what is the best module /package in python to use des /3des for encryption /decryption. could someone provide example to encrypt data with des/3des on python.


解决方案

pyDes 可用于 DES 和 3DES.示例用法:

pyDes can be used for both, DES and 3DES. Sample usage:

from pyDes import *

data = "Please encrypt my data"
k = des("DESCRYPT", CBC, "", pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
print "Encrypted: %r" % d
print "Decrypted: %r" % k.decrypt(d)
assert k.decrypt(d, padmode=PAD_PKCS5) == data

<小时>

另一种方法是 Chillkat Python 加密库,它支持很多加密算法(包括 DES 和 3DES),但它不是免费的.示例用法:

crypt.put_CryptAlgorithm("des")
crypt.put_CipherMode("cbc")
crypt.put_KeyLength(64)
crypt.put_PaddingScheme(0)
crypt.put_EncodingMode("hex")
ivHex = "0001020304050607"
crypt.SetEncodedIV(ivHex,"hex")
keyHex = "0001020304050607"
crypt.SetEncodedKey(keyHex,"hex")
encStr = crypt.encryptStringENC("The quick brown fox jumps over the lazy dog.")
print encStr
decStr = crypt.decryptStringENC(encStr)
print decStr

<小时>

无论如何,我希望您知道,如今 DES 和 3DES 都被认为是不安全的,有许多更好的选择(如果您想坚持标准,首先选择 AES,或者 Twofish、河豚等……)


Anyway, I hope that you are aware that neither DES nor 3DES are considered paritcularly safe nowadays, there are many better alternatives (AES in the first place if you want to stick to standards, or Twofish, Blowfish, etc...)

相关文章