发布到生产javax.crypto.BadPaddingException:Decryption error
原来读取证书的方法
public static byte[] getKeyFromFile(String pubCerPath) throws IOException { //InputStream inputStreams= BocUtils.class.getClassLoader().getResourceAsStream("20200226160609hx.der"); //InputStream inputStream1 = Files.newInputStream(Paths.get("E:\\work\\jingzhi\\cert\\20200226160609hx.der")); InputStream inputStream2 = BocUtils.class.getClassLoader().getResourceAsStream(pubCerPath); try (InputStream inputStream = inputStream2;) { byte[] reads = new byte[inputStream.available()]; inputStream.read(reads); return reads; } catch (FileNotFoundException e) { //log.error("密钥文件不存在:", e); e.printStackTrace(); } catch (IOException e) { //log.error("密钥文件读取失败:", e); e.printStackTrace(); } return null; }
读取cer证书文件
/** * 根据Cer文件读取密钥内容 * * @param pubCerPath 密钥文件地址 * @return */ public static byte[] getKeyFromFile(String pubCerPath) throws IOException { InputStream inputStream = BocUtils.class.getClassLoader().getResourceAsStream(pubCerPath); BufferedInputStream bufin = new BufferedInputStream(inputStream); int buffSize = 1024; ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize); byte[] temp = new byte[buffSize]; int size = 0; while ((size = bufin.read(temp)) != -1) { out.write(temp, 0, size); } bufin.close(); byte[] content = out.toByteArray(); return content; }
参考一些读取项目文件的方法: