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