• 签名算法


    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;
            }
        }
    
    }
  • 相关阅读:
    [牛客]十二桥问题 解题报告
    [NOIP2017 逛公园] 解题报告
    [JSOI2008]最小生成树计数 解题报告
    类欧几里得算法
    概率与期望题目列表
    [SCOI2008]配对 解题报告
    拦截导弹
    牛客网-约数的个数
    牛客网-成绩排名
    最大连续区间和的算法总结
  • 原文地址:https://www.cnblogs.com/chen-msg/p/10278795.html
Copyright © 2020-2023  润新知