Skip to content

Instantly share code, notes, and snippets.

@josephernest
Created January 26, 2017 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephernest/ea79e7ef7ef652194d4db66d1ae17010 to your computer and use it in GitHub Desktop.
Save josephernest/ea79e7ef7ef652194d4db66d1ae17010 to your computer and use it in GitHub Desktop.
Sublime Text plugin that adds simple encryption/decryption with password. Available with CTRL+SHIFT+P as "Eeencode" and "Dddecode"
# Based on http://stackoverflow.com/a/16321853/1422096
# Added a few things to support UTF8.
#
# Install:
# 1) Put the file in C:\Users\***\AppData\Roaming\Sublime Text 2\Packages\User
# 2) Add a reference in C:\Users\***\AppData\Roaming\Sublime Text 2\Packages\User\Default.sublime-commands:
# [{ "caption": "Eeencode", "command": "eeencode" }, { "caption": "Dddecode", "command": "dddecode" }]
import sublime, sublime_plugin
import base64
key = 'abcd1234'
class EeencodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
clear = self.view.substr(region)
clear = base64.urlsafe_b64encode(clear.encode('utf8'))
enc = []
for i in range(len(clear)):
key_c = key[i % len(key)]
enc_c = chr((ord(clear[i]) + ord(key_c)) % 256)
enc.append(enc_c)
s = base64.urlsafe_b64encode("".join(enc))
self.view.replace(edit, region, s)
class DddecodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
enc = str(self.view.substr(region))
dec = []
enc = base64.urlsafe_b64decode(enc)
for i in range(len(enc)):
key_c = key[i % len(key)]
dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256)
dec.append(dec_c)
s = "".join(dec)
s = base64.urlsafe_b64decode(s).decode('utf8')
self.view.replace(edit, region, s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment