本文实例讲述了java数字签名算法dsa。分享给大家供大家参考,具体如下:
一、介绍
dss:digital signature standard 数字签名标准
dsa:digital signature algorithm 数字签名算法
dsa仅包含数字签名
二、参数说明
三、代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package com.imooc.security.dsa; 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; public class imoocdsa { private static string src = "cakin24 security dsa" ; public static void main(string[] args) { jdkdsa(); } 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) { e.printstacktrace(); } } } |
四、实现效果
jdk dsa sign : 302c0214310539f9e19ec98167a687eb4e8f91e7f47326bf021428080b7f0ad2ccffc71466998d8d364ba516e840
jdk dsa verify : true
五、应用场景
希望本文所述对大家java程序设计有所帮助。
原文链接:https://blog.csdn.net/chengqiuming/article/details/78762929