/**
* 方法说明:
* 获取指定公钥和私钥
*
* @return String[]
* @throws Exception
*/
public static String[] getKeyByJava() throws Exception {
String[] retKey = new String[2];
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
// 得到指定的公钥和私钥
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
generator.initialize(1024, new SecureRandom());
KeyPair pair = generator.generateKeyPair();
PublicKey pubKey = pair.getPublic();
PrivateKey privKey = pair.getPrivate();
byte[] pk = pubKey.getEncoded();
byte[] privk = privKey.getEncoded();
String strpk = new String(Base64Utils.base64Encode(pk)/*Base64.encodeBase64(pk)*/);
String strprivk = new String(Base64Utils.base64Encode(privk)/*Base64.encodeBase64(privk)*/);
System.out.println("公钥:" + Arrays.toString(pk));
System.out.println("私钥:" + Arrays.toString(privk));
System.out.println("公钥Base64编码:" + strpk);
System.out.println("私钥Base64编码:" + strprivk);
// 反向生成指定的公钥和私钥
X509EncodedKeySpec pubX509 = new X509EncodedKeySpec(Base64Utils.base64Decode(strpk)/*Base64.decodeBase64(strpk.getBytes())*/);
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64Utils.base64Decode(strprivk)/*Base64.decodeBase64(strprivk.getBytes())*/);
KeyFactory keyf = KeyFactory.getInstance("RSA", "BC");
PublicKey pubkey2 = keyf.generatePublic(pubX509);
PrivateKey privkey2 = keyf.generatePrivate(priPKCS8);
System.out.println(pubKey.equals(pubkey2));
System.out.println(privKey.equals(privkey2));
byte[] signStr = SignUtils.doSign(privkey2, reqXml.getBytes("UTF-8"), "MD5withRSA");
boolean signRet = SignUtils.doVerify(pubkey2, reqXml.getBytes("UTF-8"), signStr, "MD5withRSA");
retKey[0] = strpk;
retKey[1] = strprivk;
return retKey;
}
public final class SignUtils {
/**
* 签名算法SHA1withRSA
*/
public final static String SIGN_ALGORITHM_SHA1WITHRSA = "SHA1withRSA";
/**
* 签名算法MD5withRSA
*/
public final static String SIGN_ALGORITHM_MD5WITHRSA = "MD5withRSA";
/**
* 方法说明:
*
* @param keyPath 私钥路径
* @param keyPwd 私钥密码
* @param signAlg 签名算法
* @param textByte 原始报文字节
* @return byte[]
* @throws Exception
*/
public static byte[] sign(String keyPath, String keyPwd, String signAlg, byte[] textByte) throws Exception {
PrivateKey priKey = CertUtils.getPrivateKey(keyPath, keyPwd);
return doSign(priKey, textByte, signAlg);
}
/**
* 方法说明:
*
* @param cerPath 公钥路径
* @param signAlg 签名算法
* @param textByte 原始报文字节
* @param signByte 签名字节
* @return boolean 校验结果
* @throws Exception
*/
public static boolean verify(String cerPath, String signAlg, byte[] textByte, byte[] signByte) throws Exception {
PublicKey pubKey = CertUtils.getPublicKey(cerPath);
return doVerify(pubKey, textByte, signByte, signAlg);
}
/**
* 方法说明:
*
* @param priKey 私钥路径
* @param textByte 原始报文字节
* @param algorithm 签名算法
* @return byte[]
* @throws Exception
*/
public static byte[] doSign(PrivateKey priKey, byte[] textByte, String algorithm) throws Exception {
Signature sig = Signature.getInstance(algorithm);
sig.initSign(priKey);
sig.update(textByte);
return sig.sign();
}
/**
* 方法说明:
*
* @param pubKey 公钥路径
* @param textByte 原始串字节
* @param signaByte 签名字节串
* @param algorithm 签名算法
* @return boolean 是否签名校验正确
* @throws Exception
*/
public static boolean doVerify(PublicKey pubKey, byte[] textByte, byte[] signaByte, String algorithm)
throws Exception {
Signature sig = Signature.getInstance(algorithm);
sig.initVerify(pubKey);
sig.update(textByte);
return sig.verify(signaByte);
}