Sublime Forum

Rot18, encrypt and decrypt

#1

Hello all,

I am looking for a plugin that can encrypt/decrypt text using rot18 in Sublime Text v3.2.2.

I tried this tutorial (only rot13) but it doesn’t work for me: https://www.sublimetext.com/docs/plugin-examples

I tried a lot of plugins and the only one that works fine is:
(unfortunately it is rot47)

import sublime
import sublime_plugin

class Rot47Command(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel():
            if not region.empty():
                s = self.view.substr(region)
                s = ''.join(chr(33 + ((ord(ch) + 14) % 94)) for ch in s)
                self.view.replace(edit, region, s)

Does anyone have any functional plugin on rot18, please?

0 Likes

#2

if eventually you have to write your own, you can simply use codes from https://pypi.org/project/rot-codec/#files as a module with click-related codes removed.

0 Likes

#3

I can’t write in python unfortunately :-/
I am looking for a finished solution.

0 Likes

#4

Following @jfcherng’s link, we can write a plugin in less than 50 lines of Python.

Save this in your Packages/User/ directory:

import sublime
import sublime_plugin

a = ord('a')
z = ord('z')
A = ord('A')
Z = ord('Z')
i0 = ord('0')
i9 = ord('9')
ROT_MIN = 33  # !
ROT_MAX = 126  # ~

RANGES = {
    5: ((i0, i9, 5),),
    13: ((A, Z, 13), (a, z, 13)),
    18: ((i0, i9, 5), (A, Z, 13), (a, z, 13)),
    47: ((ROT_MIN, ROT_MAX, 47),)
}


def rot(text, rot_ranges):
    result = ""
    for c in text:
        ord_c = ord(c)
        transformed = False
        for rot_range in rot_ranges:
            if rot_range[0] <= ord_c <= rot_range[1]:
                ord_c = (rot_range[1] + ord_c + rot_range[2] - 2 * rot_range[0] + 1) % (rot_range[1] - rot_range[0] + 1) + rot_range[0]
                result += chr(ord_c)
                transformed = True
                break
        if not transformed:
            result += c
    return result


class RotCommand(sublime_plugin.TextCommand):

    def run(self, edit, type):
        rot_ranges = RANGES.get(type)
        if rot_ranges:
            for region in self.view.sel():
                transformed_text = rot(self.view.substr(region), rot_ranges)
                self.view.replace(edit, region, transformed_text)
        else:
            sublime.status_message('"type" must be 5, 13, 18 or 47')

Add to your keybindings:

	{"keys": ["ctrl+t"], "command": "rot", "args": {"type": 18}},

Now select text and hit CTRLT and it’ll transform the selected text using rot18. The "type" argument in the keybinding can be 5, 13, 18 or 47.

By the way, rot is not encryption/decryption. It’s a trivially reversible operation. Rot is its own inverse! Don’t use it for encryption!

3 Likes

#5

Thank you, it works perfectly

0 Likes