User Tools

Site Tools


encrypt

Salted Block Cipher for LSL

This page documents a simple, reversible salted block cipher implemented in Linden Scripting Language (LSL). It is not real cryptography, but it provides a stronger reversible encoding than XOR or Caesar ciphers and is suitable for in‑world chat obfuscation.

Overview

The cipher works by:

  • Splitting text into 8‑byte blocks
  • Deriving 4 round keys from a salt
  • Running each block through 4 rounds of:
    • XOR with round key
    • Adding the round number
    • Wrapping into byte range
  • Decrypting by reversing the same operations

The same salt must be used for both encryption and decryption.

Key Schedule

list makeRoundKeys(string salt)
{
    integer i;
    integer slen = llStringLength(salt);
    integer acc = 0;

    for (i = 0; i < slen; ++i)
    {
        acc = (acc + llOrd(salt, i)) & 0xFF;
        acc = (acc * 37 + 11) & 0xFF;
    }

    list keys = [];
    integer k = acc;

    for (i = 0; i < 4; ++i)
    {
        k = (k * 53 + 19) & 0xFF;
        keys += k;
    }

    return keys;
}

Padding

string padToBlock(string msg, integer blockSize)
{
    integer len = llStringLength(msg);
    integer pad = blockSize - (len % blockSize);
    if (pad == blockSize) return msg;

    string p = "";
    integer i;
    for (i = 0; i < pad; ++i)
        p += llChar(pad);

    return msg + p;
}

Encrypting a Block

string encrypt_block(string block, list roundKeys)
{
    integer i, r;
    integer blen = llStringLength(block);
    string out = block;

    for (r = 0; r < 4; ++r)
    {
        integer key = llList2Integer(roundKeys, r);
        string tmp = "";

        for (i = 0; i < blen; ++i)
        {
            integer c = llOrd(out, i);
            c = c ^ key;
            c = (c + r + 1) & 0xFF;
            tmp += llChar(c);
        }

        out = tmp;
    }

    return out;
}

Full Encryption Function

string salted_block_encrypt(string msg, string salt)
{
    integer blockSize = 8;
    list roundKeys = makeRoundKeys(salt);

    msg = padToBlock(msg, blockSize);

    integer len = llStringLength(msg);
    integer i;
    string out = "";

    for (i = 0; i < len; i += blockSize)
    {
        string block = llGetSubString(msg, i, i + blockSize - 1);
        out += encrypt_block(block, roundKeys);
    }

    return out;
}

Decrypting a Block

string decrypt_block(string block, list roundKeys)
{
    integer i, r;
    integer blen = llStringLength(block);
    string out = block;

    for (r = 3; r >= 0; --r)
    {
        integer key = llList2Integer(roundKeys, r);
        string tmp = "";

        for (i = 0; i < blen; ++i)
        {
            integer c = llOrd(out, i);
            c = (c - (r + 1)) & 0xFF;
            c = c ^ key;
            tmp += llChar(c);
        }

        out = tmp;
    }

    return out;
}

Unpadding

string unpad(string msg)
{
    integer len = llStringLength(msg);
    if (len == 0) return msg;

    integer pad = llOrd(msg, len - 1);
    if (pad <= 0 || pad > 8) return msg;

    return llGetSubString(msg, 0, len - pad - 1);
}

Full Decryption Function

string salted_block_decrypt(string msg, string salt)
{
    integer blockSize = 8;
    list roundKeys = makeRoundKeys(salt);

    integer len = llStringLength(msg);
    integer i;
    string out = "";

    for (i = 0; i < len; i += blockSize)
    {
        string block = llGetSubString(msg, i, i + blockSize - 1);
        out += decrypt_block(block, roundKeys);
    }

    return unpad(out);
}

Usage Example

string salt = "mySecretSalt42";

string encrypted = salted_block_encrypt("Hello multicode world!", salt);
string decrypted = salted_block_decrypt(encrypted, salt);
encrypt.txt · Last modified: by miko

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki