Java实现数字签名:RSA、DSA、ECDSA

标签: RSA DSA ECDSA 数字签名 java
9人阅读 评论(1) 收藏 举报

RSA

RSA

import org.apache.commons.codec.binary.Hex;

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.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * RSA
 *
 * @author liuming
 * @create 2018/4/13
 */
public class RSA {

    private static String srcMsg = "imooc security rsa";

    public static void main(String[] args) {
        jdkRSA(srcMsg);

    }

    public static void jdkRSA(String srcMsg) {
        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(srcMsg.getBytes());
            byte[] bytes = signature.sign();
            System.out.println(Hex.encodeHexString(bytes));

            // 验证签名
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
            keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            signature = Signature.getInstance("MD5withRSA");
            signature.initVerify(publicKey);
            signature.update(srcMsg.getBytes());
            boolean verfied = signature.verify(bytes);
            System.out.println(verfied);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

DSA

DSA

import org.apache.commons.codec.binary.Hex;

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.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * DSS(Digital Signature Standard) 数字签名标准
 * DSA(Digital Signature Algorithm) 数字签名算法
 *
 * @author liuming
 * @create 2018/4/13
 */
public class DSA {

    private static String srcMsg = "imooc security dsa";

    public static void main(String[] args) {
        jdkDSA(srcMsg);

    }

    public static void jdkDSA(String srcMsg) {
        try {
            // 初始化密钥
            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(srcMsg.getBytes());
            byte[] bytes = signature.sign();
            System.out.println(Hex.encodeHexString(bytes));

            // 验证签名
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(dsaPublicKey.getEncoded());
            keyFactory = KeyFactory.getInstance("DSA");
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            signature = Signature.getInstance("SHA1withDSA");
            signature.initVerify(publicKey);
            signature.update(srcMsg.getBytes());
            boolean verfied = signature.verify(bytes);
            System.out.println(verfied);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

ECDSA

ECDSA

import org.apache.commons.codec.binary.Hex;

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.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * ECDSA(Elliptic Curve Digital Signature Algorithm) 椭圆曲线数字签名算法
 *
 * @author liuming
 * @create 2018/4/13
 */
public class ECDSA {

    private static String srcMsg = "imooc security ecdsa";

    public static void main(String[] args) {
        jdkECDSA(srcMsg);

    }

    public static void jdkECDSA(String srcMsg) {
        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(srcMsg.getBytes());
            byte[] bytes = signature.sign();
            System.out.println(Hex.encodeHexString(bytes));

            // 验证签名
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded());
            keyFactory = KeyFactory.getInstance("EC");
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            signature = Signature.getInstance("SHA1withECDSA");
            signature.initVerify(publicKey);
            signature.update(srcMsg.getBytes());
            boolean verfied = signature.verify(bytes);
            System.out.println(verfied);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
查看评论

数字签名– RSA、DSA、ECDSA

1、  什么是数字签名 数字签名,就是只有信息发送者才能产生的别人无法伪造的一段数字串,这段数字串同时也是对信息发送者发送信息真实性的一个有效证明。 数字签名是带有密钥的消息摘要算法 2、  数字签名...
  • OnePersonTZ
  • OnePersonTZ
  • 2016-08-30 18:49:05
  • 2379

java RSA/DSA/ECDSA实现数字签名

数字签名:带有密钥(公钥,私钥)的消息摘要算法    私钥用于签名,公钥用于验证。 数字签名的作用:验证数据的完整性,认证数据来源,抗否认。 常用数字签名算法:RSA,DSA,ECDSA   ...
  • caiandyong
  • caiandyong
  • 2015-12-13 14:51:20
  • 4749

RSA、DSA和ECDSA三者的签名

数字签名就是只有信息的发送者才能产生的别人无法伪造的一段数字串,这段数字串同时也是对信息的发送者发送信息真实性的一个有效证明。一般是非对称密钥加密技术与数字摘要技术的结合应用,目前主流的三种非对称算法...
  • sszgg2006
  • sszgg2006
  • 2014-05-10 14:23:42
  • 17766

数字签名加密算法(RSA、DSA、ECDSA)

RSA的例子:import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGe...
  • sx5273
  • sx5273
  • 2016-01-17 17:24:29
  • 1682

消息摘要算法--RSA,DSA,ECDSA

数字签名算法: 签名 数字签名---带有密钥(公钥,私钥)的消息摘要算法 验证数据完整性,认证数据来源,抗否认 OSI参考模型 私钥签名,公钥验证 RSA,DSA,ECDSA 1.j...
  • zuozhiyoulaisam
  • zuozhiyoulaisam
  • 2017-02-13 12:09:17
  • 636

DSA数字签名原理及JAVA实现

DSA数字签名          DSA数字签名是Elgamal和Schnorr数字签名的一个变种,DSA数字签名优于Elgamal数字签名的地方在于它的签名长度较短,并且某些可以破解Elgamal方...
  • qq_35612816
  • qq_35612816
  • 2017-12-01 17:38:58
  • 256

关于ECDSA/ECC(密钥加密传输)和ECDSA/ECDH(密钥磋商)

最近项目中用到了ECC的相关算法,恶补了一下相关知识 ECC:Elliptic Curves Cryptography,椭圆曲线密码编码学 ECDSA:用于数字签名,是ECC与DSA的结合,整个签...
  • xueyepiaoling
  • xueyepiaoling
  • 2017-03-16 19:04:15
  • 3829

数字签名算法DSA

一、概述 1、DSA是以RSA为基础形成的 2、java6提供了DSA的实现,但是java6只提供了SHA1withDSA的实现 3、bouncycastle扩展提供了其他的DSA实...
  • bravegogo
  • bravegogo
  • 2016-12-28 20:41:59
  • 2133

RSA与ECC的选择

数字签名技术已经广泛使用于网络安全协议或分布式系统中,目前比较流行的数字签名算法有RSA和ECDSA。很多同学在产品设计中往往都难以区分RSA和ECDSA的优劣,所以笔者将基于自己的实践,来给出一些初...
  • seccloud
  • seccloud
  • 2012-11-16 00:26:32
  • 9254

日常小结-RSA加密算法、数字签名和数字证书及其java实现

前段时间遇到过RSA这里把前段时间学到的东西做一些总结:RSARSA是目前为止应用最为广泛的非对称加密算法。非对称加密算法简单的说就是分成公钥和私钥。加密和解密采用不同的算法实现,这样的好处是不需要像...
  • u011518120
  • u011518120
  • 2016-08-11 19:54:29
  • 1915
    个人资料
    持之以恒
    等级:
    访问量: 3万+
    积分: 719
    排名: 7万+
    阿里云·云产品·幸运券(点击图片,快速领取)