Java Implementation – ROT-13 Encoding Algorithm

ROT-13 is the simplest of all encoding algorithms. The idea is simple, rotate the characters by 13. If you assume characters A-Z (or a-z) to be in a circle, for any characters, go to 13 characters ahead in the circle and replace it with that character.

The following diagram taken from Wikipedia (under wikipedia commons license), demonstrates the character substitution fact clearly.

Interestingly, if you encode a plain text, to decode it, you need to re-encode it.

That means for a character ABC, encode (encode( ABC)) is ABC

The following is the Java Implementation of ROT-13 Algorithm. Since having only A-Z or a-z characters may not suffice, I have added a condition where other characters will remain the same.

/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified on 5th September 2008
 */
package com.kushal.utilities;
/**Java Implementation of ROT-13 Encoding algorithm**/
public class Rot13Encryption {

	public static void main(String args[]) {
		//Original Text
		String plainText="Sanjaal.Com";
		//Let's Encode It
		String encodedText=rot13Encode(plainText);
		//Then Decide It
		String decodedText=rot13Encode(encodedText);

		System.out.println("Original Text: tt"+plainText);
		System.out.println("After ROT-13 Encoding: t"+encodedText);
		System.out.println("After ROT-13 Decoding: t"+decodedText);
	}

	/**
	 * @param textToEncode
	 * @return encoded (or decoded)text.
	 * Note: if you encode a text, and encode the result again,
	 * you will get the original text.
	 */
	public static String rot13Encode(String textToEncode) {
		String encodedText = "";
		int textLength = textToEncode.length();

		for (int i = 0; i < textLength; i++) {
			char currentChar = textToEncode.charAt(i);
			if ((currentChar >= 65 && currentChar <= 77)
					|| (currentChar >= 97 && currentChar <= 109)) {
				encodedText += (char) (currentChar + 13);
			} else if ((currentChar >= 78 && currentChar <= 90)
					|| (currentChar >= 110 && currentChar <= 122)) {
				encodedText += (char) (currentChar - 13);
			} else {
				encodedText += currentChar;
			}
		}
		return encodedText;
	}

}


—————————————-
Here is the sample output:
—————————————-

Original Text:                     Sanjaal.Com
After ROT-13 Encoding:     Fnawnny.Pbz
After ROT-13 Decoding:     Sanjaal.Com

Calling URL Browser From Java Application

This program teaches you how you can call the browser in your OS to open the URL that you are passing from your Java application. This program is fully compiled and tested.

package com.kushal.utilities;

/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified On: 26th August, 2008
 */
import java.lang.reflect.Method;

import javax.swing.JOptionPane;

public class OpenDefaultBrowserFromJava {
	private static final String errMsg = "ERROR WHILE ATTEMPTEING TO LUNCH WEB BROWSER";
	private static final String errFindingBrowser="COULD NOT FIND THE BROWSER";
	
	public static void main(String args [])
	{
		String urlToOpen="http://www.sanjaal.com/java";
		openURL(urlToOpen);
	}

	/**
	 * This method takes URL as a parameter and tries to
	 * identify Operating system and then lunches the broswer
	 * with the given URL
	 */
	public static void openURL(String url) {
		String operatingSystem = System.getProperty("os.name");
		try {
			/*
			 * Check if it is windows OS 
			 */
			if (operatingSystem.startsWith("Windows"))
				Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
			/*
			 * If not windows OS, Check if it is a Mac OS
			 */
			else if (operatingSystem.startsWith("Mac OS")) {
				Class fileManager = Class.forName("com.apple.eio.FileManager");
				Method openURL = fileManager.getDeclaredMethod("openURL",
						new Class[] { String.class });
				openURL.invoke(null, new Object[] { url });
			}
			
			/*
			 * Or, it might be some unix or linux OS
			 */
			else { 
				/**
				 * There are different browsers possible.
				 */
				String[] browsers = { 	"firefox", 
										"netscape",
										"opera", 
										"konqueror", 
										"epiphany", 
										"mozilla"};
				String browser = null;
				
				for (int count = 0; count < browsers.length && browser == null; count++)
					if (Runtime.getRuntime().exec(
							new String[] { "which", browsers[count] })
							.waitFor() == 0)
						browser = browsers[count];

				if (browser == null) 
					throw new Exception(errFindingBrowser);
				else
					Runtime.getRuntime().exec(new String[] { browser, url });
			}

		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, errMsg + ":n" + e.getMessage());
		}
	}

}