packagecom.chengxuyuanzhilu.rsa;importjava.security.InvalidKeyException;importjava.security.KeyFactory;importjava.security.KeyPair;importjava.security.KeyPairGenerator;importjava.security.NoSuchAlgorithmException;importjava.security.PrivateKey;importjava.security.PublicKey;importjava.security.Signature;importjava.security.SignatureException;importjava.security.interfaces.RSAPrivateKey;importjava.security.interfaces.RSAPublicKey;importjava.security.spec.InvalidKeySpecException;importjava.security.spec.PKCS8EncodedKeySpec;importjava.security.spec.X509EncodedKeySpec;/***
微信公众号:程序员之路
*
博客:http://www.cnblogs.com/chengxuyuanzhilu
* @ClassName: CXYZL_RSA
* @Description: 数组签名算法——RSA
*@author付成刚
* @date 2016年2月17日 上午7:23:02*/
public classCXYZL_RSA {//要签名和验证的签名内容
private static String src = "chengxuyuanzhilu rsa";public static voidmain(String[] args) {
jdkRSA();
}public static voidjdkRSA(){
CXYZL_RSA cxyzl_RSA= newCXYZL_RSA();try{//1.初始化密钥,产生公钥私钥对
Object[] keyPairArr =cxyzl_RSA.initSecretkey();
RSAPublicKey rsaPublicKey= (RSAPublicKey) keyPairArr[0];
RSAPrivateKey rsaPrivateKey= (RSAPrivateKey) keyPairArr[1];//2.执行签名
byte[] result =cxyzl_RSA.executeSignature(rsaPrivateKey);//3.验证签名
boolean bool =cxyzl_RSA.verifySignature(rsaPublicKey,result);
System.out.println("RSA-MD5withRSA数字签名算法运算结果:"+bool);
}catch(Exception e) {
e.printStackTrace();
}
}/*** @Title: initSecretkey
* @Description: 初始化密钥,生成公钥私钥对
*@returnObject[] 0 公钥,1 私钥
*@author微信公众号:程序员之路
*@throwsNoSuchAlgorithmException
* @date 2016年2月17日 上午7:31:06*/
private Object[] initSecretkey() throwsNoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator= KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair=keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey=(RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey=(RSAPrivateKey) keyPair.getPrivate();
Object[] keyPairArr= new Object[2];
keyPairArr[0] =rsaPublicKey;
keyPairArr[1] =rsaPrivateKey;returnkeyPairArr;
}/*** @Title: executeSignature
* @Description: 执行签名
*@returnbyte[] 签名后的内容
*@author微信公众号:程序员之路
*@throwsInvalidKeyException
*@throwsNoSuchAlgorithmException
*@throwsInvalidKeySpecException
*@throwsSignatureException
* @date 2016年2月17日 上午7:44:49*/
private byte[] executeSignature(RSAPrivateKey rsaPrivateKey) throwsInvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, SignatureException{
PKCS8EncodedKeySpec pkcs8EncodedKeySpec= newPKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory keyFactory= KeyFactory.getInstance("RSA");
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Signature signature= Signature.getInstance("MD5withRSA");
signature.initSign(privateKey);
signature.update(src.getBytes());byte[] result =signature.sign();returnresult;
}/*** @Title: verifySignature
* @Description: 验证签名
*@paramrsaPublicKey 公钥
*@paramresult 私钥执行签名的结果
*@returnboolean 验证结果
*@author微信公众号:程序员之路
*@throwsNoSuchAlgorithmException
*@throwsInvalidKeySpecException
*@throwsInvalidKeyException
*@throwsSignatureException
* @date 2016年2月17日 上午7:53:37*/
private boolean verifySignature(RSAPublicKey rsaPublicKey,byte[] result) throwsNoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException{
X509EncodedKeySpec x509EncodedKeySpec= newX509EncodedKeySpec(rsaPublicKey.getEncoded());
KeyFactory keyFactory= KeyFactory.getInstance("RSA");
PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
Signature signature= Signature.getInstance("MD5withRSA");
signature.initVerify(publicKey);
signature.update(src.getBytes());boolean bool =signature.verify(result);returnbool;
}
}