数字签名(又称公钥数字签名)是一种类似写在纸上的普通的物理签名,但是使用了公钥加密领域的技术实现,用于鉴别数字信息的方法。关于数字签名的介绍,可以参见百度百科:http://baike.baidu.com/view/7626.htm。今天,我们就开始学习java中数字签名的使用。

项目结构如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARcAAADLCAIAAADLOkBjAAAewElEQVR4nO2d+XsTR5rH++/YH/aHfXbZ2SGzWQI2jjIhBJI4yWYgZJ5MIIcxZqKME2YyGyDcEMCHbCAo2E64L9sgbGNzWQiDMQbM4QMDwYZIPrAF2JYl41stH/T+0FKruruqu+3Wrffz1JNI1e9bXcL1VR3qeotiAABQBxXsCgBA2AMqAgC1gIoAQC2+VJG9p3dPbknWwYKsgwX780oGBofQqx09A5+ll/jwdgAQIvhSRfq9xzufNju62uxdjx813M3ak4tefdLdP/f7XJIvAIQvvlFRl82xJT3rYcXWoaadrqYdww3ptmsb9mSsWbk+c+X6zC3pWV02h+9UZNRSGr3FFyWpwaLXhEI1gBDANyrakp7V/6yc6buKSePO8dHh58/7yCqy6DWU1iiVwUe5ioxayo1EcZMEVAR48I2KVm3KZnou4hPDsNOhgKvIqPWKx6LX+F5HHmSqC0Q8SlVUUVGh0+lIV5evP8DYTuPTi/G53+deqG1OO359yfYzOG//qMii1wSoqwAVRTuKVFReXj516tQpU6aQDL5ZcYR5eoxN49b8iyV5P6QYvl2e++3y3MaHT8/cNM9efvSd1fm3Hz0tPl0v8iarCL3ifW3UUhq9XouO1Sx6DdfxePogtC9C8Q7zEJXxM/H3ZV8ZtayRO9/rSGm0WnSQFxrTNyAAyKuIldDChQslVPSF9jDTtp9p2+80H9iwxZB1quF02zibzrWPO+gXrFljl/Or706KvC16DSVCWkUU0rDdTdXdaNG2627hqJTQy1yJRi3Fb/BkFVFEdQmLN2qhg4oWZFTESWjRokUSKvrw8yNM80/DD3f9c82JbcXNBQ9d4rTvmv3LFUbr0z6R96T6Iq8SBKIRfv+7NYoIEEUoPHGNsGoh5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ebB2NjY2NjYyMjI8xuHRkZGxsbGrtQ8YDslk8kU7GqGBr4Z0Unv1yKf2BfpG73CWEUmk4l2Dp+/VkfTtNPp7L6Qabt++Hn9GZqmz1+rk1MRejqkRZAjPMbS6D62UWv0WiFe0gaSJeuFxtiTLCdVc0E+WiFxCaRMARNRET+0jFDFMqdiIvEzwmHcF8YqKi0tdQ4Pnyy73t/f39vb21WaZst512639/T0nCy7zs6RSktLca5GrfCvw8/xjkCQPyTbzIR7ahUZEEv2Gmv0FiVf2ROrOe+oVswB6+hp40qa6wRUJD5rmqAii17rueCtMj9+RsgTlip6P+Ukmmw22/6iC0d27zicl2esrM46Vnqk5ML42Oj4+NjZs2cx/uLhDfGPjF7AvpYzUFQy77hyfnvmT9CV11yQj9aXj0ZvwY33sAsD/umLML1RmI0Aw1JFf1iajaYnT56YrlanHyzWHSy5dL0m+1jpL/mnxsZGx8ZGT58+jfFX1BaViWQyKhI7IseVu98ShjLKay6hInHBSmdNE1QR/0h30fnt3D5ljyExfkaoE5YqYhjmi+NJaCowXc87e4VNnZ2d2bnF7Ar4yZMncd7oaMeodzc6/rgIM/6ZlIoUlexp+ha9njcxUFlz/swDt15m1IqHf2yxWMQ9iERPhbzlaiUeuyL/DEicDFBRQHj557+gadfRIi5lHT2Z8vMR9qekoqIivL93DMGbO+DXAFSpSEnJ3r5IPiCl0prz1wv0gtmQyBpTLP6uXiN+eHThVWyZmBmg1whZKgcV+Z8by18Vp+GhQefQkHN4mHY6XTQ94qJHRlwHDhwIdmUBHGEmExnCUkWV2pcat7+PpkrtS0ODA3whOUdctF6vD3ZlAQyR9ehCeKro4sJ/FafBgQGxkCRC4QHBwT3OjJyOiAlTFbGsVUawqwlEPmGsIgAIEUBFAKAWUBEAqOX/AYgsqV1szgXjAAAAAElFTkSuQmCC" alt="" />

DSA算法

一、 HuhxDSA.java

package com.huhx.security;

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; import org.apache.commons.codec.binary.Hex; /**
* writer: huhx
*/
public class HuhxDSA {
private final static String src = "http://www.cnblogs.com/huhx"; public static void main(String[] args) {
// 初始化签名
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(src.getBytes());
byte[] result = signature.sign();
System.out.println("jdk dsa sign: " + Hex.encodeHexString(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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

二、 运行结果如下:

jdk dsa sign: 302d0215009461ae45922c11159d3b6dc4c70eb0748763d23202146f5f9a5eedb36a760cf8b9b1d1d1fdc10636aafe
jdk dsa: true

RSA算法

一、 HuhxRSA.java

package com.huhx.security;

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; import org.apache.commons.codec.binary.Hex; /**
* writer: huhx
*/
public class HuhxRSA {
private final static String src = "http://www.cnblogs.com/huhx"; public static void main(String[] args) {
try {
// 初始化签名
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(512);
KeyPair keyPair = generator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); // 执行签名
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(encodedKeySpec);
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)); // 验证签名
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: " + bool);
} catch (Exception e) {
e.printStackTrace();
}
}
}

二、 运行结果如下:

jdk rsa sign: 564dc7f96c85a7a42e579ae191997c892f234272d220e3f062cf52071e827164fadd52720b69704979fc11810bdbf92b01dd3f5f26a621ffe699ef601d08fb6c
jdk rsa: true

ECDSA算法

一、 HuhxECDSA.java

package com.huhx.security;

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; import org.apache.commons.codec.binary.Hex; /**
* writer: huhx
*/
public class HuhxECDSA {
private final static String src = "mhttp://www.cnblogs.com/huhx"; public static void main(String[] args) {
try {
// 初始化签名
KeyPairGenerator generator = KeyPairGenerator.getInstance("EC");
generator.initialize(256);
KeyPair keyPair = generator.generateKeyPair();
ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey ecPrivateKey = (ECPrivateKey) keyPair.getPrivate(); // 执行签名
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("EC");
PrivateKey privateKey = keyFactory.generatePrivate(encodedKeySpec);
Signature signature = Signature.getInstance("SHA1withECDSA");
signature.initSign(privateKey);
signature.update(src.getBytes());
byte[] result = signature.sign();
System.out.println("jdk ecdsa sign: " + Hex.encodeHexString(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: " + bool);
} catch (Exception e) {
e.printStackTrace();
}
}
}

二、 运行结果如下:

jdk ecdsa sign: 30460221009b80596c51ccdf19c5e8e825aca6d85e549a30ee72a1e9b094f7b3a8c8b9310902210098e8dcc6ea932142d8cd9dd80c08fff7359f796571f8a973d3ca6e2dc0931904
jdk ecdsa: true

友情链接

java基础---->数字签名算法的介绍的更多相关文章

  1. java基础-BigInteger类常用方法介绍

    java基础-BigInteger类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.BigInteger类概述 Java中long型为最大整数类型,对于超过long ...

  2. java基础-Math类常用方法介绍

    java基础-Math类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Math类概念 Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函 ...

  3. java基础-System类常用方法介绍

    java基础-System类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.System类概念 在API中system类介绍的比较简单,我们给出定义,system中 ...

  4. java基础-Integer类常用方法介绍

    java基础-Integer类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需 ...

  5. java基础-Eclipse开发工具介绍

    java基础-Eclipse开发工具介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 所谓工欲善其事必先利其器,即将身为一名Java开发工程师怎么能没有一款好使的IDE呢?今天就 ...

  6. java基础-Idea开发工具介绍

    java基础-Idea开发工具介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前给大家介绍过一款Java的IDE叫eclipse,有些功能用起来不是很得心应手,尤其是在导报的 ...

  7. java基础-BigDecimal类常用方法介绍

    java基础-BigDecimal类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.BigDecimal类概述 我们知道浮点数的计算结果是未知的.原因是计算机二进制 ...

  8. java基础-Arrays类常用方法介绍

    java基础-Arrays类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Array类的概念 此类包含用来操作数组(比如排序和搜索)的各种方法.需要注意,如果指定 ...

  9. Java基础-Calendar类常用方法介绍

    Java基础-Calendar类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Calendar类概念 Calendar 类是一个抽象类,它为特定瞬间与一组诸如 Y ...

随机推荐

  1. ArrayDeque

    ArrayDeque是一个基于数组的,非线程安全的,没有容量大小限制的双端队列实现 下面这张图就是添加了一些元素的数据结构图,其中head指向数据结构中的头部元素,tail指向数据结构中最后一个元素. ...

  2. netfiler/iptables

    一. 什么是netfilter netfilter is a set of hooks inside the Linux kernel that allows kernel modules to re ...

  3. Hibernate-HQL&QBC基础使用(分页)

    @Test public void testHql() { Configuration configuration = new Configuration().configure(); Session ...

  4. 看了这个才发现jQuery源代码不是那么晦涩

    很多人觉得jquery.ext等一些开源js源代码 十分的晦涩,读不懂,遇到问题需要调试也很费劲.其实我个人感觉主要是有几个方面的原因: 1.对一些js不常用的语法.操作符不熟悉 2.某个functi ...

  5. thikphp 简单的接口骨架

    //get id 获取内容,调用xml方法 public function get(){ $id = $_GET['id'];//接收id $User = M('user'); //$val-> ...

  6. 【转】C#调用WebService实例和开发

    一.基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是 ...

  7. imx6 qt 24bpp RGB

    imx6运行qt,在24bit的LVDS接口屏上显示时,显示效果与实际的不同.蓝色变成了黄色. 本来应该显示成蓝色: 实际上去显示成了黄色: 而其他绿色的图标并没有改变,只是蓝色和黄色互换了. 猜想应 ...

  8. Spring 4 官方文档学习(十)数据访问之ORM

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/orm.html 占位用,暂略.

  9. linux -- #!/bin/bash

    #!/bin/bash是指此脚本使用/bin/bash来解释执行. 其中,#!是一个特殊的表示符,其后,跟着解释此脚本的shell路径. bash只是shell的一种,还有很多其它shell,如:sh ...

  10. Unity中坐标系转换方法

    前言 本篇文章主要是参考<Unity API 解析>---陈泉宏. 这是本人在学校图书馆找到一本书,主要介绍的就是常用的类,比较实用,没有冗余的地方.在此推荐一下这本书! 一.Screen ...