To the Top

ROT13 Letter/ROT5 Digit Substitution Cipher

The ROT13 (Caesar cipher by 13 chars) is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. The term cipher itself might be a bit misleading: The ROT13 algorithm is based on a rotation of all alphabetic characters by 13th (ASCII rotation). It is an invertible algorithm i.e. applying the same algorithm to the input twice will get the origin text.

This page provides a Javascript ROT13 implementation online utility.

Example: This page provides a ROT13+ROT5 javascript online encoder/decoder.
will be translated to:
Guvf cntr cebivqrf n EBG68+EBG0 wninfpevcg bayvar rapbqre/qrpbqre.

ROT13: same action can be used for encoding and decoding
ROT47 is a derivative of ROT13. ROT47 introduces mixed letters and symbols, therefore, the encoded text looks more obvious that text has been enciphered.
ROT13 is often combined with ROT5, which is used to encode and decode digits i.e. 0-9. The digits '0123456789' will be mapped to '5678901234' and vice versa. All numbers of the text are shifted by 5 positions. ROT5 also satisfies the following, encoding the digits twice will get the original text.
ROT5: same action can be used for encoding and decoding
ROT13 and ROT5 combined sometimes is also known as ROT18. The following is an online ROT13/ROT5 cipher implemented by Javascript.

ROT5 Implementation in Javascript

The ROT5 is easy to implement, the following Javascript code is straightforward.

function rot5(str) {
  var s = [];
  for (i = 0; i < str.length; i ++) {
    idx = str.charCodeAt(i);
    if ((idx >= 48) && (idx <= 57)) {
      if (idx <= 52) {
        s[i] = String.fromCharCode(((idx + 5)));
      } else {
        s[i] = String.fromCharCode(((idx - 5)));
      }
    } else {
      s[i] = String.fromCharCode(idx);
    }
  }
  return s.join('');
}

ROT13 Implementation in Javascript

The ROT13 can be easily implemented by modern programming language in many ways, e.g. using a lookup table. For example, the following Javascript code is a one-line implementation of ROT13.

s.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});

Alternatively, ROT13 can be implemented using Reg Expression.

String.prototype.rot13 = rot13 = function(s)
{
  return (s ? s : this).split('').map(function(_)
   {
  	if (!_.match(/[A-za-z]/)) return _;
  	c = Math.floor(_.charCodeAt(0) / 97);
  	k = (_.toLowerCase().charCodeAt(0) - 83) % 26 || 26;
  	return String.fromCharCode(k + ((c == 0) ? 64 : 96));
   }).join('');
}

ROT-n Implementation by Javascript

This online tool is also implemented in Javascript where you can encode and decode the string using ROT-1 to ROT-26

Implement ROT13 with PHP

PHP provides inbuilt str_rot13 function so you don't have to re-invent the wheel.

echo str_rot13("PHP has str_rot13()");

ROT13 Function in Python

String Cipher ROT13 can be implemented as follows:

def rot13(s):
    a = []
    for i in s:
        if i.isupper():
            a.append(chr(65 + (ord(i) - 65 + 13) % 26))
        elif i.islower():
            a.append(chr(97 + (ord(i) - 97 + 13) % 26))
        else:
            a.append(i)
    return "".join(a)
See Python Implementation of ROT13 Function

API (Application Programmer Interface)

The API following has a rate-limit 1 call per second.
https://str.justyy.workers.dev/rot13/?s=Hello ROT13
It will return JSON-encoded data:
"Uryyb EBG13"
If $_GET parameter s is not specified, this API will use the $_POST text instead.
curl -s -X POST "https://str.justyy.workers.dev/rot13/" -d "ROT13 Rocks!"

ROT-n Javascript Implementation

Instead of 13, the alphabeta can be shifted (n) positions to the left or to the right.

This page provides a Javascript Implementation of ROT-n Algorithm (Alpha-beta Shifting).

Windows Batch/MSDOS Assembly/Python
VBScript/Javascript Download

Use the ROT13 function in your application easily; rot13.bat - rot13.asm - rot13.py - rot13.vbs - rot13.js - rot13c.js (Compressed)

Link here!

Just paste the HTML code, shown below, onto the site of your choice.
    <a href="https://rot47.net/rot13.html" title="Online ROT13 Encoder/Decoder">Online ROT13 Encoder</a>
Plain text example:
Online ROT13 Encoder

Share This


Page Edited: