1 RSA:
Java代码实现:
public static void jdkRSA(){
try{
//初始化密钥
KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair=keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey= (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey= (RSAPrivateKey) keyPair.getPrivate();
//执行签名--用私钥加密
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("jdkRSA rsa sign:"+ Hex.encode(result));
//验证--用公钥去验证加密后的数据
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("验证是否相同:"+bool);
}catch (Exception e){
e.printStackTrace();
}
}
DSA与RSA类似:
public static final String SRC="hello world";
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();
//执行签名
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.encodeHex(result));
//验证签名
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:"+bool);
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
jdkDSA();
}
ECDSA算法(JDK1.7后才有):
public static final String SRC="hello world";
public static void jdkECDSA(){
try{
//初始化密钥
KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("EC");
keyPairGenerator.initialize(256);
KeyPair keyPair=keyPairGenerator.generateKeyPair();
ECPublicKey ecPublicKey= (ECPublicKey) keyPair.getPublic();
ECPrivateKey ecPrivateKey= (ECPrivateKey) keyPair.getPrivate();
//执行签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(ecPrivateKey.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 ecdsa sign:"+ Hex.encodeHex(result));
//验证签名
X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(ecPublicKey.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 ecdsa verify:"+bool);
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
jdkECDSA();
}
总结:三种数字签名算法的场景大致相似。