继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

数字签名算法~~~~啦啦啦

qq_Sunshine暖阳_0
关注TA
已关注
手记 11
粉丝 2
获赞 98

数字签名~~~~

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
        }

    }
}
打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP

热门评论

楼主,可以问你个问题吗?初始化密钥的时候已经得到了dsaPrivateKey为什么后面的签名又要通过KeyFactory去获取一遍呢

查看全部评论