java.security.nosucha,java.security.NoSuchAlgorithmException:找不到任何支持AES / ECB / PKCS7PADDING的提供程序...

I was trying to encrypt data using AES algorithm.

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.

解决方案

You don't want to specify PKCS#7 padding for block cipher use. You want to specify PKCS#5 padding. PKCS#5 is specified for use with block ciphers while PKCS#7 is not (it's use for different places like in 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. :)

So, instead of "AES/ECB/PKCS7PADDING", you want "AES/ECB/PKCS5PADDING". This is a cipher implementation that every implementation of the Java platform is required to support. See the documentation of the Cipher class for more details.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对不起,我之前提供的代码有误。AES/ECB/ZeroPadding是不被标准Java加密提供程序支持的一个加密算法。在Java标准库中,只支持PKCS5Padding作为填充模式。 以下是修正后的代码示例,使用AES/ECB/PKCS5Padding算法: ```java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class AESUtil { private static final String ALGORITHM = "AES"; private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding"; public static String encrypt(String plainText, String key) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } public static String decrypt(String encryptedText, String key) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes); } public static void main(String[] args) { try { String plainText = "Hello, World!"; String key = "ThisIsASecretKey"; // 密钥长度必须是16、24或32字节 String encryptedText = encrypt(plainText, key); System.out.println("Encrypted Text: " + encryptedText); String decryptedText = decrypt(encryptedText, key); System.out.println("Decrypted Text: " + decryptedText); } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,此示例使用AES/ECB/PKCS5Padding算法进行加解密,密钥长度必须是16、24或32字节。在示例中,明文为"Hello, World!",密钥为"ThisIsASecretKey"。运行代码后,会输出加密后的密文和解密后的明文。如果仍然出现问题,请确保使用的Java版本支持AES加密算法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值