0

I've trying to encrypt my data using asymmetric encryption. I've chosen MD5 as the algorithm. PS: I know about MD5 collisions.

I'm following this blog to encrypt and decrypt my data. This uses SHA256. I'm able to run the code successfully with SHA1, SHA512 as well, but not with MD5.

Whenever, I change the algorithm to MD5, it says

cryptography.exceptions.UnsupportedAlgorithm: This combination of padding and hash algorithm is not supported by this backend.

Which padding should be used to encrypt the data with MD5?

My Code:-

# ########## Encrypting and decrypting ##########
message = b'encrypt me!'
encrypted = public_key.encrypt(
    message,
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.MD5()),
        algorithm=hashes.MD5(),
        label=None
    )
)
original_message = private_key.decrypt(
    encrypted,
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.MD5()),
        algorithm=hashes.MD5(),
        label=None
    )
)
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • Usually there is only a specific set of paddings supported (such as pkcs1.5, oaep+mgf1-sha-1, oaep+mgf1-sha256). I am not sure for this specific library. The encryption is RSA, the hash is used for the mask (oaep) generation. So you should not say "encrypt with MD5". – gusto2 Mar 11 '19 at 07:43

1 Answers1

0

Actually, MD5 is (an old) hash algorithm, it is not an encryption algorithm. A hash is used to get a sort of "checksum" for a given text (or data byte array). That "checksum" as a fix length, whatever is the size of the text you hash.

In cryptography, you typically may use hash function to get a private (symmetric) key from a passphrase or you may encrypt a hash with a private key : this is a digital signature.

Vincent GODIN
  • 467
  • 4
  • 8
  • Similar is the case with SHA family. – Praful Bagai Mar 11 '19 at 07:40
  • You're right. So my answer won't help, sorry. Actually the padding...() is used to add a signature at the end of the encrypted message... if you only want your message to be encrypted but not signed (for signature is not allowed with md5) I guess you can ignore it. – Vincent GODIN Mar 11 '19 at 13:57