## 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
*/
package com.kushal.utilities;
/**Java Implementation of ROT-13 Encoding algorithm**/
public class Rot13Encryption {

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

System.out.println(&quot;Original Text: tt&quot;+plainText);
System.out.println(&quot;After ROT-13 Encoding: t&quot;+encodedText);
System.out.println(&quot;After ROT-13 Decoding: t&quot;+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 = &quot;&quot;;
int textLength = textToEncode.length();

for (int i = 0; i &amp;lt; textLength; i++) {
char currentChar = textToEncode.charAt(i);
if ((currentChar &amp;gt;= 65 &amp;amp;&amp;amp; currentChar &amp;lt;= 77)
|| (currentChar &amp;gt;= 97 &amp;amp;&amp;amp; currentChar &amp;lt;= 109)) {
encodedText += (char) (currentChar + 13);
} else if ((currentChar &amp;gt;= 78 &amp;amp;&amp;amp; currentChar &amp;lt;= 90)
|| (currentChar &amp;gt;= 110 &amp;amp;&amp;amp; currentChar &amp;lt;= 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
*/
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());
}
}

}
```