package com.xxx;
import android.util.Base64;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
/**
* create by chenyingjie on 2020/3/30
* desc
*/
public class RSAUtil {
private static final String PUB_K = "";
private static final Charset CHARSET = StandardCharsets.UTF_8;
private static final String RSA = "RSA";
private static final String RSA_ECB = "RSA/ECB/PKCS1Padding";
/**
* 随机生成密钥对
*/
public static void getKeyPair() {
try {
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(RSA);
// 初始化密钥对生成器,密钥大小为96-1024位
keyPairGen.initialize(1024, new SecureRandom());
// 生成一个密钥对,保存在keyPair中
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公钥
String publicKeyString = Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT);
// 得到私钥字符串
String privateKeyString = Base64.encodeToString(privateKey.getEncoded(), Base64.DEFAULT);
// 将公钥和私钥打印
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public static String encryptParams(String str) {
return rsaEncode(str, PUB_K);
}
/**
* 简单字符串加密
*/
public static String encryptSimple(String str, String publicKey) {
try {
//base64编码的公钥
byte[] decoded = Base64.decode(publicKey, Base64.DEFAULT);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(RSA).generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance(RSA_ECB);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String outStr = Base64.encodeToString(cipher.doFinal(str.getBytes(CHARSET)), Base64.DEFAULT);
return outStr;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return "";
}
/**
* 简单字符串解密
*/
public static String decryptSimple(String str, String privateKey) {
try {
//64位解码加密后的字符串
byte[] inputByte = Base64.decode(str.getBytes(CHARSET), Base64.DEFAULT);
//base64编码的私钥
byte[] decoded = Base64.decode(privateKey, Base64.DEFAULT);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance(RSA).generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance(RSA_ECB);
cipher.init(Cipher.DECRYPT_MODE, priKey);
String outStr = new String(cipher.doFinal(inputByte));
return outStr;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return "";
}
/**
* 解密内容过长需要使用分段加密
*
* @return String
*/
public static String rsaEncode(String data, String publicKey) {
byte[] b = data.getBytes();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
byte[] decoded = Base64.decode(publicKey, Base64.DEFAULT);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(RSA).generatePublic(new X509EncodedKeySpec(decoded));
int keySize = pubKey.getModulus().bitLength();
int maxBlock = keySize / 8 - 11;
int inputLen = b.length;
int offSet = 0;
byte[] cache;
int i = 0;
Cipher cipher = Cipher.getInstance(RSA_ECB);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > maxBlock) {
cache = cipher.doFinal(b, offSet, maxBlock);
} else {
cache = cipher.doFinal(b, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * maxBlock;
}
byte[] decryptedData = out.toByteArray();
return Base64.encodeToString(decryptedData, Base64.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return "";
}
/**
* 分段解密
*
* @return String
*/
public static String rsaDecode(String data, String privateKey) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
byte[] decoded = Base64.decode(privateKey, Base64.DEFAULT);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance(RSA).generatePrivate(new PKCS8EncodedKeySpec(decoded));
int keySize = priKey.getModulus().bitLength();
int maxBlock = keySize / 8;
byte[] b = Base64.decode(data.getBytes(CHARSET), Base64.URL_SAFE);
int inputLen = b.length;
int offSet = 0;
byte[] cache;
int i = 0;
Cipher cipher = Cipher.getInstance(RSA_ECB);
cipher.init(Cipher.DECRYPT_MODE, priKey);
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > maxBlock) {
cache = cipher.doFinal(b, offSet, maxBlock);
} else {
cache = cipher.doFinal(b, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * maxBlock;
}
byte[] decryptedData = out.toByteArray();
return new String(decryptedData);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
RSA加解密及分段加解密
最新推荐文章于 2024-06-20 10:09:57 发布