VIGENERE Encryption

VIGENERE.lsl
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
list wasReversePermuteListElements(list input, integer times) {
    if(times == 0) return input;
    return wasReversePermuteListElements(
         llList2List(input, 1, -1) + llList2String(input, 0), 
        --times
    );
}
 
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
integer wasIsUpper(string a) {
    if(a == "") return FALSE;
    integer x = llBase64ToInteger("AAAA" + 
        llStringToBase64(llGetSubString(a, 0, 0)));
    return x >= 65 && x <= 90;
}
 
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
integer wasIsAlpha(string a) {
    if(a == "") return FALSE;
    integer x = llBase64ToInteger("AAAA" + 
        llStringToBase64(llGetSubString(a, 0, 0)));
    return (x >= 65 && x <= 90) || (x >= 97 && x <= 122);
}
 
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
string wasVIGENEREExpandKey(string input, string enc_key) {
    // expand the key
    string exp_key = "";
    integer i = 0;
    integer j = 0;
    do {
        string p = llGetSubString(input, i, i);
        if(!wasIsAlpha(p)) {
            exp_key += p;
            jump continue;
        }
        integer m = j % llStringLength(enc_key);
        exp_key += llGetSubString(enc_key, m, m);
        ++j;
@continue;
        ++i;
    } while(i<llStringLength(input));
 
    return exp_key;
}
 
 
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
string wasEncryptVIGENERE(string input, string enc_key) {
    list a = 
        [ 
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
            "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" 
        ];
 
    // expand the key
    enc_key = wasVIGENEREExpandKey(input, enc_key);
 
    string result = "";
    integer i = 0;
    do {
        string p = llGetSubString(input, i, i);
        if(!wasIsAlpha(p)) {
            result += p;
            jump continue;
        }
        string q = 
            llList2String(
                wasReversePermuteListElements(
                    a, 
                    llListFindList(
                        a, 
                        (list)llGetSubString(
                            enc_key, 
                            i, 
                            i
                        )
                    )
                ), 
                llListFindList(
                    a, 
                    (list)llToLower(p)
                )
            );
        if(wasIsUpper(p)) {
            q = llToUpper(q);
        }
        result += q;
@continue;
        ++i;
    } while(i<llStringLength(input));
 
    return result;
}
 
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
string wasDecryptVIGENERE(string input, string enc_key) {
    list a = 
        [ 
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
            "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" 
        ];
 
    // expand the key
    enc_key = wasVIGENEREExpandKey(input, enc_key);
 
    string result = "";
    integer i = 0;
    do {
        string p = llGetSubString(input, i, i);
        if(!wasIsAlpha(p)) {
            result += p;
            jump continue;
        }
        string q =  
            llList2String(
                a, 
                llListFindList(
                    wasReversePermuteListElements(
                        a, 
                        llListFindList(
                            a, 
                            (list)llGetSubString(
                                enc_key, 
                                i, 
                                i
                            )
                        )
                    ), 
                    (list)llToLower(p)
                )
            );
        if(wasIsUpper(p)) {
            q = llToUpper(q);
        }
        result += q;
@continue;
        ++i;
    } while(i<llStringLength(input));
 
    return result;
}

Symmetry

A consistency test call that first encrypts the string It is a good day! and then decrypts the string would be:

        llOwnerSay(
            wasDecryptVIGENERE(
                wasEncryptVIGENERE(
                    "It is a good day!",
                    "was"
                ),
                "was"
            )
        );

which should return It is a good day!.