Encryption (mode and padding)

前端 未结 2 720
傲寒
傲寒 2020-12-30 16:18

I was tasked with writing a small Java console application that involves encryption. I am not familiar with encryption, so I had to do some reading up first. So far the high

相关标签:
2条回答
  • 2020-12-30 16:46

    You do not want to be asking the users about padding or block mode. What I think that you need to do is:

    Generate a random 256bit key (make sure you use a cryptographic library to do this, with good seeding)

    Encrypt the file using AES256 with the key (DO NOT USE ECB, use CBC and possibly padding if you want to slightly conceal the exact length of the file)

    Encrypt the key using RSA-2048 (here block mode from a security point of view is irrelevant because the key is smaller than one block, as is padding because the key is a known length. I would use ECB)

    Your protocol gives no guarantees of who sent the file or integrity of the file. This is really important to think about, consider signing.

    Unfortunately when implementing anything to do with crypto, you are extremely likely to get things wrong. Definitely use the Java libraries and if you can find higher level trusted libraries doing things closer to exactly what you want, consider using those.

    0 讨论(0)
  • 2020-12-30 16:51

    No, for sending messages you should use the newer OAEP scheme, as RSA with PKCS#1 v1.5 may be vulnerable to the Bleichenbacher attack. It is however entirely probable and even likely that somebody requesting RSA hybrid encryption has never heard of the attack. In general PKCS#1 v1.5 padding is still used as the default.

    You should never expect users to make security decisions for you, unless the only users are students of cryptography (and know about the attack above). Security in general should not rely too much on educating users.

    Personally I would certainly asking the requester about the padding. You should also check if they would expect authentication (MAC, HMAC, authenticated cipher or a signature) for the symmetric encryption. If he/she cannot answer the question they may not know that much about encryption.

    I would not currently consider the requirements you have been given to be complete (although "for learning purposes" can be one hell of an excuse).

    Notes

    "RSA/ECB/PKCS1Padding" actually doesn't implement ECB mode encryption. It should have been called "RSA/None/PKCS1Padding" as it can only be used to encrypt a single block of plaintext (or, indeed a secret key). This is just a naming mistake of Sun/Oracle.

    There is also a hybrid encryption mode called RSA-KEM that should be at least as secure as RSA OAEP, but it has not been implemented within Java SE.

    AES-256 itself should not be used to "generate a one time key". You should use an instance of KeyGenerator generate an AES-256 one time key (this is likely a bit of naming confusion because the KeyGenerator itself does not use AES, it creates keys for AES).

    0 讨论(0)
提交回复
热议问题