简体   繁体   English

java.security.NoSuchAlgorithmException:找不到任何支持 AES/ECB/PKCS7PADDING 的提供商

[英]java.security.NoSuchAlgorithmException:Cannot find any provider supporting AES/ECB/PKCS7PADDING

I was trying to encrypt data using AES algorithm.我试图使用 AES 算法加密数据。 However, with the following exception has occurred.但是,发生了以下异常。

java.security.NoSuchAlgorithmException:
    Cannot find any provider supporting AES/ECB/PKCS7PADDING

Someone know a solution to this issue?有人知道这个问题的解决方案吗? My JDK's version is 1.7.我的 JDK 版本是 1.7。

You don't want to specify PKCS#7 padding for block cipher use.您不想为分组密码使用指定 PKCS#7 填充。 You want to specify PKCS#5 padding.您想指定 PKCS#5 填充。 PKCS#5 is specified for use with block ciphers while PKCS#7 is not (it's use for different places like in S/MIME). PKCS#5 被指定用于分组密码,而 PKCS#7 不是(它用于不同的地方,比如在 S/MIME 中)。 I will point out that PKCS#5 and PKCS#7 actually specify exactly the same type of padding (they are the same!), but it's called #5 when used in this context.我将指出 PKCS#5 和 PKCS#7 实际上指定了完全相同的填充类型(它们是相同的!),但在此上下文中使用时称为 #5。 :) :)

So, instead of "AES/ECB/PKCS7PADDING" , you want "AES/ECB/PKCS5PADDING" .因此,而不是"AES/ECB/PKCS7PADDING"想要"AES/ECB/PKCS5PADDING" This is a cipher implementation that every implementation of the Java platform is required to support.这是一个密码实现,Java 平台的每个实现都需要支持。 See the documentation of the Cipher class for more details.有关更多详细信息,请参阅Cipher文档

如果你想使用 AES/ECB/PKCS7Padding 那么充气城堡将支持 ht tp://www.bouncycastle.org/specifications.html

For a very comprehensive explanation of the issue that includes the text of the PKCS#5 and PKCS#7 cryptographic standards, please take a look here .有关包括 PKCS#5 和 PKCS#7 加密标准文本在内的问题的非常全面的解释,请查看此处


PKCS#5 padding means padding 1 to 8 bytes. PKCS#5 padding 表示填充 1 到 8 个字节。 The padding bytes themselves contain the amount of padding bytes encoded as a byte.填充字节本身包含编码为字节的填充字节量。 PKCS#5 padding was specified for DES, but it would be suitable for any block cipher with a block size of 8 bytes.为 DES 指定了 PKCS#5 填充,但它适用于块大小为 8 字节的任何块密码。

Now the DES specifications and even the PKCS#5 specification for password based encryption precede and Java by quite a long time.现在 DES 规范甚至基于密码的加密的 PKCS#5 规范早于 Java。 AES was only standardized in 2002, long after Java and even Java 2 was introduced. AES 直到 2002 年才被标准化,早在 Java 甚至 Java 2 被引入之后。 So (triple) DES and PKCS#5 padding was integrated into Java before AES made its appearance.因此,在 AES 出现之前,(三重)DES 和 PKCS#5 填充已集成到 Java 中。

When Java - or more precisely, the Sun JCE provider - gained AES functionality it required a padding method for a block size of 16 bytes.当 Java——或者更准确地说,Sun JCE 提供者——获得 AES 功能时,它需要一个 16 字节的块大小的填充方法。 PKCS#7 specifies this padding method that is identical to PKCS#5 padding , except that it is defined for block sizes of 2 to 255 bytes (the maximum value of a byte if it encodes a zero based unsigned integer). PKCS#7 指定了与 PKCS#5 padding 相同的填充方法,除了它是为 2 到 255 个字节的块大小定义的(如果它编码基于零的无符号整数,则为字节的最大值)。 However, the padding method was already there;但是,填充方法已经存在; it was named "PKCS5Padding" .它被命名为"PKCS5Padding" So instead of introducing a new name, "PKCS5Padding" was simply re-used.因此,没有引入新名称, "PKCS5Padding"简单地重新使用了"PKCS5Padding"

By now the Sun provider should really support "PKCS7Padding" as PKCS#5 padding is simply incorrect.到目前为止,Sun 提供者应该真正支持"PKCS7Padding"因为 PKCS#5 填充是完全不正确的。 It's not just a Java naming issue, it's an issue for any developer that tries to implement cryptographic protocols or port other applications to Java.这不仅仅是 Java 命名问题,对于任何试图实现加密协议或将其他应用程序移植到 Java 的开发人员来说,都是一个问题。 For now however, you should use "PKCS5Padding" instead of "PKCS7Padding" .但是现在,您应该使用"PKCS5Padding"而不是"PKCS7Padding"

Solution: Step1: Add bcprov-ext-jdk16-1.46.jar ( https://mvnrepository.com/artifact/org.bouncycastle/bcprov-ext-jdk16/1.46 ) to your project解决方案: Step1:将 bcprov-ext-jdk16-1.46.jar ( https://mvnrepository.com/artifact/org.bouncycastle/bcprov-ext-jdk16/1.46 ) 添加到您的项目中

Step2: Add line "Security.addProvider(new BouncyCastleProvider());" Step2:添加行“Security.addProvider(new BouncyCastleProvider());” to bebore init Cipher common进入 init Cipher common

Then, run the project, OK, successfully decrypted.然后,运行项目,OK,解密成功。

Please consider the following solution if the environment was Android and you were trying to read the Encrypted Private Key or Passphrase protected Private Key in PKCS8 format from the file.如果环境为Android ,并且您尝试从文件中读取 PKCS8 格式的加密私钥或密码保护私钥,请考虑以下解决方案。

The Exception that I received was as follows:我收到的异常如下:

W/System.err: org.bouncycastle.pkcs.PKCSException: unable to read encrypted data: 1.2.840.113549.1.5.13 not available: No provider found for 2.16.840.1.101.3.4.1.42
W/System.err:     at org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo.decryptPrivateKeyInfo(Unknown Source:60)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err:     at android.os.Looper.loop(Looper.java:223)
W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:67)
W/System.err: Caused by: org.bouncycastle.operator.OperatorCreationException: 1.2.840.113549.1.5.13 not available: No provider found for 2.16.840.1.101.3.4.1.42
W/System.err:     at org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder$1.get(Unknown Source:322)
W/System.err:     at org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo.decryptPrivateKeyInfo(Unknown Source:6)
W/System.err:   ... 8 more
W/System.err: Caused by: java.security.NoSuchAlgorithmException: No provider found for 2.16.840.1.101.3.4.1.42
W/System.err:     at javax.crypto.Cipher.createCipher(Cipher.java:736)
W/System.err:     at javax.crypto.Cipher.getInstance(Cipher.java:619)
W/System.err:     at org.bouncycastle.jcajce.util.DefaultJcaJceHelper.createCipher(Unknown Source:0)
W/System.err:     at org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder$1.get(Unknown Source:101)
W/System.err:   ... 9 more

The only solution that worked was to set the Provider as Bouncy Castle like below:唯一可行的解决方案是将 Provider 设置为 Bouncy Castle,如下所示:

InputDecryptorProvider decryptorProvider = new JceOpenSSLPKCS8DecryptorProviderBuilder()
    .setProvider(new BouncyCastleProvider())
    .build(mContext.getResources().getString(R.string.password).toCharArray());

The complete block code would look like this:完整的块代码如下所示:

privateKeyString = privateKeyString.replace("-----BEGIN ENCRYPTED PRIVATE KEY-----", "").replaceAll(System.lineSeparator(), "")
                .replace("-----END ENCRYPTED PRIVATE KEY-----", "");
byte[] keyStringBytes = DatatypeConverter.parseBase64Binary(privateKeyString);
ASN1Sequence asn1Sequence = ASN1Sequence.getInstance(keyStringBytes);
PKCS8EncryptedPrivateKeyInfo pkcs8EncryptedPrivateKeyInfo = new PKCS8EncryptedPrivateKeyInfo(
    EncryptedPrivateKeyInfo.getInstance(asn1Sequence));
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
InputDecryptorProvider decryptorProvider = new JceOpenSSLPKCS8DecryptorProviderBuilder()
    .setProvider(new BouncyCastleProvider())
    .build(mContext.getResources().getString(R.string.password).toCharArray());
PrivateKeyInfo privateKeyInfo = pkcs8EncryptedPrivateKeyInfo.decryptPrivateKeyInfo(decryptorProvider);
mPrivateKey = converter.getPrivateKey(privateKeyInfo);

暂无
暂无

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

相关问题 java.security.NoSuchAlgorithmException:找不到支持Blowfish / ECB / PKCS5Padding的任何提供程序 - java.security.NoSuchAlgorithmException: Cannot find any provider supporting Blowfish/ECB/PKCS5Padding java.security.NoSuchAlgorithmException:找不到任何支持AES的提供程序 - java.security.NoSuchAlgorithmException:cannot find any provider supporting AES java.security.NoSuchAlgorithmException:在 jar 文件中找不到任何支持 RSA/NONE/PKCS1Padding 的提供程序 - java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/NONE/PKCS1Padding in jar-file java.security.NoSuchAlgorithmException:AES / ECB / PKCS5Padding - java.security.NoSuchAlgorithmException: AES/ECB/PKCS5Padding junit java.security.NoSuchAlgorithmException:找不到任何支持的提供程序 - junit java.security.NoSuchAlgorithmException: Cannot find any provider supporting java.security.NoSuchAlgorithmException:提供者SunJCE不提供DES / ECB / NoPadding - java.security.NoSuchAlgorithmException: Provider SunJCE does not provide DES/ECB/NoPadding 使用PHP OpenSSL将Java AES / ECB / PKCS7Padding /代码转换为PHP - Converting Java AES/ECB/PKCS7Padding/ code to PHP using PHP OpenSSL 使用 AES/CBC/PKCS7Padding 的 JAVA 加密 - JAVA Encryption with AES/CBC/PKCS7Padding 使用 CBC 和 PKCS7Padding 的 Java AES 加密 - Java AES Encryption with CBC and PKCS7Padding RSA / NONE / PKCS1Padding给出错误,如java.security.NoSuchAlgorithmException - RSA/NONE/PKCS1Padding giving error as java.security.NoSuchAlgorithmException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM