ROT13 (Subtitution Cipher)

This site aims to provide an educatiounal side of learning the ROT13 cipher, it is a subset of the ceaser cipher which shifts according to 26 letters. The source code provided is very different from online sources as they don't show the entire process in detail. I will be looking at creating this cipher for Arabic users in the near future.

How the cipher works

This is the list of letters required for this cipher

a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

In this example we will be using the word "John"

The first letter that occurs in the name is "J". We locate the position in the table above we go for 9. To perform the substitution we add 13 (in ceaser 26) which yields 22. So the result would be 22 mod 26 which return 22 in our case the letter w. That is it.

Reversing you take similar approach but you subtract 13 instead of adding. NOTE: Negative numbers have different syntax approaches for most languages.

public class Rot13 {
	private String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
			"r", "s", "t", "u", "v", "w", "x", "y", "z" };
	public enum TYPE{ENCRYPTION,DECRYPTION} ;
	public Rot13() {
		super();
	}

	public String generateROT13(String plainTextStr,Rot13.TYPE type) {
		plainTextStr = plainTextStr.toLowerCase();
		String cipherItem = "";
		for (int i = 0; i < plainTextStr.length(); i++) {
			for (int j = 0; j < letters.length; j++) {
				if (plainTextStr.charAt(i) == letters[j].charAt(0)) {
					int position = 0;
					if(type == Rot13.TYPE.ENCRYPTION){
						position = ((j + 13) % letters.length);
					} else if(type== Rot13.TYPE.DECRYPTION){ // Just for clarification
						position = ((j - 13) % letters.length);
						
						if(position<0){
							position += letters.length; 
						}
						System.out.println("Position: " + position);
					}
					
					cipherItem += letters[position];
				}
			}
		}
		return cipherItem;
	}
	
	public static void main(String[] args) {
		Rot13 mRot = new Rot13();
		String encrypt = mRot.generateROT13("Kings College London",Rot13.TYPE.ENCRYPTION);
		System.out.println("Rot13 Encryption: " +encrypt);
		String decrypt = mRot.generateROT13(encrypt,Rot13.TYPE.DECRYPTION);
		System.out.println("Rot13 Decryption: " +decrypt);
	}	
}
@interface Rot13 : NSObject{
    NSString *plainText;
    NSArray *englishLettersArr;
    NSString *cipherText;
}
- (id)initwithPlainText:(NSString *) plainText;

@end

@implementation Rot13
- (id) initwithPlainText:(NSString *) plaintext{
    if(self){
      englishLettersArr=  @[@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z"];
        plainText = plaintext;
        plainText = [plainText lowercaseString];
    }
    return self;
}
- (void) encodeROT{
    int textLength = [plainText length];
    int arrSize = [englishLettersArr count];
    NSMutableString *cipherTextArr = [NSMutableString stringWithCapacity:textLength];
    for(int i=0;i<textLength;i++){
        for(int j=0;j<arrSize;j++){
            NSString *plainChar = [NSString stringWithFormat:@"%c", [plainText characterAtIndex:i]];
            NSString *letterChar = [englishLettersArr objectAtIndex:j];
            BOOL strChecker = ([plainChar isEqualToString:letterChar]);
            if(strChecker){
                int shift = ((j+13)%arrSize);
                NSString *cipherItem =[englishLettersArr objectAtIndex:shift];
                [cipherTextArr  appendString:[NSString stringWithFormat:@"%@", cipherItem]];
            }
        }
    }
    cipherText = cipherTextArr;
    NSLog(@"The Cipher Text is %@", cipherTextArr);
}
- (void) decodeCipher{
    if (cipherText != nil){
        int textLength = [cipherText length];
        int arrSize = [englishLettersArr count];
        NSMutableString *plainTextArr = [NSMutableString stringWithCapacity:textLength];
        for(int i=0;i<textLength;i++){
            for(int j=0;j<arrSize;j++){
                NSString *plainChar = [NSString stringWithFormat:@"%c", [plainText characterAtIndex:i]];
                NSString *letterChar = [englishLettersArr objectAtIndex:j];
                BOOL strChecker = ([plainChar isEqualToString:letterChar]);
                if(strChecker){
                    int shift = ((j-13)%arrSize);
                    if(shift <0){
                        shift += 13;
                    }
                    NSString *cipherItem =[englishLettersArr objectAtIndex:shift];
                    [plainTextArr  appendString:[NSString stringWithFormat:@"%@", cipherItem]];
                }
            }
        }
        
        NSLog(@"The plaintext is %@" , [plainText capitalizedString]);
        
        
    }else{
        NSLog(@"You have no cipher text  available");
    }
}

@end

int main(int argc, const char * argv[]) {
    Rot13 *encode = [[Rot13 alloc] initwithPlainText:@"Kings College London"];
    [encode encodeROT];
    [encode decodeCipher];
    return 0;
}
var letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
function encodePlainText(plainText){
	var cipherText= "";
	var size = letters.length;
	plainText = plainText.toLowerCase();
	for(var i=0;i<plainText.length;i++){
		for(var j=0;j<size;j++){
			if(plainText.charAt(i) == letters[j].charAt(0)){
				var shift = ((j+13)%size);
				cipherText += letters[shift];
			}
		}
	}
	console.log(cipherText);
	return cipherText;
}
function decodeCipher(cipherText){
	var plainText= "";
	var size = letters.length;
	cipherText = cipherText.toLowerCase();
	for(var i=0;i<cipherText.length;i++){
		for(var j=0;j<size;j++){
			if(cipherText.charAt(i) == letters[j].charAt(0)){
				var shift = ((j-13)%size);
				if(shift<0){
					shift +=size;
				}
				plainText += letters[shift];
			}
		}
	}
	console.log(plainText);
	return plainText;
}