简体   繁体   English

在Bouncy Castle中使用“ RSA / ECB / PKCS7Padding”

[英]Using “RSA/ECB/PKCS7Padding” with Bouncy Castle

I tried to use "RSA/ECB/PKCS7Padding" for encryption. 我尝试使用“ RSA / ECB / PKCS7Padding”进行加密。 It is not supported in JCE. JCE不支持它。 So I downloaded Bouncy Castle but it seems that Bouncy Castle also does not support this transformation. 因此,我下载了Bouncy Castle,但看来Bouncy Castle也不支持此转换。 The following codes: 以下代码:

Security.insertProviderAt(new BouncyCastleProvider(), 1);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS7Padding");

throws

Caused by: java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/ECB/PKCS7Padding
    at javax.crypto.Cipher.getInstance(Cipher.java:524)
    ....
Caused by: javax.crypto.NoSuchPaddingException: Unsupported padding PKCS7Padding
    at sun.security.pkcs11.P11RSACipher.engineSetPadding(P11RSACipher.java:129)
    at javax.crypto.Cipher$Transform.setModePadding(Cipher.java:360)
    at javax.crypto.Cipher.getInstance(Cipher.java:517)
    ... 4 more

Am I doing it correctly? 我做得对吗?

TIA. TIA。

It's not possible to implement PKCS#7 padding as described in RFC2315 section 10.3 note 2 for all RSA key sizes: 对于所有RSA密钥大小,无法实现RFC2315第10.3节注释2中所述的PKCS#7填充:

Some content-encryption algorithms assume the input length is a multiple of k octets, where k > 1, and let the application define a method for handling inputs whose lengths are not a multiple of k octets. 一些内容加密算法假定输入长度是k个八位组的倍数,其中k> 1,并让应用程序定义一种用于处理长度不是k个八位组的倍数的输入的方法。 For such algorithms, the method shall be to pad the input at the trailing end with k - (l mod k) octets all having value k - (l mod k), where l is the length of the input. 对于这种算法,该方法应在尾端用k-(l mod k)个八位字节填充输入,所有八位字节均具有值k-(l mod k),其中l是输入的长度。 In other words, the input is padded at the trailing end with one of the following strings 换句话说,在输入的末尾用以下字符串之一填充

and specifically: 特别是:

This padding method is well-defined if and only if k < 256; 当且仅当k <256时,才可以很好地定义此填充方法。 methods for larger k are an open issue for further study. 较大k的方法是一个有待进一步研究的问题。

Which means that you could implement this for RSA with 2048-bit keys, but already 4096-bit keys are too much for arbitrary data. 这意味着您可以使用2048位密钥对RSA实施此操作,但是对于任意数据来说,4096位密钥已经太多了。 This is why PKCS7Padding is reserved for block ciphers where each block is usually between 128 and 256-bit. 这就是为什么PKCS7Padding保留给块密码的原因,其中每个块通常在128位和256位之间。 This is also why libraries don't generally support this sort of combination. 这也是为什么库通常不支持这种组合的原因。

The above specification contains everything you need to know to implement the PKCS#7 padding scheme. 上面的规范包含实现PKCS#7填充方案所需的所有知识。 When you do, you will pad your data using it and then encrypt with RSA/ECB/NoPadding . 完成后,将使用它填充数据,然后使用RSA/ECB/NoPadding加密。 You might run into the problem that although the padded plaintext and the key size are the same, the encryption doesn't work. 您可能会遇到这样的问题:尽管填充的纯文本和密钥大小相同,但是加密不起作用。 That's because your padded plaintext might still exceed the key. 那是因为您填充的纯文本可能仍然超出密钥。 You may need to add a zero byte to the front of the plaintext and only pad (0x00 + plaintext) so that the most significant bits are unset. 您可能需要在纯文本的开头添加一个零字节,并且仅填充(0x00 +纯文本),以便未设置最高有效位。

Even if such a thing would exist, using RSA with PKCS#7 padding or a scheme directly derived from PKCS#7 padding would be insecure (or, to be more precise, it would not be CPA-secure). 即使存在这种情况,使用带有PKCS#7填充的RSA或直接从PKCS#7填充派生的方案也将是不安全的(或更确切地说,它不是CPA安全的)。

What is infinitely more likely is that the client doesn't require PKCS#7 padding but that the encrypted data should be contained in a CMS message format . 无限可能的是,客户端不需要PKCS#7 填充,但是加密的数据应以CMS消息格式包含。 PKCS#7 is a predecessor of this format, the PKCS#7 padding is only a small part of this specification. PKCS#7是此格式的前身,PKCS#7填充仅是此规范的一小部分。

Bouncy Castle contains an implementation of this format: 有弹性的城堡包含此格式的实现:

Generators/Processors for S/MIME and CMS (PKCS7/RFC 3852). S / MIME和CMS(PKCS7 / RFC 3852)的生成器/处理器。

currently contained in the bcpkix* JAR files. 当前包含在bcpkix* JAR文件中。

To summaries my commenta, According to the java7 doc, There list of standard algorithms must be supported by every implementation of JCE Cipher API . 总结一下我的评论,根据java7 doc,JCE Cipher API的每个实现都必须支持标准算法列表。 Bouncy castle support additional algorithms as well. 弹力城堡也支持其他算法。 I use " AES/ECB/PKCS7Padding " in my application. 我在应用程序中使用“ AES/ECB/PKCS7Padding ”。 You could find supported algorithms by bouncy castle in algorithm section and "RSA/ECB/PKCS1Padding" or "RSA/NONE/PKCS1Padding" should work. 您可以在算法部分中找到有弹性城堡支持的算法,并且"RSA/ECB/PKCS1Padding""RSA/NONE/PKCS1Padding"应该起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有弹性的城堡PKCS7填充 - Bouncy Castle PKCS7 padding C# Bouncy Castle 中的 RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING - 对于 RSA 密码输入太大 - RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING in C# Bouncy Castle - input too large for RSA cipher 使用弹性城堡和python PKCS1-OAEP的Java RSA加密 - java RSA encryption using bouncy castle and python PKCS1-OAEP 使用PHP OpenSSL将Java AES / ECB / PKCS7Padding /代码转换为PHP - Converting Java AES/ECB/PKCS7Padding/ code to PHP using PHP OpenSSL 使用CBC和PKCS5 / 7的AES加密,使用Java / android中的充气城堡进行填充 - AES Encryption using CBC and PKCS5/7Padding using bouncy castle in java/android java.security.NoSuchAlgorithmException:找不到任何支持 AES/ECB/PKCS7PADDING 的提供商 - java.security.NoSuchAlgorithmException:Cannot find any provider supporting AES/ECB/PKCS7PADDING 使用RSA / ECB / PKCS1Padding在Java中进行加密无法在.Net中解密 - Encrypted in Java using RSA/ECB/PKCS1Padding unable to decrypt in .Net 如何使用算法 RSA/ECB/PKCS1Padding 通过加密字符串的 node.js 进行加密 - How to encrypt through node.js of encrypted string using algorithm RSA/ECB/PKCS1Padding 使用RSA / ECB / PKCS1padding的Android加密问题。 它在Java和Android中给出不同的结果 - Issue in Android Encryption using RSA/ECB/PKCS1padding. It gives different results in Java and Android 使用模量和指数的C#中的RSA / ECB / PKCS1填充解密 - RSA/ECB/PKCS1Padding Decryption in C# using Modulus and Exponent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM