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

posted on 2013-01-07 14:47  chitti  阅读(1855)  评论(0编辑  收藏  举报

导航