解决方案

福利推荐-办理成功后可加微信领取20红包

java PKCS7Padding 加密Cannot find any provider supporting AES/CBC/PKCS7Padding 解决办法

2023-01-31 20:10:25 michael007js 438

在java中用aes256进行加密,但是发现java里面不能使用PKCS7Padding,而java中自带的是PKCS5Padding填充,那解决办法是,通过BouncyCastle组件来让java里面支持PKCS7Padding填充。


首先,maven导入org.bouncycastle:bcprov-ext-jdk16:1.46到项目中

然后,照抄下方即可


注意:如果出现Illegal key size 报错问题,请点击此处

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Base64;

import java.io.UnsupportedEncodingException;
import java.security.Security;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


class test {
 static String encode(String keyString, String stringToEncode) {
     if (keyString == null || keyString.length() == 0) {
         throw new NullPointerException("Please give Password");
     }
     if (stringToEncode == null || stringToEncode.length() == 0) {
         throw new NullPointerException("Please give text");
     }
     try {
         SecretKeySpec skeySpec = getKey(keyString);
         byte[] clearText = stringToEncode.getBytes("UTF8");
         final byte[] iv = new byte[16];
         Arrays.fill(iv, (byte) 0x00);
         IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
         /**
            * 这个地方调用BouncyCastleProvider
            *让java支持PKCS7Padding
            */
         Security.addProvider(new BouncyCastleProvider());
         Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
         cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);

         return new String(Base64.encode(cipher.doFinal(clearText)));

     } catch (Exception e) {
         e.printStackTrace();
     }
     return "";
 }

 static String decode(String password, String text) throws NullPointerException {
     if (password == null || password.length() == 0) {
         throw new NullPointerException("Please give Password");
     }
     if (text == null || text.length() == 0) {
         throw new NullPointerException("Please give text");
     }
     try {
         SecretKey key = getKey(password);
         final byte[] iv = new byte[16];
         Arrays.fill(iv, (byte) 0x00);
         IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
         /**
            * 这个地方调用BouncyCastleProvider
            *让java支持PKCS7Padding
            */
         Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
         Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
         cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
         byte[] decrypedValueBytes = (cipher.doFinal(Base64.decode(text)));

         return new String(decrypedValueBytes);

     } catch (Exception e) {
         e.printStackTrace();
     }
     return "";
 }

 private static SecretKeySpec getKey(String password) throws UnsupportedEncodingException {
     int keyLength = 256;
     byte[] keyBytes = new byte[keyLength / 8];
     Arrays.fill(keyBytes, (byte) 0x0);

     byte[] passwordBytes = password.getBytes("UTF-8");
     int length = Math.min(passwordBytes.length, keyBytes.length);
     System.arraycopy(passwordBytes, 0, keyBytes, 0, length);
     return new SecretKeySpec(keyBytes, "AES");
 }

 public static void main(String args[]) {
     String encodeStr = encode("我是秘钥", "我是等待加密的文本");
     System.out.println("encoder:" + encodeStr);
     String decodeStr = decode("我是秘钥", encodeStr);
     System.out.println("decoder:" + decodeStr);

 }
}



首页
关于博主
我的博客
搜索