javarsaaes_java使⽤RSA与AES加密解密的实例代码详解⾸先了解下,什么是堆成加密,什么是⾮对称加密?
对称加密:加密与解密的密钥是相同的,加解密速度很快,⽐如AES
⾮对称加密:加密与解密的秘钥是不同的,速度较慢,⽐如RSA
·先看代码(先会⽤在研究)
相关依赖:
org.bouncycastle
bcprov-jdk15on
1.58
1,RSA⼯具类:
package cn.wangtao.utils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
pto.Cipher;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
/**
* @ClassName RSAUtils
* @Auth 桃⼦
* @Date 2019-6-25 15:15
* @Version 1.0
* @Description
**/
public class RSAUtils {
private static final String RSA = "RSA"; // 加密⽅式
private static final Logger logger= Logger(RSAUtils.class);
//获取密钥
public static KeyPair getKey() throws Exception {
try {
KeyPairGenerator keyPairGenerator = Instance(RSA, new BouncyCastleProvider()); keyPairGenerator.initialize(2048); // 初始化密钥长度
KeyPair keyPair = ateKeyPair();// ⽣成密钥对
return keyPair;
} catch (Exception e) {
<("获取RSA秘钥对异常",e);
throw new Exception("获取RSA秘钥对异常",e);
}
}
//利⽤公钥进⾏加密
public static String encryptStr(RSAPublicKey publicKey, String str) throws Exception {
try {
Cipher cipher = Instance(RSA, new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//加密
byte[] bytes = Bytes(), cipher);
//2进⾏转换成16进制
String result = CommonUtils.parseByte2HexStr(bytes);
return result;
} catch (Exception e) {
<("使⽤RSA公钥进⾏加密异常",e);
throw new Exception("使⽤RSA公钥进⾏加密异常",e);
}
}
//利⽤私钥进⾏解密
public static String decryptStr(RSAPrivateKey privateKey, String str) throws Exception {
try {
Cipher cipher = Instance(RSA, new BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, privateKey); // ⽤密钥初始化此Cipher对象
//16进制转换成2进制
byte[] bytes = CommonUtils.parseHexStr2Byte(str);
//解密
byte[] bs = getBytes(bytes, cipher);
String content=new String(bs,"utf-8");
return content;
} catch (Exception e) {
<("使⽤RSA私钥进⾏解密异常",e);
throw new Exception("使⽤RSA私钥进⾏解密异常",e);
}
}
//通过cipher获取字节数组
public static byte[] getBytes(byte[] bytes,Cipher cipher) throws Exception {
int blockSize = BlockSize(); // 返回块的⼤⼩
int j = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (bytes.length - j * blockSize > 0) { // 将⼆进制数据分块写⼊ByteArrayOutputStream中if(bytes.length-j*blockSize>blockSize){
baos.write(cipher.doFinal(bytes, j * blockSize, blockSize));
}else{
baos.write(cipher.doFinal(bytes, j * blockSize,bytes.length-j*blockSize));
}
j++;
}
baos.close();
byte[] byteArray = ByteArray();
return byteArray;
}
//保存秘钥对到⽂件
public void saveRSAKey(String fileName) throws Exception {
FileOutputStream fos=null;
ObjectOutputStream oos=null;
try {
KeyPair keyPair = getKey();
fos=new FileOutputStream(fileName);
oos=new ObjectOutputStream(fos); //对象序列号
oos.writeObject(keyPair);
} catch (Exception e) {
<("RSA秘钥对保存到⽂件异常[{}]",fileName,e); throw new Exception("RSA秘钥对保存到⽂件异常",e);
}finally {
if(oos!=null){
try {
oos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
java源代码加密}
if(fos!=null){
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
2,CommonUtils通⽤⼯具类:
package cn.wangtao.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* @ClassName CommonUtils
* @Auth 桃⼦
* @Date 2019-6-27 12:51
* @Version 1.0
* @Description
**/
public class CommonUtils {
private static final Logger logger= Logger(CommonUtils.class);
//编码⽅式
public static final String CODE_TYPE = "UTF-8";
//字符补全
private static final String[] consult = new String[]{"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G"}; //关流
public static void closeReaderandWriter(Reader reader, Writer writer){
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
<("关闭输出流失败",e);
}
}
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
<("关闭输出流失败",e);
}
}
}
/
/将16进制转换为⼆进制
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。