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

当前位置:我的异常网» Java Web开发 » 帮忙,RSA私钥解密乱码,分数要多少给多少,该如何解

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

www.myexceptions.net  网友分享于:2013-03-05  浏览:355次

帮忙,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;

文章评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值