当前位置: 代码迷 >> Java Web开发 >> 帮忙,RSA私钥解密乱码,分数要多少给多少,该如何解决
  详细解决方案

帮忙,RSA私钥解密乱码,分数要多少给多少,该如何解决

热度:93   发布时间:2016-04-17 12:16:32.0
帮忙,RSA私钥解密乱码,分数要多少给多少
package cn.com.guanghua.myself;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.Key;
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.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class RSAUtil {
private static String pubExponentKey = "65537";
private static String pubModuleKey = "D:\\workspace\\encryptAndDecrypt\\src\\pubModuleKey.txt";
private static String txtStr = "D:\\workspace\\encryptAndDecrypt\\src\\mytest.txt";
private static String priExponentFile = "D:\\workspace\\encryptAndDecrypt\\src\\priExponentKey.txt";
private static String priModuleFile = "D:\\workspace\\encryptAndDecrypt\\src\\priModuleKey.txt";

private RSAUtil() {

}

private byte[] getFileStream(String file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len = 0;
try {
while((len = fis.read(b)) != -1) {
baos.write(b, 0, len);
b = new byte[1024];
}
} catch (IOException e) {
e.printStackTrace();
}
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}

public KeyPair getKeyPair1() {
RSAPublicKey publicKey = (RSAPublicKey) getPublicKey(new String(getFileStream(pubModuleKey)), pubExponentKey);
RSAPrivateKey privateKey = (RSAPrivateKey)getPrivateKey(new String(getFileStream(priModuleFile)), new String(getFileStream(priExponentFile)));
KeyPair keyPair = new KeyPair(publicKey, privateKey);
return keyPair;
}

private PublicKey getPublicKey(String modules,String publicExponent) {
RSAPublicKeySpec rsaSpec = new RSAPublicKeySpec(new BigInteger(modules),new BigInteger(publicExponent));
KeyFactory keyFactory = null;
try {
keyFactory = KeyFactory.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
PublicKey publicKey = null;
try {
publicKey = (RSAPublicKey)keyFactory.generatePublic(rsaSpec);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return publicKey;
}

private Key getPrivateKey(String modules,String privateExponent) {
RSAPrivateKeySpec rsaSpec = new RSAPrivateKeySpec(new BigInteger(modules),new BigInteger(privateExponent));
KeyFactory keyFactory = null;
try {
keyFactory = KeyFactory.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
PrivateKey privateKey = null;
try {
privateKey = (PrivateKey)keyFactory.generatePrivate(rsaSpec);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return privateKey;
  相关解决方案