快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Java数字签名算法DSA实例详解-创新互联

本文实例讲述了Java数字签名算法DSA。分享给大家供大家参考,具体如下:

武川ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

一、介绍

DSS:Digital Signature Standard 数字签名标准

DSA:Digital Signature Algorithm 数字签名算法

DSA仅包含数字签名

二、参数说明

Java数字签名算法DSA实例详解

三、代码实现

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


本文标题:Java数字签名算法DSA实例详解-创新互联
网页URL:http://6mz.cn/article/cchjcg.html

其他资讯