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;
}
private byte[] myEncrypt(byte[] data,Key key) {
Cipher cipher = null;
try {
cipher = Cipher.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (InvalidKeyException e) {
e.printStackTrace();
}
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(data.length);
int leaveSize = data.length%blockSize;
int outputBlockNum = (leaveSize == 0 ? data.length/blockSize : data.length/blockSize + 1); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(outputBlockNum * outputSize);
int j = 0;
while(data.length > j * blockSize) {
if(data.length - j * blockSize > blockSize) {
try {
baos.write(cipher.doFinal(data, j * blockSize, blockSize));
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
baos.write(cipher.doFinal(data, j * blockSize, data.length - j * blockSize));
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
j++;
}
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}

public byte[] mydecrypt(byte[] b, Key key) {
Cipher cipher = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
try {
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (InvalidKeyException e) {
e.printStackTrace();
}
int i = 0;
int blockSize = cipher.getBlockSize();
while(b.length - i * blockSize > 0) {
try {
baos.write(cipher.doFinal(b, i * blockSize, blockSize));
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
i++;
}
return baos.toByteArray();

}

public static void main(String[] args) {
RSAUtil rsaUtil = new RSAUtil();
RSAPublicKey publicKey = (RSAPublicKey) rsaUtil.getPublicKey(new String("公钥系数"), "65537");
RSAPrivateKey privateKey = (RSAPrivateKey)rsaUtil.getPrivateKey(new String("私钥系数"), new String("私钥专用指数"));        
                byte[] ttt = rsaUtil.myEncrypt(rsaUtil.getFileStream(txtStr), publicKey);
byte[] output = rsaUtil.mydecrypt(ttt, privateKey);
System.out.println(new String(output));
}
}
说明:(1)公钥的系数和公用指数,私钥的系数和公用指数都是由密钥对生成器生成,在此省略
      (2)用私钥解密时为什么还是乱码,大家帮忙啊

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【liuxinghua60】截止到2008-07-10 10:25:37的历史汇总数据(不包括此帖):
    发帖的总数量:6                        发帖的总分数:270                      每贴平均分数:45                       
    回帖的总数量:1                        得分贴总数量:0                        回帖的得分率:0%                       
    结贴的总数量:1                        结贴的总分数:50                       
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:5                        未结的总分数:220                      
    结贴的百分比:16.67 %               结分的百分比:18.52 %                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
      

  2.   

    RSAPublicKey publicKey = (RSAPublicKey) rsaUtil.getPublicKey(new String("公钥系数"), "65537");RSAPublicKeySpec rsaSpec = new RSAPublicKeySpec(new BigInteger(modules),new BigInteger(publicExponent));
    我怎么感觉连密钥对的生成都有问题啊?"公钥系数" 能产生 BigInteger 对象么?