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
 * 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

[My Video Promotion]
Tagged , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

9 Responses to Tutorial – Encryption And Decryption Using DESede (Triple DES) In Java

  1. Modha says:

    Hello Kushal

    I enjoyed reading your articles ( and jokes ) at sanjaal.

    I have a question on encryption and decryption

    will this work only on UTF-8?

    I want to write a utility which encrypts a column in a database table.

    Can I use this program to read string from a database table – encrypt it and – write it back.

    Will it work if database in not in UTF-8, (I am trying to ask if it can encode some other charecter set like chinese or japanese)?
    I am trying to learn what these charecter encodings mean, excuse me if my question are basic.

    Hope to get a reply.


    • kushalzone says:

      Thank you Modha for enjoying my blogs at Sanjaal.

      Yes, the program is capable of handling the other character sets like ASCII, UTF-16 etc. UTF-8 is the unicode format, so most probably the japanese or chinese characters will be in this Character Set.

      If you know exactly what character set your database is using, you can pass that onto the getBytes () method as parameter, and do the encryption/decryption. The character sets will be defined while creating the database or database tables.

      For example, if your database character set is UTF-16, you can modify the above program where it says

      byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);


      byte[] plainText = unencryptedString.getBytes(“UTF-16”);

      Or, if the character set defined is ASCII, you simply define the line as:

      byte[] plainText = unencryptedString.getBytes(“ASCII”);

      Also make sure you modify the following line accordingly:
      keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);

      I hope it helps.

  2. Raanan says:


    Thanks for the post !!

    I have 2 questions:

    i. in your your example you wrote “…In the following tutorial, we have used Keying Option 3, where all the keys are identical.” – what make the code use option 3?

    ii. if I have a byte array that is used to build a DESedeKeySpec instance, can I tell which length is the encryption key (168 or 112)?


  3. Minal says:


    This article is very helpful. I am trying to encrypt and decrypt using DES but getting following error
    javax.crypto.BadPaddingException: Given final block not properly padded
    I tried all possible ways but problem is not solved yet. I appreciate if you could hlep me in this regards


  4. Send the code that you are trying to run, I will have a look at it.

  5. Minal says:

    code to get cipher is
    key = InternalSecretKey.getInstance();
    //keyspec = InternalSecretKey.getInstance();
    System.out.println(“Key is:”+key);

    byte[] iv = new byte[]{
    (byte)0x8E, 0x12, 0x39, (byte)0x9C,
    0x07, 0x72, 0x6F, 0x5A
    //byte[] ivBytes = null;
    //IvParameterSpec iv = new IvParameterSpec(ivBytes);
    //IvParameterSpec iv = null;

    //ecipher = Cipher.getInstance(“DES”);
    //dcipher = Cipher.getInstance(“DES”);

    ecipher = Cipher.getInstance(“DES/CBC/NoPadding”);
    dcipher = Cipher.getInstance(“DES/CBC/NoPadding”);

    AlgorithmParameterSpec algparam = new IvParameterSpec(iv);
    //IvParameterSpec iv = null;

    System.out.println(” iv ====================” + ecipher.getIV());
    //iv = new IvParameterSpec(ivBytes);

    //IvParameterSpec iv2 = new IvParameterSpec(dcipher.getIV());

    AlgorithmParameters algparamdecrypt = ecipher.getParameters();
    ecipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key, algparam);
    dcipher.init(javax.crypto.Cipher.DECRYPT_MODE, key, algparam);

    I tryed padding input stream before encryptin

    byte[] padding = null;
    byte padded[] = null;
    if (null != fileData) {

    System.out.println(“length of byte arrary before ency but before padding ” + fileData.length);
    if((fileData.length % 8) > 0){
    int offset = ((fileData.length % 8));
    System.out.println(” offset is ” + offset);
    padding = new byte[((8-offset) + fileData.length)];
    System.out.println(“lenght of fileData is ” + fileData.length);
    System.out.println(“lenght of padding is ” + padding.length);
    System.arraycopy(fileData, 0, padding, 0, fileData.length);
    for(int i = fileData.length; i < (fileData.length + (8-offset)); i++){
    padding[i] = (byte)0xbb;
    System.out.println(" padding " + padding.toString());
    System.out.println(" fileData " + fileData.toString());

    System.out.println("length of byte arrary before ency but after padding ==" + padding.length);

    AesEncryptionDecryptionUtil endeUtil = new AesEncryptionDecryptionUtil();
    byte[] fileDataEnc = null;
    if(padding != null){
    fileDataEnc = ecipher.doFinal(padding);
    fileDataEnc = ecipher.doFinal(fileData);
    System.out.println("Bytes encrypted:"+fileDataEnc.toString());

    With this padding logic code might work for .txt, .doc and .xls. However if I want to ebcrypt pdf, image files or zip files what is to be done?

    Thanking in advance

  6. Minal says:

    I would also like to know that if I am using cipher to encrypt the data, and encrypted data is stored in file/database. Next time when I decrypt the data do I need same instance of cipher which was used to encrypt the data (ofcourse with mode as decryption) or differnect instance created by passing same parameters can be used?

  7. webuser says:

    Getting wroung key size
    Exception in thread “main” Wrong key size

  8. Fabian says:


    I have a question,

    Which key do you use in your sample? I need decrypt it:

    Text: D64A1C4C83C16917
    Key: CF21BCFB0C4CB6E3

    I tried to use some web for this but I can´t do it.

    Can you help me, thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.