数字签名~~~~
package com.security3;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.KeyGenerator;
import org.apache.commons.codec.binary.Hex;
public class 消息摘要 {
private static String src="immoc security";
public static void main(String[] args) {
// TODO Auto-generated method stub
//jdkRSA();
//jdkDSA();
jdkECDSA();
}
public static void jdkRSA(){
try {
//1.初始化密钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair=keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey=(RSAPublicKey)keyPair.getPublic();
RSAPrivateKey rsaPrivateKey=(RSAPrivateKey)keyPair.getPrivate();
//2.执行签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(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();
System.out.println("jdk rsa sign:"+Hex.encodeHexString(result));
//3.验证签名
X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(rsaPublicKey.getEncoded());
keyFactory=KeyFactory.getInstance("RSA");
PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
signature=Signature.getInstance("MD5withRSA");
signature.initVerify(publicKey);//初始化验证方式
signature.update(src.getBytes());
boolean bool=signature.verify(result);
System.out.println("jdk rsa verify:"+bool);
} catch (Exception e) {
// TODO: handle exception
}
}
public static void jdkDSA(){
try {
//1.初始化密钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
keyPairGenerator.initialize(512);
KeyPair keyPair=keyPairGenerator.generateKeyPair();
DSAPublicKey dsaPublicKey=(DSAPublicKey)keyPair.getPublic();
DSAPrivateKey dsaPrivateKey=(DSAPrivateKey)keyPair.getPrivate();
//2.执行签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(dsaPrivateKey.getEncoded());
KeyFactory keyFactory=KeyFactory.getInstance("DSA");
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Signature signature=Signature.getInstance("SHA1withDSA");//声明签名对象
signature.initSign(privateKey);//执行签名对面的初始化方法
signature.update(src.getBytes());
byte[] result=signature.sign();
System.out.println("jdk dsa sign:"+Hex.encodeHexString(result));
//3.验证签名
X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(dsaPublicKey.getEncoded());
keyFactory=KeyFactory.getInstance("DSA");
PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
signature=Signature.getInstance("SHA1withDSA");
signature.initVerify(publicKey);//初始化验证方式
signature.update(src.getBytes());
boolean bool=signature.verify(result);
System.out.println("jdk dsa verify:"+bool);
} catch (Exception e) {
// TODO: handle exception
}
}
public static void jdkECDSA(){
try {
//1.初始化密钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
keyPairGenerator.initialize(256);
KeyPair keyPair=keyPairGenerator.generateKeyPair();
ECPublicKey dsaPublicKey=(ECPublicKey)keyPair.getPublic();
ECPrivateKey dsaPrivateKey=(ECPrivateKey)keyPair.getPrivate();
//2.执行签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(dsaPrivateKey.getEncoded());
KeyFactory keyFactory=KeyFactory.getInstance("EC");
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Signature signature=Signature.getInstance("SHA1withECDSA");//声明签名对象
signature.initSign(privateKey);//执行签名对面的初始化方法
signature.update(src.getBytes());
byte[] result=signature.sign();
System.out.println("jdk ec sign:"+Hex.encodeHexString(result));
//3.验证签名
X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(dsaPublicKey.getEncoded());
keyFactory=KeyFactory.getInstance("EC");
PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
signature=Signature.getInstance("SHA1withECDSA");
signature.initVerify(publicKey);//初始化验证方式
signature.update(src.getBytes());
boolean bool=signature.verify(result);
System.out.println("jdk ec verify:"+bool);
} catch (Exception e) {
// TODO: handle exception
}
}
}
热门评论
楼主,可以问你个问题吗?初始化密钥的时候已经得到了dsaPrivateKey为什么后面的签名又要通过KeyFactory去获取一遍呢