关于CA证书 报错--急急急

20170421w 2017-07-26 10:40:08
package com.aisino.Util;

import static com.google.common.base.Strings.isNullOrEmpty;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.io.Closer;

public class PKCS7 {
private final static Logger LOGGER = LoggerFactory.getLogger(PKCS7.class);
//CA相关配置文件
private static Properties properties;
//设置编码集
public final static String DEFAULT_CHARSET = "UTF-8";
//设置信任链
public static native boolean setTrusts(String trusts);

//设置解密证书
public static native boolean setDecryptPfx(byte[] decPfx, String passwd);

//设置签名证书
public static native boolean setSignedPfx(byte[] sigPfx, String passwd);

//验证证书,成功返回1
public static native int validateCert(String base64Cert);

//打包数字信封,传递加密证书(即接收者的证书)
public synchronized static native byte[] signedAndEnveloped(String encBase64Cert, byte[] inData);

//解包数字信封
public synchronized static native PKCS7 unpack(byte[] inData);

//获取错误码
public static native int getLastError();

/**
* sigCert、serial、subject、data以下参数 请不要有任何幻想修改,包括你看不惯的命名!!!!!!!!!!!!!!
*/
private String sigCert; //签名证书
private String serial; //证书序列号
private String subject; //证书主题
private byte[] data; //原文

//加载动态库
static {
//如果是Windows 64位系统使用SOFJni_x64.dll
//如果是Windows 32位系统使用SOFJni_x86.dll
//如果是linux 64位系统使用SOFJni_x64.so
System.load(System.getProperty("user.dir")+"\\src\\main\\resources\\SOFJni_x64.dll");
}

public PKCS7() {

}

/**
* 初始化PKCS7类
* @param trustsBytes 证书信任链
* @param privatePFXBytes 加密/签名私钥
* @param privatePFXKey 私钥密码
* @throws Exception
*/
public PKCS7(byte[] trustsBytes, byte[] privatePFXBytes, String privatePFXKey) throws Exception {
System.out.println(new String(trustsBytes));
if (!setTrusts(new String(trustsBytes))) {

throw new Exception("" + getLastError());
}

if (setDecryptPfx(privatePFXBytes, privatePFXKey)) {
throw new Exception("" + getLastError());
}

if (setSignedPfx(privatePFXBytes, privatePFXKey)) {
throw new Exception("" + getLastError());
}
}
/**
* 依据文件绝对路径, 读取文件
*
* @param fileUri 文件绝对路径
* @return byte[] 读取成功的文件字节流
*/
private byte[] readFile(String fileUri) {
final Closer closer = Closer.create();
try {
final BufferedInputStream bufferedInputStream = closer.register(new BufferedInputStream(new FileInputStream(fileUri)));
final byte[] bufferedBytes = new byte[bufferedInputStream.available()];

bufferedInputStream.read(bufferedBytes, 0, bufferedBytes.length);

return bufferedBytes;
} catch (IOException e) {
LOGGER.error("read file ioException:", e.fillInStackTrace());
} finally {
try {
closer.close();
} catch (IOException e) {
LOGGER.error("close file ioException:", e.fillInStackTrace());
}
}

return new byte[0];
}

/**
* 签名加密
*
* @param plainContent 预加密的原文
* @param publicPFXBytes 公钥加/解密证书的绝对路径
* @return 加密后的密文数据
*/
public byte[] pkcs7Encrypt(String plainContent, byte[] publicPFXBytes) {
try {
final byte[] certBytes = publicPFXBytes;

if (certBytes == null) {
throw new Exception("传入参数公钥为NULL,不可用");
}

final String encCert = new String(certBytes);
/* if (1 != validateCert(encCert)) {//证书无效
throw new Exception("" + getLastError());
}*/

if (isNullOrEmpty(plainContent)) {
throw new Exception("传入参数原文为NULL,不可用");
}
byte[] signedResult=signedAndEnveloped(encCert, plainContent.getBytes(DEFAULT_CHARSET));
if (signedResult == null) {
throw new Exception("加密返回空,请确认公钥证书是否正常!!!");
}
return signedResult;
} catch (Exception e) {
LOGGER.error("pkcs7Encrypt Exception:", e.fillInStackTrace());
}

return new byte[0];
}

/**
* 解密验签
*
* @param decodeBase64EncryptTxtBytes 经过Base64解压后的密文字节流
* @return byte[] 经过解密的明文字节流
*/
public byte[] pkcs7Decrypt(byte[] decodeBase64EncryptTxtBytes,String otherCer) {
byte[] decryptBytes = new byte[0];
try {
//解密
if (decodeBase64EncryptTxtBytes == null) {
throw new Exception("传入参数密文为NULL,不可用");
}

final PKCS7 pkcs7 = unpack(decodeBase64EncryptTxtBytes);

if (pkcs7 == null) {
throw new Exception("" + getLastError());
}
//比对公钥
String sigCert = pkcs7.sigCert;
//LOGGER.info("sigCert:{} sigCert.length:{}", sigCert, sigCert.replaceAll("[\\t\\n\\r]", "").length());
String ptQyCer = FileUtils.readFileToString(new File(otherCer));
//LOGGER.info("ptQyCer:{} ptQyCer.length:{}", ptQyCer, ptQyCer.replaceAll("[\\t\\n\\r]", "").length());
//LOGGER.info("sigCert compare to ptQyCer:" + sigCert.replaceAll("[\\t\\n\\r]", "").equals(ptQyCer.replaceAll("[\\t\\n\\r]", "")));
if(!sigCert.replaceAll("[\\t\\n\\r]", "").equals(ptQyCer.replaceAll("[\\t\\n\\r]", ""))) {
LOGGER.error("验签失败!!!");
throw new Exception("验签失败");
}
decryptBytes = pkcs7.data;
} catch (Exception e) {
LOGGER.error("pkcs7Decrypt Exception:", e.fillInStackTrace());
}

return decryptBytes;
}

}
这是CA工具
public static void main(String[] args) {

//System.out.println(System.getProperty("java.library.path"));
String workUrl=System.getProperty("user.dir");
String trustsBytes = workUrl+"\\src\\main\\resources\\trust.txt";
String PFXBytes = workUrl+"\\src\\main\\resources\\测试客户端私钥.pfx";
String PFXKey = workUrl+"\\src\\main\\resources\\测试客户端私钥密码.txt";
String CER=workUrl+"\\src\\main\\resources\\测试服务器端公钥.cer";
System.load(System.getProperty("user.dir")+"\\src\\main\\resources\\SOFJni_x64.dll");


// 测s试客户端加密过程1:信任链+测试客户端私钥(pfx)+密码(txt)来初始化PKCS7这个类
try {
PKCS7 pkcs7Client = new PKCS7(FileUtils.readFileToByteArray(new File(trustsBytes)), FileUtils.readFileToByteArray(new File(PFXBytes)),FileUtils.readFileToString(new File( PFXKey)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
报错 :Exception in thread "main" java.lang.UnsatisfiedLinkError: com.aisino.Util.PKCS7.setTrusts(Ljava/lang/String;)Z
at com.aisino.Util.PKCS7.setTrusts(Native Method)
at com.aisino.Util.PKCS7.<init>(PKCS7.java:74)
at com.aisino.test.test.main(test.java:27)

初始化失败?别人的 项目我导入可以直接运行,不报错,为啥换个项目里就报这个错误 ,,急急急 读取.dll文件没问题。
...全文
507 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
20170421w 2017-07-28
  • 打赏
  • 举报
回复
问题解决:把加载的工具放到com.xxx 测试com.xxx.xxx就不报错了
tianfang 2017-07-26
  • 打赏
  • 举报
回复
jdk版本,32/64? 下载相应jdk版本的Java Cryptography Extension (JCE)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧