bcprov-jdk150n-147.jar RSA实现实例
(2012-10-23 15:38:50)
标签:
杂谈
如果你使用了SHA1并对散列值进行RSA签名、加密,则Padding的过程无需你本人去干预。
或者你可以看看下面的代码(RSAUtils.java):
package org.dev2dev.security.crypto.Asymmetric;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.encodings.OAEPEncoding;
import org.bouncycastle.crypto.encodings.PKCS1Encoding;
import org.bouncycastle.crypto.engines.DESEngine;
import org.bouncycastle.crypto.engines.RSAEngine;
import
org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
import
org.bouncycastle.crypto.params.RSAKeyGenerationParameters;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import
org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
import org.bouncycastle.util.encoders.Hex;
import
org.dev2dev.security.crypto.blockcipher.BlockCipherTool;
public class RSAUtils {
int keylength=1024;
int certainty=20;
RSAKeyGenerationParameters keyparam;
AsymmetricBlockCipher eng = null;
RSAKeyPairGenerator pGen = null;
AsymmetricCipherKeyPair pair = null;
public RSAUtils()
{
}
public String getName()
{
return "RSA";
}
public void setKeyLength(int rsakeylength)
{
if(rsakeylength==512||rsakeylength==768||rsakeylength==1024||rsakeylength==2048)
keylength=rsakeylength;
}
public void setCertaintyOfPrime(int certaintyofprime)
{
certainty=certaintyofprime;
}
public void initRSAKeyPair()
{
RSAKeyGenerationParameters rsaparam=
new RSAKeyGenerationParameters(BigInteger.valueOf(0x3),
new SecureRandom(), this.keylength, this.certainty);
this.keyparam = rsaparam;
//RSA Keypair的生成依赖于rsaparam
RSAKeyPairGenerator pGen = new RSAKeyPairGenerator();
pGen.init(keyparam);
pair = pGen.generateKeyPair();
pair.getPublic();
}
public void setRSAKeyPair(RSAKeyParameters pubparam,
RSAPrivateCrtKeyParameters privparam)
{
AsymmetricCipherKeyPair newpair=new
AsymmetricCipherKeyPair(pubparam,privparam);
pair=newpair;
}
public RSAKeyParameters getPublicKey()
{
return (RSAKeyParameters)pair.getPublic();
}
public RSAPrivateCrtKeyParameters getPrivateKey()
{
return (RSAPrivateCrtKeyParameters)pair.getPrivate();
}
public void setRSAMode(int mode)
{
eng = new RSAEngine(); //默认就是RAW模式, 安全性问题,已不再使用
if (mode==2)
eng = new PKCS1Encoding(eng);
else
eng = new OAEPEncoding(eng); //mode==3
}
public String encrypt(String input)
{
byte[] inputdata=Hex.decode(input);
//用公钥加密
eng.init(true, pair.getPublic());
System.out.println(">>>加密参数");
System.out.println(">>>明文字节数:"+inputdata.length);
System.out.println(">>>RSA Engine Input Block
Size="+this.eng.getInputBlockSize());
System.out.println(">>>RSA Engine Output Block
Size="+this.eng.getOutputBlockSize());
try
{
inputdata = eng.processBlock(inputdata, 0, inputdata.length);
}
catch (Exception e)
{
e.printStackTrace();
}
return new String(Hex.encode(inputdata));
}
public byte[] encrypt(byte[] inputdata)
{
byte[] outputdata=null;
//用公钥加密
eng.init(true, pair.getPublic());
try
{
inputdata = eng.processBlock(inputdata, 0, inputdata.length);
outputdata=new byte[eng.getOutputBlockSize()];
outputdata=inputdata;
}
catch (Exception e)
{
e.printStackTrace();
}
return outputdata;
}
public byte[] encryptPro(byte[] inputload)
{
ByteArrayInputStream inputstream=new
ByteArrayInputStream(inputload);
ByteArrayOutputStream outputstream=new
ByteArrayOutputStream();
//用公钥加密
eng.init(true, pair.getPublic());
int inBlockSize =this.eng.getInputBlockSize() ;
int outBlockSize = this.eng.getOutputBlockS