io.afu.utils.encryption.RSAHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils Show documentation
Show all versions of utils Show documentation
RffanLAB Utils For Many Way use
The newest version!
package io.afu.utils.encryption;
import io.afu.common.exception.BaseException;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jcajce.provider.asymmetric.RSA;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* @author RffanLAB.方露宇
* @version 0.1
*/
public class RSAHelper {
private String KEY_ALGORTHM = "RSA";
public static RSAHelper build(){
return new RSAHelper();
}
public static void main(String[] args) {
}
private String publicKey;
private String privateKey;
public String getPublicKey() {
return publicKey;
}
public RSAHelper setPublicKey(String publicKey) {
this.publicKey = publicKey;
return this;
}
public String getPrivateKey() {
return privateKey;
}
public RSAHelper setPrivateKey(String privateKey) {
this.privateKey = privateKey;
return this;
}
public byte[] encryptByPublicKey(byte[] data) throws BaseException {
if (publicKey == null || publicKey.equals("")){
throw new BaseException("请先设置publicKey");
}
try {
byte[] keyBytes = Base64.decodeBase64(publicKey);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM);
Key privateKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance(KEY_ALGORTHM,new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE,privateKey);
return cipher.doFinal(data);
}catch (Exception e){
throw new BaseException(e);
}
}
private RSAPrivateKey loadPrivateKey() throws Exception {
byte[] buffer = Base64.decodeBase64(privateKey.getBytes(StandardCharsets.ISO_8859_1.toString()));
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM);
PrivateKey rsaPrivateKey = keyFactory.generatePrivate(keySpec);
buffer = null;
keySpec = null;
return (RSAPrivateKey) rsaPrivateKey;
}
public String decryptByPrivateKey(byte[] data) throws BaseException {
if (privateKey == null | privateKey.equals("")){
throw new BaseException("请先设置privateKey");
}
ByteArrayOutputStream bout = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA", new BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, (PrivateKey) loadPrivateKey());
int blockSize = cipher.getBlockSize();
bout = new ByteArrayOutputStream(64);
int j = 0;
byte[] raw = Base64.decodeBase64(data);
while (raw.length - j * blockSize > 0) {
bout.write(cipher.doFinal(raw, j * blockSize, blockSize));
j++;
}
raw = null;
return bout.toString();
}catch (Exception e){
throw new BaseException(e);
}
}
}