签名算法
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; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通