签名算法

package com.community.library.common.utils;

import lombok.extern.log4j.Log4j;

import java.security.*;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * 签名算法
 */
@Log4j
public class DsaUtil {

    /**
     * 获取密钥
     * @return
     */
    public static DsaKey createKey(){
        DsaKey key = new DsaKey();
        try {
            KeyPairGenerator  keyPairGenerator = KeyPairGenerator.getInstance("DSA");
            keyPairGenerator.initialize(512);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();

            DSAPublicKey dsaPublicKey = (DSAPublicKey) keyPair.getPublic();
            DSAPrivateKey dsaPrivateKey = (DSAPrivateKey)keyPair.getPrivate();

            byte[] encodedPublicKey =  dsaPublicKey.getEncoded();
            byte[] encodedPrivateKey =  dsaPrivateKey.getEncoded();

            String strPublicKey = Base64.getEncoder().encodeToString(encodedPublicKey);
            //byte[] decodedPublicKey = Base64.getDecoder().decode(strPublicKey);
            key.setPublicKey(strPublicKey);
            String strPrivateKey = Base64.getEncoder().encodeToString(encodedPrivateKey);
           // byte[] decodedPrivateKey = Base64.getDecoder().decode(strPrivateKey);
            key.setPrivateKey(strPrivateKey);
        } catch (NoSuchAlgorithmException e) {
            log.error(e);
        }
        return key;
    }

    /**
     * 执行签名
     * @param privateKey
     * @param str
     * @return
     */
     public static String sign(String privateKey,String str){

         try {
             byte[] decodedPrivateKey = Base64.getDecoder().decode(privateKey);
             PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(decodedPrivateKey);
             KeyFactory keyFactory = KeyFactory.getInstance("DSA");
             PrivateKey priKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
             Signature signature = Signature.getInstance("SHA1withDSA");
             signature.initSign(priKey);
             signature.update(str.getBytes());
             byte[] result = signature.sign();
             return Base64.getEncoder().encodeToString(result);
         } catch (NoSuchAlgorithmException e) {
             log.error(e);
         } catch (SignatureException e) {
             log.error(e);
         } catch (InvalidKeyException e) {
             log.error(e);
         } catch (InvalidKeySpecException e) {
             log.error(e);
         }
         return null;

     }

    /**
     * 验证
     * @param publicKey
     * @param str
     * @return
     */
     public static boolean verify(String publicKey,String str,String sign){

         try {
             byte[] decodedPublicKey = Base64.getDecoder().decode(publicKey);
             X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(decodedPublicKey);
             KeyFactory keyFactory = KeyFactory.getInstance("DSA");
             keyFactory = KeyFactory.getInstance("DSA");
             PublicKey pubKey = keyFactory.generatePublic(x509EncodedKeySpec);
             Signature signature = Signature.getInstance("SHA1withDSA");
             signature = Signature.getInstance("SHA1withDSA");
             signature.initVerify(pubKey);
             signature.update(str.getBytes());
             return signature.verify(Base64.getDecoder().decode(sign));
         } catch (NoSuchAlgorithmException e) {
             log.error(e);
         } catch (InvalidKeySpecException e) {
             log.error(e);
         } catch (SignatureException e) {
             log.error(e);
         } catch (InvalidKeyException e) {
             log.error(e);
         }

         return false;
     }


    public static class DsaKey{
        private String privateKey;
        private String publicKey;

        public String getPrivateKey() {
            return privateKey;
        }

        public void setPrivateKey(String privateKey) {
            this.privateKey = privateKey;
        }

        public String getPublicKey() {
            return publicKey;
        }

        public void setPublicKey(String publicKey) {
            this.publicKey = publicKey;
        }
    }

}

 

转载于:https://www.cnblogs.com/chen-msg/p/10278795.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值