To the Top

ROT47 Character Substitution Cipher

The ROT47 (Caesar cipher by 47 chars) is a simple character substitution cipher that replaces a character within the ASCII range [33, 126] with the character 47 character after it (rotation) in the ASCII table. It is an invertible algorithm i.e. applying the same algorithm to the input twice will get the origin text.
The page provides a Javascript implementation online ROT47 encoder/decoder.
Example: This page provides a Javascript online web-based ROT47 Encoder/Decoder.
will be translated to:
%9:D A286 AC@G:56D 2 y2G2D4C:AE @?=:?6 H63\32D65 #~%cf t?4@56C^s64@56C]

ROT47: 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. The following is an online ROT47 cipher implemented by Javascript.

ROT47 Cipher Implementation in PHP

The ROT47 can be easily implemented by modern programming language in many many ways, e.g. using a lookup table. For example, the following PHP code uses strstr to convert the text by using a lookup table.

function str_rot47($str) {
  return strtr($str, 
    '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~', 
    'PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO'
  );
}

For more details, please refer to this post: The PHP str_rot47 implementation, and here is a PHP Rot47 Online Encoder/Decoder.

ROT47 Cipher Function In Object-Pascal/Delphi

Alternatively, ROT47 can be implemented by computing directly the ROT47-ed ASCII of each given character.

function Rot47(const s: string): string;
var
  i, j: integer;
begin
  Result := s;
  for i := 1 to Length(s) do
  begin
    j := Ord(s[i]);
    if (j in [33..126]) then
    begin
      Result[i] := Chr(33 + ((j + 14) mod 94));
    end;
  end;
end;

ROT47 Implemnetation in Javascript / NodeJs

Someone is nice to wrap this up and provide it free in NPM (Node Package Manager) in NodeJS.
module.exports = function rot47(x)
{var s=[];for(var i=0;i<x.length;i++)
{var j=x.charCodeAt(i);if((j>=33)&&(j<=126))
{s[i]=String.fromCharCode(33+((j+ 14)%94));}
else
{s[i]=String.fromCharCode(j);}}
  return s.join('');}

See more details in Node Tool for ROT47

Example Usage:

gzip -d -c file.txt | base64 -D | rot47 > orig.txt

ROT47 Cipher Function in LUA

Here is the ROT47 Implementation for LUA scripting language:

  local str = io.read("a")
  io.write(str:gsub('.', function(c)
  local ch = string.byte(c)
  if ch > 32 and ch <= 126 then
  return string.char(((ch - 32) + 47) % 94 + 32)
  end
  return c
  end))  

And one-liner in LUA:

lua -e 'io.write(io.read("a"):gsub(".", function(c) ch = string.byte(c) return ch > 32 and ch <= 126 and string.char(((ch - 32) + 47) % 94 + 32) or d end))' <<< input-string

ROT47 Cipher Implementation in Python

The following is a classic ROT47 cipher implementation in Python.

def rot47(s):
    x = []
    for i in range(len(s)):
        j = ord(s[i])
        if j >= 33 and j <= 126:
            x.append(chr(33 + ((j + 14) % 94)))
        else:
            x.append(s[i])
    return ''.join(x)

ROT47 Cipher Program in C/C++

We can make a ROT47 cipher a command line tool using gcc/g++ compiler:

void rot47(char *buf, int l) {
    for (int i = 0; i < l; ++ i) {
        if (buf[i] >= 33 && buf[i] <= 126) {
            buf[i] = 33 + ((buf[i] + 14) % 94);
        }
    }
}

ROT47 Cipher Function in C#

public static string rot47(string value) {
    var buff = value.ToCharArray();
    if (buf[i] >= 33 && buf[i] <= 126) {
        buf[i] = 33 + ((buf[i] + 14) % 94);
    } 
    return new string(buf);
}

ROT47 Implementation in PostgreSQL

Some offers the PostgreSQL plpgsql function that implements the ROT47 cipher:
CREATE OR REPLACE FUNCTION public.rot47(character varying)
RETURNS character varying AS
$BODY$
-- SAMPLE
/*
SELECT public.rot47($$Hello$$);
SELECT public.rot47($$w6==@$$);
*/
DECLARE
s ALIAS FOR $1;
i INTEGER;
j INTEGER;
Result VARCHAR[];
BEGIN
FOR i IN 1..length(s) LOOP
j := ascii(substr(s,i,1));
IF (j >= 33 AND j <= 126) THEN
Result := array_append(Result, Chr(33 + mod(j + 14, 94))::varchar);
ELSE
Result := array_append(Result, substr(s,i,1)::varchar); 
END IF;
END LOOP;
RETURN array_to_string(Result, '');
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 1;
ALTER FUNCTION public.rot47(character varying)
OWNER TO postgres;

ROT47 Cipher API (Application Programmer Interface)

You can use the following CloudFlare Serveless Function to Invoke the ROT47 Cipher API. You would need to supply the parameter of input s:

https://str.justyy.workers.dev/rot47/?s=Hello ROT47
It will return JSON-encoded data:
"w6==@ #~%cf"
If $_GET parameter s is not specified, this API will use the $_POST text instead.
curl -X POST "https://str.justyy.workers.dev/rot47/" -d "ROT47 Rocks!"

ROT47 Function/Library Download

Use the ROT47 function in your application easily; rot47.pas - rot47.py - rot47.vbs (Is VBScript Dead?) - rot47.js - rot47c.js (Compressed)

Link here!

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

Share This


Page Edited: