加密(模式和填充) [英] Encryption (mode and padding)

查看:144
本文介绍了加密(模式和填充)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是编写一个涉及加密的小型Java控制台应用程序。我不熟悉加密,所以我不得不先做一些阅读。到目前为止,给出的高级要求是使用AES-256来生成加密文件的一次性密钥。



之后,收件人的公钥( RSA-2048)应用于加密AES-256一次性密钥。加密的文件和加密的一次性AES-256密钥将被压缩并发送给收件人。



根据我从阅读中了解的加密和解密,除了算法(RSA,AES等),还有一些叫做模式和填充的东西。例如,以下代码将RSA指定为算法,ECB模式和PKCS1Padding。

 密码密码= Cipher.getInstance(RSA / ECB / PKCS1Padding); 

同样的算法,模式和填充必须用于加密和解密。因此,我是否可以向用户询问他们想要什么模式和填充?



我注意到密码密码= Cipher.getInstance(RSA)似乎使用ECB的默认模式和PKCS1Padding的填充,所以这行代码与上述相同。那么可以假设ECB模式和PKCS1Padding模式将用作默认的RSA-2048?

解决方案

不,for发送消息您应该使用较新的OAEP方案,因为具有PKCS#1 v1.5的RSA可能容易受到 Bleichenbacher攻击。然而,完全有可能甚至有人要求RSA混合加密从未听说过这种攻击。一般来说,PKCS#1 v1.5填充仍然被用作默认值。



除非唯一的用户是学生,否则您不应该期望用户为您做出安全决策加密(并了解上面的攻击)。一般来说,安全性不应太多地依赖于教育用户。



我个人一定会向请求者询问填充。您还应该检查它们是否期望对称加密的认证(MAC,HMAC,认证密码或签名)。如果他/她不能回答这个问题,他们可能不太了解加密。



我目前不会考虑你所提供的要求是完整的(虽然for



笔记



RSA / ECB / PKCS1Padding实际上并不实现ECB模式加密。它应该被称为RSA /无/ PKCS1Padding,因为它只能用于加密单个明文块(或者确实是秘密密钥)。这只是Sun / Oracle的命名错误。



还有一种称为RSA-KEM的混合加密模式应至少与RSA OAEP一样安全,但它在Java SE中尚未实现。



AES-256本身不应用于生成一次性密钥。您应该使用 KeyGenerator 的实例生成AES-256一次性密钥(这可能是一个命名混乱,因为 KeyGenerator 本身不使用 AES,它为AES创建密钥


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 level requirements given is that AES-256 should be used to generate a one-time key to encrypt a file.

After that, the recipient's public key (RSA-2048) should be used to encrypt that AES-256 one-time key. The encrypted file and the encrypted one-time AES-256 key will then be zipped up and send to recipient.

From what I understand about encryption and decryption from reading up, apart from the algorithm (RSA, AES, etc), there is also things called mode and padding. For example, the following code specified RSA as algorithm, ECB mode and PKCS1Padding.

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

The same algorithm, mode and padding must be used in the encryption and decryption. Therefore, is it reasonable for me to go ask the users on what mode and padding they want?

I notice that Cipher cipher = Cipher.getInstance("RSA") seems to use the default mode of ECB and padding of PKCS1Padding, and so this line of code is the same as the above. So is it ok to assume that ECB mode and PKCS1Padding mode will be used for RSA-2048 as default?

解决方案

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).

这篇关于加密(模式和填充)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆