packagecom.weiyuan.test;importjava.io.ByteArrayOutputStream;importjava.security.KeyPair;importjava.security.KeyPairGenerator;importjava.security.NoSuchAlgorithmException;importjava.security.PrivateKey;importjava.security.PublicKey;importjavax.crypto.Cipher;importjavax.crypto.NoSuchPaddingException;public classRSACrypt {public static voidmain(String[] args) {//生成RSA的密钥对
try{
String input="我爱黑马程序文件和将服务很纠结二十夫君今何伤方法反倒是积极上进发动机号的时间较慢的方式结算单夫君今何伤的今君得所附渐觉受东风基金会的深井冻痕生解决好多说几句话的实践活动睡觉觉";
KeyPairGenerator keyPairGenerator= KeyPairGenerator.getInstance("RSA");
KeyPair keyPair=keyPairGenerator.generateKeyPair();
PrivateKey privateKey=keyPair.getPrivate();
PublicKey publicKey=keyPair.getPublic();
String encryptByPublicKey=encryptByPublicKey(publicKey, input);
String privateKey2=decryptByPrivateKey(privateKey, encryptByPublicKey);
System.out.println(privateKey2);
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}/*** 接下来我们来实现私钥加密
*@throwsNoSuchPaddingException
*@throwsException
*
**/
public static String encryptByPublicKey(PublicKey publicKey,String input) throwsException, NoSuchPaddingException{
Cipher cipher= Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);//这里要采用分段加密的方式对数据进行加密//RSA分段加密最大的分段大小是117个字节//加密的偏移量
int offset = 0;byte[] buffer = new byte[1024];
ByteArrayOutputStream bOutputStream= newByteArrayOutputStream();//判断加密数据是否结束
while(input.getBytes().length - offset > 0){//判断是否到了最后一个分段块
if(input.getBytes().length - offset > 117){
buffer= cipher.doFinal(input.getBytes(),offset,117);
bOutputStream.write(buffer);
offset= offset +117;
}else{//说明最后一个分段快
buffer = cipher.doFinal(input.getBytes(),offset,input.getBytes().length -offset);
bOutputStream.write(buffer);
offset=input.getBytes().length;
}
}//将加密之后的数据进行base64编码,输出
String result =Base64Util.encode(bOutputStream.toByteArray());returnresult;
}/*** 接下来我们来实现公钥加密
*@throwsNoSuchPaddingException
*@throwsException
*
**/
public static String encryptByPrivateKey(PrivateKey privateKey,String input) throwsException, NoSuchPaddingException{
Cipher cipher= Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);//这里要采用分段加密的方式对数据进行加密//RSA分段加密最大的分段大小是117个字节//加密的偏移量
int offset = 0;byte[] buffer = new byte[1024];
ByteArrayOutputStream bOutputStream= newByteArrayOutputStream();//判断加密数据是否结束
while(input.getBytes().length - offset > 0){//判断是否到了最后一个分段块
if(input.getBytes().length - offset > 117){
buffer= cipher.doFinal(input.getBytes(),offset,117);
bOutputStream.write(buffer);
offset= offset +117;
}else{//说明最后一个分段快
buffer = cipher.doFinal(input.getBytes(),offset,input.getBytes().length -offset);
bOutputStream.write(buffer);
offset=input.getBytes().length;
}
}//将加密之后的数据进行base64编码,输出
String result =Base64Util.encode(bOutputStream.toByteArray());returnresult;
}/*** 接下来我们来实现私钥解密
*@throwsNoSuchPaddingException
*@throwsException
*
**/
public static String decryptByPrivateKey(PrivateKey privateKey,String input) throwsException, NoSuchPaddingException{//传入的参数是经过base64加密的数据,首先需要对数据进行base64解密
byte[] inputCode =Base64Util.decode(input);
Cipher cipher= Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);//这里要采用分段加密的方式对数据进行加密//RSA分段解密最大的分段大小是128个字节//加密的偏移量
int offset = 0;byte[] buffer = new byte[1024];
ByteArrayOutputStream bOutputStream= newByteArrayOutputStream();//判断加密数据是否结束
while(inputCode.length - offset > 0){//判断是否到了最后一个分段块
if(inputCode.length - offset > 128){
buffer= cipher.doFinal(inputCode,offset,128);
bOutputStream.write(buffer);
offset= offset +128;
}else{//说明最后一个分段快
buffer = cipher.doFinal(inputCode,offset,inputCode.length -offset);
bOutputStream.write(buffer);
offset=inputCode.length;
}
}//将加密之后的数据进行base64编码,输出
String result =bOutputStream.toString();returnresult;
}
}