Free Online Articles Directory
17.11.2008 Sign In Register Hello Guest
Email:
Password:
Remember Me 
forgot your password?


Encryption Using Rsa Algorithm in Java

Author: Debadatta Mishra Author Ranking Blue | Posted: 23-08-2008 | Comments: 2 | Views: 398 | Rating:  (208) Article Popularity - Blue (?) Got a Question? Ask.
Sign Up Now!

Encryption using RSA algorithm in java

 

Introduction

In this article I will provide you an approach of using RSA algorithm for long String. As you know that RSA algorithm is limited 117 bytes, long strings can not be encrypted or decrypted. However it is possible to break the bytes into several chunks and then to encrypt or decrypt the contents. This algorithm is used for asymmetric cryptography. For asymmetric cryptography, you can click this link.

 

Technicalities

In this article I provide below the complete example for encryption and decryption of long strings. If you use the method of Cipher class ie.doFinal( byte[] bytesString), it will throw exception that it can be encrypted for more than 117 bytes for RSA.  But in the real application, you may not be sure about the length of the String you want to encrypt or decrypt. In this case you have to break the bytes and then to encrypt it. Please refer to the

Following complete example.

 

Complete example

 

Class name : SecurityUtil.java

 

package com.dds.core.security;

 

import java.security.KeyFactory;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.Security;

import java.security.spec.EncodedKeySpec;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.spec.X509EncodedKeySpec;

 

import javax.crypto.Cipher;

 

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

 

import com.sun.crypto.provider.SunJCE;

 

/**This is a utility class which provides

 * convenient method for security. This

 * class provides the way where you can

 * encrypt and decrypt the String having

 * more than 117 bytes for RSA algorithm

 * which is an asymmetric one.

 * @author Debadatta Mishra(PIKU)

 *

 */

public class SecurityUtil {

            /**

             * Object of type {@link KeyPair}

             */

            private KeyPair keyPair;

            /**

             * String variable which denotes the algorithm

             */

            private static final String ALGORITHM = "RSA";

            /**

             * varibale for the keysize

             */

            private static final int KEYSIZE = 1024;

 

            /**

             * Default constructor

             */

            public SecurityUtil() {

                        super();

                        Security.addProvider(new SunJCE());

            }

 

            /**

             * This method is used to generate

             * the key pair.

             */

            public void invokeKeys() {

                        try {

                                    KeyPairGenerator keypairGenerator = KeyPairGenerator

                                    .getInstance(ALGORITHM);

                                    keypairGenerator.initialize(KEYSIZE);

                                    keyPair = keypairGenerator.generateKeyPair();

                        } catch (Exception e) {

                                    e.printStackTrace();

                        }

            }

 

            /**This method is used to obtain the String

             * representation of the PublicKey.

             * @param publicKey of type {@link PublicKey}

             * @return PublicKey as a String

             */

            public String getPublicKeyString(PublicKey publicKey) {

                        return new BASE64Encoder().encode(publicKey.getEncoded());

            }

 

            /**This method is used to obtain the String

             * representation of the PrivateKey.

             * @param privateKey of type {@link PrivateKey}

             * @return PrivateKey as a String

             */

            public String getPrivateKeyString(PrivateKey privateKey) {

                        return new BASE64Encoder().encode(privateKey.getEncoded());

            }

 

            /**This method is used to obtain the

             * {@link PrivateKey} object from the

             * String representation.

             * @param key of type String

             * @return {@link PrivateKey}

             * @throws Exception

             */

            public PrivateKey getPrivateKeyFromString(String key) throws Exception {

                        PrivateKey privateKey = null;

                        try {

                                    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);

                                    EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(

                                                            new BASE64Decoder().decodeBuffer(key));

                                    privateKey = keyFactory.generatePrivate(privateKeySpec);

                        } catch (Exception e) {

                                    e.printStackTrace();

                        }

                        return privateKey;

            }

 

            /**This method is used to obtain the {@link PublicKey}

             * from the String representation of the Public Key.

             * @param key of type String

             * @return {@link PublicKey}

             * @throws Exception

             */

            public PublicKey getPublicKeyFromString(String key) throws Exception {

                        PublicKey publicKey = null;

                        try {

                                    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);

                                    EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(

                                                            new BASE64Decoder().decodeBuffer(key));

                                    publicKey = keyFactory.generatePublic(publicKeySpec);

                        } catch (Exception e) {

                                    e.printStackTrace();

                        }

                        return publicKey;

            }

 

            /**This method is used to obtain the

             * encrypted contents from the original

             * contents by passing the {@link PublicKey}.

             * This method is useful when the byte is more

             * than 117.

             * @param text of type String

             * @param key of type {@link PublicKey}

             * @return encrypted value as a String

             * @throws Exception

             */

            public String getEncryptedValue(String text, PublicKey key)

            throws Exception {

                        String encryptedText;

                        try {

                                    byte[] textBytes = text.getBytes("UTF8");

                                    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

                                    cipher.init(Cipher.ENCRYPT_MODE, key);

 

                                    int textBytesChunkLen = 100;

                                    int encryptedChunkNum = (textBytes.length - 1) / textBytesChunkLen

                                    + 1;

 

                                    // RSA returns 128 bytes as output for 100 text bytes

                                    int encryptedBytesChunkLen = 128;

                                    int encryptedBytesLen = encryptedChunkNum * encryptedBytesChunkLen;

                                    System.out.println("Encrypted bytes length-------"

                                                            + encryptedBytesChunkLen);

                                    // Define the Output array.

                                    byte[] encryptedBytes = new byte[encryptedBytesLen];

 

                                    int textBytesChunkIndex = 0;

                                    int encryptedBytesChunkIndex = 0;

 

                                    for (int i = 0; i < encryptedChunkNum; i++) {

                                                if (i < encryptedChunkNum - 1) {

                                                            encryptedBytesChunkIndex = encryptedBytesChunkIndex

                                                            + cipher.doFinal(textBytes, textBytesChunkIndex,

                                                                                    textBytesChunkLen, encryptedBytes,

                                                                                    encryptedBytesChunkIndex);

 

                                                            textBytesChunkIndex = textBytesChunkIndex

                                                            + textBytesChunkLen;

                                                } else {

                                                            cipher.doFinal(textBytes, textBytesChunkIndex,

                                                                                    textBytes.length - textBytesChunkIndex,

                                                                                    encryptedBytes, encryptedBytesChunkIndex);

                                                }

                                    }

                                    encryptedText = new BASE64Encoder().encode(encryptedBytes);

                        } catch (Exception e) {

                                    throw e;

                        }

                        return encryptedText;

            }

 

            /**This method is used to decrypt the contents.

             * This method is useful when the size of the

             * bytes is more than 117.

             * @param text of type String indicating the

             * encrypted contents.

             * @param key of type {@link PrivateKey}

             * @return decrypted value as a String

             */

            public String getDecryptedValue(String text, PrivateKey key) {

                        String result = null;

                        try {

                                    byte[] encryptedBytes = new BASE64Decoder().decodeBuffer(text);

                                    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

                                    cipher.init(Cipher.DECRYPT_MODE, key);

 

                                    int encryptedByteChunkLen = 128;

                                    int encryptedChunkNum = encryptedBytes.length

                                    / encryptedByteChunkLen;

                                    int decryptedByteLen = encryptedChunkNum * encryptedByteChunkLen;

                                    byte[] decryptedBytes = new byte[decryptedByteLen];

                                    int decryptedIndex = 0;

                                    int encryptedIndex = 0;

 

                                    for (int i = 0; i < encryptedChunkNum; i++) {

                                                if (i < encryptedChunkNum - 1) {

                                                            decryptedIndex = decryptedIndex

                                                            + cipher.doFinal(encryptedBytes, encryptedIndex,

                                                                                    encryptedByteChunkLen, decryptedBytes,

                                                                                    decryptedIndex);

                                                            encryptedIndex = encryptedIndex + encryptedByteChunkLen;

                                                } else {

                                                            decryptedIndex = decryptedIndex

                                                            + cipher.doFinal(encryptedBytes, encryptedIndex,

                                                                                    encryptedBytes.length - encryptedIndex,

                                                                                    decryptedBytes, decryptedIndex);

                                                }

                                    }

                                    result = new String(decryptedBytes).trim();

                        } catch (Exception e) {

                                    e.printStackTrace();

                        }

                        return result;

            }

 

            /**This method is used obtain the

             * {@link PublicKey}

             * @return {@link PublicKey}

             */

            public PublicKey getPublicKey() {

                        return keyPair.getPublic();

            }

 

            /**This method is used to obtain

             * the {@link PrivateKey}

             * @return {@link PrivateKey}

             */

            public PrivateKey getPrivateKey() {

                        return keyPair.getPrivate();

            }

}

 

The above class provides several useful methods for generation of Private key , Public Key and encryption of String and decryption of String.

 

Please refer to the following subordinate classes for the above class.

 

Class name : KeyGenerator.java

 

package com.dds.core.security;

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.util.Properties;

 

/**This class is used to generate the

 * Private and Public key and stores

 * them in files.

 * @author Debadatta Mishra(PIKU)

 *

 */

public class KeyGenerator {

            /**This method is used to obtain the

             * path of the keys directory where

             * Private and Public key files are

             * stored.

             * @return path of the keys directory

             */

            private static String getKeyFilePath() {

                        String keyDirPath = null;

                        try {

                                    keyDirPath = System.getProperty("user.dir") + File.separator

                                    + "keys";

                                    File keyDir = new File(keyDirPath);

                                    if (!keyDir.exists())

                                                keyDir.mkdirs();

                        } catch (Exception e) {

                                    e.printStackTrace();

                        }

                        return keyDirPath;

            }

 

            /**

             * This method is used to generate the

             * Private and Public keys.

             */

            public static void generateKeys() {

                        Properties publicProp = new Properties();

                        Properties privateProp = new Properties();

                        try {

                                    OutputStream pubOut = new FileOutputStream(getKeyFilePath()

                                                            + File.separator + "public.key");

                                    OutputStream priOut = new FileOutputStream(getKeyFilePath()

                                                            + File.separator + "private.key");

 

                                    SecurityUtil secureUtil = new SecurityUtil();

                                    secureUtil.invokeKeys();

 

                                    PublicKey publicKey = secureUtil.getPublicKey();

                                    PrivateKey privateKey = secureUtil.getPrivateKey();

 

                                    String publicString = secureUtil.getPublicKeyString(publicKey);

                                    String privateString = secureUtil.getPrivateKeyString(privateKey);

 

                                    publicProp.put("key", publicString);

                                    publicProp.store(pubOut, "Public Key Info");

 

                                    privateProp.put("key", privateString);

                                    privateProp.store(priOut, "Private Key Info");

                        } catch (Exception e) {

                                    e.printStackTrace();

                        }

            }

}

 

The above class is used to generate the Public and Private keys. It generates and stores them in different files called Public.key and Private.key. Please refer the test harness class for the above class.

 

Class name: TestKeyGenerator

 

import com.dds.core.security.KeyGenerator;

 

/**This is a testharness class

 * for the KeyGenerator class.

 * @author Debadatta Mishra(PIKU)

 *

 */

public class TestKeyGenerator {

      public static void main(String[] args) {

            KeyGenerator.generateKeys();

      }

 

}

If you run the above class, you will find a directory called keys in your root path of your application folder. In this folder you will find two files one is for Private Key information and another is for Public Key.

There is another class which is used to obtain the Private key and Public key information stored in the files.

 

Class name: KeyReader.java

 

package com.dds.core.security;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.security.PublicKey;

import java.util.Properties;

 

/**This class is used to read the

 * keys from the file.

 * @author Debadatta Mishra(PIKU)

 *

 */

public class KeyReader {

            /**This method is used to obtain the

             * string value of the Public Key

             * from the file.

             * @return String of {@link PublicKey}

             */

            public static String getPublicKeyString() {

                        String publicString = null;

                        try {

                                    Properties prop = new Properties();

                                    String publicKeyPath = System.getProperty("user.dir")

                                    + File.separator + "keys" + File.separator + "public.key";

                                    InputStream in = new FileInputStream(publicKeyPath);

                                    prop.load(in);

                                    publicString = prop.getProperty("key");

                        } catch (Exception e) {

                                    e.printStackTrace();

                        }

                        return publicString;

            }

 

            /**This method is used to obtain the

             * String of Private Key from the file.

             * @return String of private key

             */

            public static String getPrivateKeyString() {

                        String publicString = null;

                        try {

                                    Properties prop = new Properties();

                                    String publicKeyPath = System.getProperty("user.dir")

                                    + File.separator + "keys" + File.separator + "private.key";

                                    InputStream in = new FileInputStream(publicKeyPath);

                                    prop.load(in);

                                    publicString = prop.getProperty("key");

                        } catch (Exception e) {

                                    e.printStackTrace();

                        }

                        return publicString;

            }

}

 

This is a utility class to read the Public and Private keys from the files.

 

Now refer to the test harness class which makes encryption and decryption of String.

 

import java.security.PrivateKey;

import java.security.PublicKey;

 

import com.dds.core.security.KeyReader;

import com.dds.core.security.SecurityUtil;

 

/**

 * This is a test harness class for encryption and decryption.

 *

 * @author Debadatta Mishra(PIKU)

 *

 */

public class TestEncryption {

 

            public static void main(String[] args) {

                        String privateKeyString = KeyReader.getPrivateKeyString();

                        SecurityUtil securityUtil = new SecurityUtil();

                        String publicKeyString = KeyReader.getPublicKeyString();

                        try {

                                    PublicKey publicKey = securityUtil

                                    .getPublicKeyFromString(publicKeyString);

                                    PrivateKey privateKey = securityUtil

                                    .getPrivateKeyFromString(privateKeyString);

                                    String originalValue = "provide some very long string";

 

                                    String encryptedValue = securityUtil.getEncryptedValue(

                                                            originalValue, publicKey);

                                    System.out.println("EncryptedValue-----" + encryptedValue);

                                    String decryptedValue = securityUtil.getDecryptedValue(

                                                            encryptedValue, privateKey);

                                    System.out.println("Original Value------" + decryptedValue);

 

                        } catch (Exception e) {

                                    e.printStackTrace();

                        }

            }

 

}

 

This test harness class is used to encrypt and decrypt the long string contents. You can also use the same method for file encryption and decryption. First you have to read the contents of a file as String and then you can apply method to encrypt it.

 

Conclusion

I hope that you will enjoy my article for this asymmetric cryptography for RSA. For asymmetric cryptography please refer to the link http://www.articlesbase.com/...ation-technology-articles/asymmetric-cryptography-in-java-438155.html. If you find any problems or errors, please feel free to send me a mail in the address debadattamishra@aol.com . This article is only meant for those who are new to java development. This article does not bear any commercial significance. Please provide me the feedback about this article

Rate this Article: Current: 5 / 5 stars - 1 vote(s).

Article Source: http://www.articlesbase.com/programming-articles/encryption-using-rsa-algorithm-in-java-533881.html

Print this Article Print article   Email to a Friend Send to friend   Publish this Article on your Website Publish this Article   Send Author Feedback Author feedback  
Debadatta MishraAbout the Author:

Debadatta Mishra - An agressive java developer with a difference

Submitting articles has become one of the most popular means to drive traffic to your website and promote yourself and your business. Join us today - It's Free!

Article Comments

Comment on this article Comment on this article
Your Name
Your Email:
Comment Body
Enter Validation Code: Captcha

0
1. Patel (03:32, 17.10.2008)
Very good atricle in cryptography. Nicely explained.
0
2. Shekhar (02:44, 29.08.2008)
This articles forced me to think Encryption-Decryption is how easy !!

Thank's !!!

Got a Question? Ask.

Ask the community a question about this article:

Q&A Powered by:
Powered by Yedda 

Latest Programming Articles

Mvc Design Pattern
By: TuVinhSoft .,JSC | 14/11/2008
Model-view-controller (MVC) is an architectural pattern used in software engineering. In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (View) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.

Advantages of Low Cost Contract Programmers in Freelance Programming
By: Joanna Gadel | 12/11/2008
It observed that web industry is getting tougher thus the necessity of freelance contract programmer is required for developing more effective website with flexible features. This article states the fruitful advantages of freelance programmers in contract programming.

A Guide to Cnc Kits
By: Martin Applebaum | 09/11/2008
CNC kits are a way in which to construct your CNC machine. This article will provide some information on these machines.

A Guide to Cnc Tube Bending Machines
By: Martin Applebaum | 08/11/2008
Are you familiar with a CNC tube bending machine? This article will shed some light on the main function and components of this machine.

Ways to Hire Dedicated Php Programmers
By: Jucick | 08/11/2008
It’s not at all easy to hire dedicated PHP programmers unless you know where and how to find them. Whether you need to fix, update or enhance your website you naturally want the job done quick and right.

Top 4 Reasons Why Addressing Web Accessibility is Important
By: Matt Cave | 05/11/2008
There are very high chances that web accessibility is more important to the performance of your web site than you realize. Article takes a look at the top 4 reasons why it would be important to address the issue of web accessibility.

Xlphoto Printing, the Best Choice for Banner Printing and Poster Printing
By: Vikram Kumar | 01/11/2008
Suppose, you have visited a tourist spot or a hill station; over there, you have witnessed some of the mesmerizing locations of serene nature.

Simple & Complete Gridview Functionality(dotnet 2.0) by Syed Shees Abidi
By: Shees Abidi | 31/10/2008
My this article covers GridView functionality in a Complte,Simple & Precise way in ASP.NET(2.0),C#,SQLServer 2005.This article provides a single GridView which is capable of ~Displaying Data ~Updating it ~Deleting the existing records from database based on a Confirmation Message and ~Inserting a new record(which is beyond the implicit usage of GridView). NOTE: The application requires three simple stored proc. named empins,empupd,empdel for updation insertion and deletion respectively

More from Debadatta Mishra

Symmetric Cryptography in Java
By: Debadatta Mishra | 23/08/2008 | Programming
This article gives an insight into the symmetric cryptography in java. It basically provides you the use of DES algorithm.

Eclipse Plugin Development - a Bird’s Eye View
By: Debadatta Mishra | 08/08/2008 | Programming
This article gives a brief introduction into the eclipse plugin development. You will find some of the terms used in the eclipse plugin development.

Asymmetric Cryptography in Java
By: Debadatta Mishra | 04/06/2008 | Information Technology
An example for asymmetric cryptography in java

Object Persistence in Java
By: Debadatta Mishra | 16/05/2008 | Information Technology
An article on the persistence of plain old java object in the file system. I provide an approach to the freshers to store the entire java object. I have highlighted on XML Persistence as well as serialization.

Article Categories





Give Feedback

Sign up for our email newsletter

Receive updates, enter your email below