Monday, August 26, 2013

Java RSA Encryption and Decryption Example

This is the sample code

package sajith.foru.rsaencryption;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class RsaEncryption {

/**
* Generate RSA private and public key and Encrypt/Decrypt
*
* @param args
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static void main(String[] args) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
/**
* Generate KeyPair
*/
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048); // key size can be change to 512,1024
KeyPair kp = keyGen.genKeyPair();
/**
* Generate private and public key
*/
PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();
/**
* String want to be encrypted
*/
String text = "Your text Message";
Cipher cipher = Cipher.getInstance("RSA");
/**
* Enable Encrypt mode
*/
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedData = cipher.doFinal(text.getBytes());
String encryptedText = new String(encryptedData);
System.out.println("Text Encrypted : " + encryptedText);
/**
* Enable Decrypt mode
*/
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptDataValue = cipher.doFinal(encryptedData); // decrypted
// the
// encrypted
// data
String decryptedText = new String(decryptDataValue);
System.out.println("Text Decryted : " + decryptedText);

}

}

Java Eliptic Curve Cryptography Sign/verify Example

In this Post I am going to talk about the Elliptic curve encryption.First I will tell you why we use ECC. ECC is public-key Cryptography. ECC is used for the Authentication and authorization.Mainly The structure is like this way.In here(http://www.johannes-bauer.com/compsci/ecc/) you can see very good explanation how ECC works. .First Using public key we have sign data.and then using private key we can verify data.In here we verify that compare the signing data and what are the we input data.So I used bouncy castle for ECC. In here (http://www.bouncycastle.org/latest_releases.html) You can download the bouncy castle jar and add to the Project.In here I First sign my data using Private key.Then I Verify the signing data using Public 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.SecureRandom;
import java.security.Signature;

import java.security.Security;


import java.security.interfaces.ECPublicKey;


import java.security.spec.InvalidKeySpecException;

import java.security.spec.X509EncodedKeySpec;

import org.bouncycastle.jce.ECNamedCurveTable;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECParameterSpec;

public class PublickeyEncryption {


public static void main(String args[]) {


try {


/**

* ECC signature sign and verify
*/
ECParameterSpec ecSpec = ECNamedCurveTable
.getParameterSpec("prime192v1");
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");

g.initialize(ecSpec, new SecureRandom());

// Genarate keypair
KeyPair pair = g.generateKeyPair();
System.out.println("Provider Name :"+BouncyCastleProvider.PROVIDER_NAME);

/**
* Genarate Private and Public keys
*/
PublicKey pubKey = pair.getPublic();
PrivateKey prikey = pair.getPrivate();

byte[] inputData = new byte[] { (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01 };
Signature sig = Signature.getInstance("ECDSA", "BC");
/**
* Initialize the sign and update data to be Sign
*/
sig.initSign(prikey);
sig.update(inputData);
/**
* SIGN the data using private key
*/
byte[] signatureBytes = sig.sign();
System.out.println("Sign byte arry:" + Arrays.toString(signatureBytes));

/**
* Initialize the verify and update data to be Verify
*/
sig.initVerify(pubKey);
sig.update(inputData);

/**
* Check verify true or False
*/

System.out.println("VERIFY :" + sig.verify(signatureBytes));
} catch (Exception e) {
e.printStackTrace();
}
}
}