DES 密码 pyca/cryptography (PBEWithMD5AndDES)
DES cipher with pyca/cryptography (PBEWithMD5AndDES)
为了支持一些遗留应用程序,我需要在 python 中实现 PBEWithMD5AndDES
(RFC2898 Section 6.1)。我知道这是不安全的,已弃用,不应再使用。但遗憾的是,这是我的要求。
我已经有一个使用 PyCrypto
/PyCryptodome
的工作版本,但我需要在我们代码的其他部分引入 PyCryptodome
as additional dependency to the project which is something I want to avoid. As we are already using pyca/cryptography
我更喜欢这个库而不是 PyCrypto(dome)
。然而,由于 PBEWithMD5AndDES
的性质,我需要 DES 加密支持,但据我所知,pyca/cryptography
仅支持三重 DES (3DES)。
有没有办法使用 pyca/cryptography
(单个)DES 加密某些内容?基本上我需要用 pyca/cryptography
:
中的内容替换 Crypto.Cipher.DES
中的以下用法
key, init_vector = _pbkdf1_md5(a_password, a_salt, a_iterations)
cipher = DES.new(key, DES.MODE_CBC, init_vector)
encrypted_message = cipher.encrypt(encoded_message)
**更新**:
感谢@SquareRootOfTwentyThree,我得到了这个:
(key, init_vector) = _pbkdf1_md5(a_password, a_salt, a_iterations)
cipher = Cipher(algorithms.TripleDES(key), modes.CBC(init_vector), default_backend())
encryptor = self.cipher.encryptor()
encrypted = encryptor.update(encoded_message)
encryptor.finalize()
def _pbkdf1_md5(a_password, a_salt, a_iterations):
digest = Hash(MD5(), default_backend())
digest.update(a_password)
digest.update(a_salt)
key = None
for i in range(a_iterations):
key = digest.finalize()
digest = Hash(MD5(), default_backend())
digest.update(key)
digest.finalize()
return key[:8], key[8:16]
Is there a way to (single) DES encrypt something using pyca/cryptography?
是的,只需将一个 8 字节的密钥传递给 cryptography.hazmat.primitives.ciphers.algorithms.TripleDES
。这将为三重 DES 中的每个 DES 转换使用相同的密钥。
Triple-DES 也称为 DES-EDE,表示先加密、解密再加密。如果您对每个使用相同的密钥,那么其中一个加密/解密对将产生身份函数,只留下一个 DES 加密。
请注意,并非所有三重 DES 实现都将接受单个密钥(因为通常存在单个 DES),但这个可以:
The secret key. This must be kept secret. Either 64
, 128
, or 192
bits long. DES only uses 56
, 112
, or 168
bits of the key as there is a parity byte in each component of the key. Some writing refers to there being up to three separate keys that are each 56
bits long, they can simply be concatenated to produce the full key.
尽管我必须承认,您必须了解三重 DES 的工作原理才能理解该文本。
另请注意,针对单个 DES 的 DES-EDE 实现目前尚未优化,它将执行所有三个操作,即使其中两个操作相互抵消。
为了支持一些遗留应用程序,我需要在 python 中实现 PBEWithMD5AndDES
(RFC2898 Section 6.1)。我知道这是不安全的,已弃用,不应再使用。但遗憾的是,这是我的要求。
我已经有一个使用 PyCrypto
/PyCryptodome
的工作版本,但我需要在我们代码的其他部分引入 PyCryptodome
as additional dependency to the project which is something I want to avoid. As we are already using pyca/cryptography
我更喜欢这个库而不是 PyCrypto(dome)
。然而,由于 PBEWithMD5AndDES
的性质,我需要 DES 加密支持,但据我所知,pyca/cryptography
仅支持三重 DES (3DES)。
有没有办法使用 pyca/cryptography
(单个)DES 加密某些内容?基本上我需要用 pyca/cryptography
:
Crypto.Cipher.DES
中的以下用法
key, init_vector = _pbkdf1_md5(a_password, a_salt, a_iterations)
cipher = DES.new(key, DES.MODE_CBC, init_vector)
encrypted_message = cipher.encrypt(encoded_message)
**更新**:
感谢@SquareRootOfTwentyThree,我得到了这个:
(key, init_vector) = _pbkdf1_md5(a_password, a_salt, a_iterations)
cipher = Cipher(algorithms.TripleDES(key), modes.CBC(init_vector), default_backend())
encryptor = self.cipher.encryptor()
encrypted = encryptor.update(encoded_message)
encryptor.finalize()
def _pbkdf1_md5(a_password, a_salt, a_iterations):
digest = Hash(MD5(), default_backend())
digest.update(a_password)
digest.update(a_salt)
key = None
for i in range(a_iterations):
key = digest.finalize()
digest = Hash(MD5(), default_backend())
digest.update(key)
digest.finalize()
return key[:8], key[8:16]
Is there a way to (single) DES encrypt something using pyca/cryptography?
是的,只需将一个 8 字节的密钥传递给 cryptography.hazmat.primitives.ciphers.algorithms.TripleDES
。这将为三重 DES 中的每个 DES 转换使用相同的密钥。
Triple-DES 也称为 DES-EDE,表示先加密、解密再加密。如果您对每个使用相同的密钥,那么其中一个加密/解密对将产生身份函数,只留下一个 DES 加密。
请注意,并非所有三重 DES 实现都将接受单个密钥(因为通常存在单个 DES),但这个可以:
The secret key. This must be kept secret. Either
64
,128
, or192
bits long. DES only uses56
,112
, or168
bits of the key as there is a parity byte in each component of the key. Some writing refers to there being up to three separate keys that are each56
bits long, they can simply be concatenated to produce the full key.
尽管我必须承认,您必须了解三重 DES 的工作原理才能理解该文本。
另请注意,针对单个 DES 的 DES-EDE 实现目前尚未优化,它将执行所有三个操作,即使其中两个操作相互抵消。