Skip to main content

lolcryption.py

gist link


"""
lolcryption.py
Translates text into LOLcrypted ciphertext. <http://lolcryption.master5o1.com/>
Created By:
    - Jason Schwarzenberger <https://github.com/master5o1>
License:
    MIT
"""

from cloudbot import hook
import math

@hook.command()
def enlolcrypt(text):
	""" enlolcrypt <text> -- enLOLcrypts <text> in <ciphertext>. """
	return tr(text, "aeioubcdfghjklmnpqrstvwxyz", "iouaenpqrstvwxyzbcdfghjklm")

@hook.command()
def delolcrypt(text):
	""" delolcrypt <ciphertext> -- deLOLcrypts <ciphertext> in <text>. """
	return tr(text, "iouaenpqrstvwxyzbcdfghjklm", "aeioubcdfghjklmnpqrstvwxyz")

@hook.command()
def detheucon(text):
    """ detheucon <text> -- unscrambles Theucon scrambled <text>. """
    return theucon_decrypt(text)
    
@hook.command()
def entheucon(text):
    """ entheucon <text> -- scrambles <text> using Theucon algorithm. """
    return theucon_encrypt(text)

def theucon_encrypt(text):
    output = ""
    remaining = text
    while len(remaining) > 0:
        primeIndexed = ""
        nonPrimeIndexed = ""
        for i in range(0, len(remaining)):
            if i == 0 or is_prime(i):
                primeIndexed = primeIndexed + remaining[i]
            else:
                nonPrimeIndexed = nonPrimeIndexed + remaining[i]
        output = output + primeIndexed
        remaining = nonPrimeIndexed
    return output

def theucon_decrypt(text):
    output = make_empty_list(len(text), '')
    remaining = text
    while len(remaining) > 0:
        primes = [0] + primes_until(len(remaining))
        currentOutput = make_empty_list(len(remaining), '')
        current = remaining[0:len(primes)]
        remaining = remaining[len(primes):]
        for i in range(0, len(primes)):
            currentOutput[primes[i]] = current[i]
        if len(output) == 0:
            output = currentOutput
        else:
            for i in range(0, len(output)):
                if output[i] == '':
                    output[i] = currentOutput[0]
                    currentOutput = currentOutput[1:]
    return str.join("", output)
        
def is_prime(n):
    if n < 2 or n % 2 == 0 and n > 2: 
        return False
    return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))


def primes_until(n):
    list = []
    for x in range(1, n):
        if is_prime(x):
            list.append(x)
    return list
    
def make_empty_list(n, sep=""):
    list = []
    for x in range(0, n):
        list.append(sep)
    return list

def tr(text, inAlphabet, outAlphabet):
	text = text.translate(str.maketrans(inAlphabet, outAlphabet))
	return text.translate(str.maketrans(inAlphabet.upper(), outAlphabet.upper()))