java加签bcprov_bcprov-jdk150n-147.jar  RSA实现实例

本文详细介绍了如何使用bcprov-jdk150n-147.jar库进行RSA加密和签名操作。通过示例代码展示了RSAKeyPairGenerator的使用,以及RSAEngine在PKCS1Encoding和OAEPEncoding两种填充模式下的应用。此外,还包含了公钥加密和私钥解密的完整流程。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值