Don't click here unless you want to be banned.

LSL Wiki : LibraryVigenereCipher

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl423.us.archive.org
A Vigenere cipher. It works by taking the key that you input, repeating it to the same length of the text, and then rotates the alphabet with which it encrypts. You can read more about it here.

Thanks, BlindWanderer for the tip about using strings as opposed to lists.
string _key = "goat";

string keyRepeat(string text, string ky){
    integer i;
    integer j;
    integer l = llStringLength(text);
    string out;
    for(i = 0; i <= l; ++i){
        out += llGetSubString(ky,j,j);
        if(j == llStringLength(ky)){
            j = 0;
        } else {
            ++j;
        }
    }
    return out;
}

string encrypt(string text, string ky){
    string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. ,!?&";
    ky = keyRepeat(text,ky);
    list t = llParseString2List(text,[],[]);
    integer l = llGetListLength(t);
    integer i;
    string o;
    integer prev;
    for(i = 0; i < l; ++i){
        integer x;
        integer y = llStringLength(llList2String(t,i));
        string ky_temp = llGetSubString(ky,prev,llStringLength(llList2String(t,i)));
        prev = llStringLength(llList2String(t,i));
        for(x = 0; x < y; ++x){
            integer text_p = llSubStringIndex(alphabet,llGetSubString(llList2String(t,i),x,x));
            integer key_p = llSubStringIndex(alphabet,llGetSubString(ky_temp,x,x));
            
            integer pos = (text_p + key_p)%57;
            
            o += llGetSubString(alphabet,pos,pos);
        }
        o += " ";
    }
    return o;
}

string decrypt(string text, string ky){
    string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. ,!?&";
    ky = keyRepeat(text,ky);
    list t = llParseString2List(text,[],[]);
    integer l = llGetListLength(t);
    integer i;
    string o;
    integer prev;
    for(i = 0; i < l; ++i){
        integer x;
        integer y = llStringLength(llList2String(t,i));
        string ky_temp = llGetSubString(ky,prev,llStringLength(llList2String(t,i)));
        prev = llStringLength(llList2String(t,i));
        for(x = 0; x < y; ++x){
            integer text_p = llSubStringIndex(alphabet,llGetSubString(llList2String(t,i),x,x));
            integer key_p = llSubStringIndex(alphabet,llGetSubString(ky_temp,x,x));
            
            integer pos = (text_p - key_p + 57)%57;
            
        
            
            o += llGetSubString(alphabet,pos,pos);
        }
        o += " ";
    }
    return o;
}


default{
    state_entry(){
        llListen(0,"",llGetOwner(),"");
        llSetObjectName("Cipher");
    }
    listen(integer c, string n, key i, string ms){
        list m = llParseString2List(ms,[":"],[]);
        if(llList2String(m,0) == "encrypt"){
            llSay(0,encrypt(llList2String(m,1),_key));
        } else if(llList2String(m,0) == "decrypt"){
            llSay(0,decrypt(llList2String(m,1),_key));
        } else if(llList2String(m,0) == "key"){
            _key = llList2String(m,1);
            llSay(0,"Key set to \""+_key+"\".");
        }
    }
}
Comments [Hide comments/form]
I recommend replacing your alphabet list with a single string, it will run faster and take up less script memory. You would use llSubStringIndex instead of llListFindList and llGetSubString, instead of llList2String, in appropriate places.
-- BlindWanderer (2006-04-30 10:48:56)
it would be interesting to have an example of "before" and "after" the process instead of jsut the code (unfortunatly right now I don't have time to log and run it to post here the examples..... :/
-- 201-95-56-183.dsl.telesp.net.br (2007-05-16 18:15:36)
Attach a comment to this page: