This article describes the Java digital signature algorithm DSA. Share it for your reference, as follows:
1. Introduction
DSS: Digital Signature Standard
DSA: Digital Signature Algorithm Digital Signature Algorithm
DSA only contains digital signatures
2. Parameter description
3. Code implementation
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. Initialization key KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA"); keyPairGenerator.initialize(512); KeyPair keyPair = keyPairGenerator.generateKeyPair(); DSAPublicKey dsaPublicKey = (DSAPublicKey) keyPair.getPublic(); DSAPrivateKey dsaPrivateKey = (DSAPrivateKey)keyPair.getPrivate(); //2. Execute the signature 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. Verify the signature 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(); } }}4. Realize the effect
jdk dsa sign: 302c0214310539f9e19ec98167a687eb4e8f91e7f47326bf021428080b7f0ad2ccffc71466998d8d364ba516e840
jdk dsa verify : true
5. Application scenarios
PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:
Online RSA encryption/decryption tools:
http://tools.VeVB.COM/password/rsa_encode
Text online encryption and decryption tools (including AES, DES, RC4, etc.):
http://tools.VeVB.COM/password/txt_encode
MD5 online encryption tool:
http://tools.VeVB.COM/password/CreateMD5Password
Online hash/hash algorithm encryption tool:
http://tools.VeVB.COM/password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool:
http://tools.VeVB.COM/password/hash_md5_sha
Online sha1/sha224/sha256/sha384/sha512 encryption tool:
http://tools.VeVB.COM/password/sha_encode
For more information about Java related content, please check out the topics of this site: "Summary of Java Mathematical Operation Skills", "Tutorial on Java Data Structures and Algorithms", "Summary of Java Characters and String Operation Skills", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.