Java--数字签名算法

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();
    }

总结:三种数字签名算法的场景大致相似。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值