VIGENERE Encryption

  • wasEncryptVIGENERE takes as parameter a plain-text and an encryption key and returns cypher-text.
  • wasDecryptVIGENERE takes as parameter a cypher-text and an encryption key and returns plain-text.
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!.


fuss/lsl/cryptography/cyphers/vigenere.txt ยท Last modified: 2022/04/19 08:28 by 127.0.0.1

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.