org.kaizen4j.common.encrypt.RSASignature Maven / Gradle / Ivy
package org.kaizen4j.common.encrypt;
import com.google.common.base.Throwables;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* 基于RSA加密算法的数字签名类
*/
public final class RSASignature {
private static final String ALGORITHM = "RSA";
private static final String MD5_WITH_RSA = "MD5withRSA";
private RSASignature() {}
/**
* 签名方法
*
* @param data 待签名字符串
* @param privateKeyStr 私钥字符串
* @return 签名后的字节数组
*
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws InvalidKeyException
* @throws SignatureException
*/
public static byte[] sign(String data, String privateKeyStr) {
try {
PrivateKey privatekey = toPrivateKey(privateKeyStr);
Signature signature = Signature.getInstance(MD5_WITH_RSA);
signature.initSign(privatekey);
signature.update(data.getBytes(UTF_8));
return signature.sign();
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
throw new RuntimeException(e);
}
}
/**
* 校验方法
*
* @param data 待校验字符串
* @param signData 已签名的字节数组
* @param publicKeyStr 公钥
* @return 校验通过返回 true,失败返回 false
*
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws SignatureException
*/
public static boolean verify(String data, byte[] signData, String publicKeyStr) {
try {
PublicKey publicKey = toPublicKey(publicKeyStr);
Signature signature = Signature.getInstance(MD5_WITH_RSA);
signature.initVerify(publicKey);
signature.update(data.getBytes(UTF_8));
return signature.verify(signData);
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
throw new RuntimeException(e);
}
}
/**
* 加密数据
*
* @param data
* @param publicKey
* @return 加密后的字节数组
*
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
*/
public static byte[] encrypt(byte[] data, String publicKey) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, RSASignature.toPublicKey(publicKey));
return cipher.doFinal(data);
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | NoSuchAlgorithmException
| InvalidKeyException e) {
throw new RuntimeException(e);
}
}
/**
* 解密数据
*
* @param data
* @param privateKey
* @return 解密后的原始字节数组
*
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
*/
public static byte[] decrypt(byte[] data, String privateKey) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, RSASignature.toPrivateKey(privateKey));
return cipher.doFinal(data);
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | NoSuchAlgorithmException
| InvalidKeyException e) {
throw new RuntimeException(e);
}
}
/**
* 转换字符串到 PublicKey 对象
*
* @param key
* @return PublicKey 对象
*
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
*/
public static PublicKey toPublicKey(String key) {
try {
byte[] keyBytes = Base64.decodeBase64(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
/**
* 转换字符串到 PrivateKey 对象
*
* @param key
* @return PrivateKey 对象
*
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
*/
public static PrivateKey toPrivateKey(String key) {
try {
byte[] keyBytes = Base64.decodeBase64(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}