手动实现RSA java。填补想法 [英] Manual implementation RSA java. Padding ideas
问题描述
我正在研究实现填充方案的可能性,以将我的纯文本分解成小文本,然后对其进行加密。
有没有关于如何填充的建议?
提前感谢
我很抱歉,我正在撰写一个答案,其中包含与divanov的答案基本相同的信息,但是添加所有我认为重要的小细节的编辑将是一个完整的重写的答案。
通常你不想直接对数据进行不对称加密,而是使用RSA交换用于对称加密数据的对称密钥。您可以这样做(来自Ferguson,Kohno和Schneier的加密工程的想法,一本可以全心全意推荐的书):
- 假设
l
是您的模数的位长度n
,生成l-1
位长随机数r
。使用RSA公钥加密r
- 使用加密散列函数生成对称密钥
k
出r
。我建议使用sha256:k = sha256(r)
- 使用适当的AES256加密块密码加密数据模式,如 CBC 。
此过程的优点是您根本无需关心RSA paddings(并且有很多可能会出错的东西)。请不要在解密之后检查 r
的结构,只要填入哈希函数,否则可能会打开自己超出范围的填充oracle攻击(类似于这一个)我的答案虽然如此。
请注意,对于真实世界的场景,您必须关心真实性的数据也是。加密最为充足的唯一常见用例是静止数据,即如果没有数据通过网络传输,您只关心数据的物理窃取。
I am implementing RSA manually on java (yes i know not the best idea but it is for university purposes). My algorithm is working good enough with small texts but when big texts come into consideration the algorithm breaks as the number of bits is superior than my key.
I am looking into the possibility to implement a padding scheme in order to fragment my plain text into small ones and then encrypt them.
Is there any suggestion on how to the padding?
Thanks in advance.
I'm sorry that I'm writing an answer that contains basically the same information as divanov's answer, but an edit to add all the little details that I think are important would be a complete rewrite of the answer.
Generally you don't want to asymmetrically encrypt your data directly, but instead use RSA to exchange a symmetric key that is used to encrypt your data symmetrically. You can do it like this (idea taken from "Cryptography Engineering" by Ferguson, Kohno and Schneier, a book I can wholeheartedly recommend):
- Assuming that
l
is the bit length of your modulusn
, generate anl-1
bit long random numberr
. Encryptr
with the RSA public key. - Use a cryptographic hash function to generate the symmetric key
k
out ofr
. I would advise the use of sha256:k=sha256(r)
- Encrypt you data with a block cipher like AES256 using a proper "mode" like CBC.
The advantage of this procedure is that you do not have to care about RSA paddings at all (and there is a lot of stuff that can go wrong with them). Please don't check the structure of r
after decrypting, though, and just stuff if into the hash function as you otherwise might open yourself up to padding oracle attacks (akin to this one) that are beyond the scope of my answer though.
Note that for a real world scenario you have to care about authenticity of the data, too. The only common use case where encryption is mostly enough is "data at rest", i.e. if no data is transmitted over the network and you only care about physical theft of your data.
这篇关于手动实现RSA java。填补想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!