两个类解决.代码如下:直接copy
package util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
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.Cipher;
/**
* RSA使用X509EncodedKeySpec、PKCS8EncodedKeySpec生成公钥和私钥
* 加密数据大小不能超过127 bytes
* @Description:加签、验签、加密、解密、生成公钥、私钥
* @date:2018年2月24日
*/
public class RSAUtil
{
public static void main(String[] args) throws Exception
{
//生成公私钥文件
// RSAUtil.getKeyPair("/home/herman/tttttttt");
String publicKey = RSAUtil.readKeyFromFile("/home/herman/tttttttt/publicKey.keystore");
String privateKey = RSAUtil.readKeyFromFile("/home/herman/tttttttt/privateKey.keystore");
String cipherText = RSAUtil.encryptByPublicKey("herman", RSAUtil.readPublicKeyFromString(publicKey));
System.out.println("加密之后是:"+cipherText);
String plainText = RSAUtil.decryptByPrivateKey(cipherText, RSAUtil.readPrivateKeyFromString(privateKey));
System.out.println("解密之后:"+plainText);
}
/**
* 生成公私钥对
* @param filePath 生成文件路径
*/
@SuppressWarnings("static-access")
public static void getKeyPair(String filePath)
{
KeyPairGenerator keyPairGenerator = null;
try
{
keyPairGenerator = keyPairGenerator.getInstance("RSA");
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
keyPairGenerator.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();String publicKeyString = Base64.encode(publicKey.getEncoded());
String privateKeyString = Base64.encode(privateKey.getEncoded());
try
{
BufferedWriter publicbw = new BufferedWriter(new FileWriter(new File(filePath+"/publicKey.keystore")));
BufferedWriter privatebw = new BufferedWriter(new FileWriter(new File(filePath+"/privateKey.keystore")));
publicbw.write(publicKeyString);
privatebw.write(privateKeyString);
publicbw.flush();
publicbw.close();
privatebw.flush();
privatebw.close();
}
catch (IOException e)
{
e.printStackTrace();
}}
/**
* 从文件中读取公钥或私钥
* @param filePath 文件路径
* @return 公钥或私钥
*/
public static String readKeyFromFile(String filePath)
{
try
{
BufferedReader br = new BufferedReader(new FileReader(new File(filePath)));
String readLine = null;
StringBuilder sb = new StringBuilder();
while((readLine = br.readLine()) != null)
{
sb.append(readLine);
}
br.close();
return sb.toString();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}/**
* 从字符串中加载公钥
* @return 公钥
*/
public static RSAPublicKey readPublicKeyFromString(String publicKeyStr)
{
try
{
byte[] bt = Base64.decode(publi