为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Java

数字签名~~~~

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 class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="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
        }

    }
}
点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消