Need help in RSA encryption and decryption programming using Java 1.5 and above.

channabasanna

Disciple
Hi All,

My friend needs to write a program for encryption and decryption using Java. Can anyone of you people give me the code for the same.

What the program has to do is
I enter the String as "techenclave" it should give me a encrypted value.
If i use this encrypted value and pass it to the program, it should decrypt and give back the value as "techenclave".

I am not able to give a try for the same, due to the amount of work. It would be of great help if you people help me in this.

Thanks in Advance,
Channabasanna
 
Have you tried searching google for "java crypto sample code" or something?

Otherwise check out bouncycastle.org - maybe they have some samples...

Also a nice primer on crypto programming should help you get started, like Bruce Schnier's Applied Cryptography book, or my old tech talk slides: Slide 1 :D

Typically RSA being an asymmetric (slower) algorithm, you don't use it for encryption/decryption, rather use a symmetric (faster) algorithm like AES/Rijndael and the secret key is protected using RSA.

--- Updated Post - Automerged ---

Yeah, try bouncycastle.org and Home - The Legion of the Bouncy Castle
 
My friend did try this and it is working fine as per the requirement:

Code:
package rsaexample;

import java.util.*;

public class Encoder {

	public static void main(String[] args) {
		String message; // Line of text entered by user.
		Scanner sc = new Scanner(System.in);
		String codeWord;
		System.out.println("Enter the code word: ");
		codeWord = sc.nextLine();
		System.out.println("(E)ncode or (D)ecode? ");
		String encode = sc.nextLine();
		if (encode.equals("E") || encode.equals("e"))
			System.out.println(encodeHexString(codeWord));
		else
			System.out.println(decodeHexString(codeWord));

	}

	public static String encodeHexString(String sourceText) {

		byte[] rawData = sourceText.getBytes();
		StringBuffer hexText = new StringBuffer();
		String initialHex = null;
		int initHexLength = 0;

		for (int i = 0; i < rawData.length; i++) {
			int positiveValue = rawData[i] & 0x000000FF;
			initialHex = Integer.toHexString(positiveValue);
			initHexLength = initialHex.length();
			while (initHexLength++ < 2) {
				hexText.append("0");
			}
			hexText.append(initialHex);
		}
		return hexText.toString();
	}

	public static String decodeHexString(String hexText) {

		String decodedText = null;
		String chunk = null;

		if (hexText != null && hexText.length() > 0) {
			int numBytes = hexText.length() / 2;

			byte[] rawToByte = new byte[numBytes];
			int offset = 0;
			int bCounter = 0;
			for (int i = 0; i < numBytes; i++) {
				chunk = hexText.substring(offset, offset + 2);
				offset += 2;
				rawToByte[i] = (byte) (Integer.parseInt(chunk, 16) & 0x000000FF);
			}
			decodedText = new String(rawToByte);
		}
		return decodedText;
	}
}

Following was the input i tested: The output is from the Eclipse console which i am pasting here.

Run 1 to Encode the Text: initHexLength = initialHex.length();
Enter the code word:
initHexLength = initialHex.length();
(E)ncode or (D)ecode?
E
696e69744865784c656e677468203d20696e697469616c4865782e6c656e67746828293b


Run 2 to Decode the Text: 696e69744865784c656e677468203d20696e697469616c4865782e6c656e67746828293b
Enter the code word:
696e69744865784c656e677468203d20696e697469616c4865782e6c656e67746828293b
(E)ncode or (D)ecode?
D
initHexLength = initialHex.length();
 
Ah, then your friend definitely needs to read up on the basics of cryptography (try my slides link or wikipedia) before moving on to crypto programming because the above looks like regular (hex) encoding/decoding from one format to another and not encryption/decryption.
 
Back
Top