Bouncy Castle - RSA PKCS1.5Padding Algorithm

Introduce RSA algorithm
RSA is an algorithm for public-key cryptography. Public-key cryptography, also known as asymmetric cryptography, is a form of cryptography in which a user has a pair of cryptographic keys—a public key and a private key. The private key is kept secret, while the public key may be widely distributed. The keys are related mathematically, but the private key cannot be practically derived from the public key. A message encrypted with the public key can be decrypted only with the corresponding private key.
Bouncy Castle Library
In my opinion, BC library is a good and safe cryptography library. it is open source and supports both Java and C#.
You can download the library of Bouncy Castle from website www.bouncycastle.org
I recommend you may download crypto-138.zip package from this website.
It supports encryption library for the common J2EE applications and provides lightweight APIs for J2ME.
Using lightweight APIs for J2ME
After you download crypto-138.zip, in zips folder have two classes for J2ME: cldc_classes.zip and cldc_crypto.zip. You can add library reference in your J2ME application.
An important note, to deploy and run J2ME application using Bouncy Castle library, you must obfuscate source code to hight level. As below:
-Right-Click on your J2ME Project, choose Properties
-Select Build/Obfuscating and set Hight level on Obfuscation Level
Encryption class

import java.math.BigInteger;
import java.security.SecureRandom;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.encodings.PKCS1Encoding;
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;
/**
*
* @author khanh.mai
*/
public class Encryption {
    public RSAPrivateCrtKeyParameters _RSAPrivateKey;
    public RSAKeyParameters _RSAPublicKey;
    public void generateRSAKeyPair () throws Exception {
        SecureRandom theSecureRandom = new SecureRandom();
        BigInteger thePublicExponent = new BigInteger("10001", 16);
        RSAKeyGenerationParameters theRSAKeyGenParam =
            new RSAKeyGenerationParameters(thePublicExponent, theSecureRandom, 960, 80);
        //960 is key length (bit)
        RSAKeyPairGenerator theRSAKeyPairGen = new RSAKeyPairGenerator();
        theRSAKeyPairGen.init(theRSAKeyGenParam);
        AsymmetricCipherKeyPair theKeyPair = theRSAKeyPairGen.generateKeyPair(); 
        _RSAPrivateKey = (RSAPrivateCrtKeyParameters) theKeyPair.getPrivate();
        _RSAPublicKey = (RSAKeyParameters) theKeyPair.getPublic();
    }
    public byte [] RSAEncrypt (byte [] toEncrypt) throws Exception {
        if (_RSAPublicKey == null) {
            throw new Exception("Please generate RSA keys first in order to work");
        } 
        AsymmetricBlockCipher theEngine = new RSAEngine();
        theEngine = new PKCS1Encoding(theEngine);
        theEngine.init(true, _RSAPublicKey);
        return theEngine.processBlock(toEncrypt, 0, toEncrypt.length);
    }
    public byte [] RSADecrypt (byte [] toDecrypt) throws Exception {
        if (_RSAPrivateKey == null) {
            throw new Exception("Please generate RSA keys first in order to work");
        } 
        AsymmetricBlockCipher theEngine = new RSAEngine();
        theEngine = new PKCS1Encoding(theEngine);
        theEngine.init(false, _RSAPrivateKey);
        return theEngine.processBlock(toDecrypt, 0, toDecrypt.length);
    } 
}

There are many algorithms in this Bouncy Castle library but I have not researched yet bigsmile
It is my first post. Maybe it is helpful for you and me in the future!
Regards,
mdk

refer to:http://my.opera.com/myjava/blog/2008/08/20/bouncy-castle-rsa-pkcs1-5padding-algorithm

转载于:https://www.cnblogs.com/Ymete/archive/2013/01/07/2849304.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值