Difference between revisions of "Basic Encryption Modules"

From Second Life Wiki
Jump to navigation Jump to search
(Some optimization, I am highly suspect of the math involved.)
Line 2: Line 2:




I have posted 3 sections to this code, the encryption, decyption, and also a 3rd file. While the 3rd is unneccessary, since the value for the next channel to communicate on is changed each time data is sent, i found it annoying that when you edited the code to change the number, or tweak minor things, when it was saved, it reloaded the default values. The third script does nothing more than store the next channel to communicate on and pass it to either the encrypter or decypter when that script is reset. If you choose to use it, it should be placed in both prims that you are using.
I have posted 3 sections to this code, the encryption, decryption, and also a 3rd file. While the 3rd is unnecessary, since the value for the next channel to communicate on is changed each time data is sent, i found it annoying that when you edited the code to change the number, or tweak minor things, when it was saved, it reloaded the default values. The third script does nothing more than store the next channel to communicate on and pass it to either the encrypter or decypter when that script is reset. If you choose to use it, it should be placed in both prims that you are using.


This code basically takes a float, converts it to an alpha key, randomizes it, than takes a random channel, converts it to an alpha key, randomizes it, appends it to the string for the float, and shouts it to another prim, which decrypts it and returns the value of the float, as well as change it's listen to the next randomly determined channel. In this incarnation the script is not practicle for transferring data, however can be incorporated into your code to do so.
This code basically takes a float, converts it to an alpha key, randomizes it, than takes a random channel, converts it to an alpha key, randomizes it, appends it to the string for the float, and shouts it to another prim, which decrypts it and returns the value of the float, as well as change it's listen to the next randomly determined channel. In this incarnation the script is not practical for transferring data, however can be incorporated into your code to do so.




Line 10: Line 10:
<pre>//Simple Alpha Hash for practical floats by Beverly Larkin
<pre>//Simple Alpha Hash for practical floats by Beverly Larkin


//WARNING!!!  Before actually using this to trasfer any numbers(the princial is the same for strings)  CHANGE THE ORDER OF THE LETTERS! - don't leave them sequential (a,b,c, etc.) it might even be a good idea to perform a simple math function on your number before you send it, (before encrypting it obviously. This is not practicle for any use by itself, but i present it here for those that wish to customize it for their own needs (change the list in the Decrypter to match this one too :P
//WARNING!!!  Before actually using this to transfer any numbers(the principal is the same for strings)  CHANGE THE ORDER OF THE LETTERS! - don't leave them sequential (a,b,c, etc.) it might even be a good idea to perform a simple math function on your number before you send it, (before encrypting it obviously. This is not practice for any use by itself, but i present it here for those that wish to customize it for their own needs (change the list in the Decrypter to match this one too :P


list letters = ["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", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "t", "u", "v", "w", "x", "y", "z"];
string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnoptuvwxyz";
list numbers = [8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.75, 0.5, 0.4, 0.2, 0.1, 0.05, 0.04, 0.02, 0.01, 0.005, 0.004, 0.002, 0.001, 0.0005, 0.0004, 0.0002, 0.0001, 0.00005, 0.00004, 0.00002, 0.00001, 0.000005, 0.000004, 0.000002, 0.000001 ];   
list numbers = [8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.75, 0.5, 0.4, 0.2, 0.1, 0.05, 0.04, 0.02, 0.01, 0.005, 0.004, 0.002, 0.001, 0.0005, 0.0004, 0.0002, 0.0001, 0.00005, 0.00004, 0.00002, 0.00001, 0.000005, 0.000004, 0.000002, 0.000001 ];   


integer negative = FALSE;
integer com_channel;
integer com_channel;


Line 22: Line 21:
// each pass of data includes an encrypted "next channel command" the channel to be used for the next packet of data is determined by:
// each pass of data includes an encrypted "next channel command" the channel to be used for the next packet of data is determined by:
// integer com_channel = (integer)llFrand((8388608) + 100000) * -1;
// integer com_channel = (integer)llFrand((8388608) + 100000) * -1;
// next channel could be lower, but left to a "smaller" range to keep it practicle for use with this script
// next channel could be lower, but left to a "smaller" range to keep it practical for use with this script




Line 33: Line 32:
string Encrypt(float X)
string Encrypt(float X)
{
{
     if(llCeil(X) != llAbs(llCeil(X)))
     integer negative = (X < 0.0);
     {
     if( negative )
        negative = TRUE;
         X = -X;
         X *= -1;
    }
    integer int;
     string returned;
     string returned;
     for(int = 0; int < 49; int++)
     integer int = 0;
    for(; int < 49; int++)
     {
     {
         if(X >= llList2Float(numbers, int))
         if(X >= llList2Float(numbers, int))
         {
         {
             returned += llList2String(letters, int);
             returned += llGetSubString(letters, int, int);
             X -= llList2Float(numbers, int);
             X -= llList2Float(numbers, int);
         }
         }
     }
     }
     if(negative == TRUE)
     if( negative )
     {
     {
         returned += "r";
         returned += "r";
        negative = FALSE;
     }
     }
     return returned;
     return returned;
Line 61: Line 57:
     integer int;
     integer int;
     list randomized;
     list randomized;
    string returned;
     for(int = 0; int < length; ++int)
     for(int = 0; int <= length; int++)
     {
     {
         randomized += llGetSubString(X, int, int);
         randomized += llGetSubString(X, int, int);
     }
     }
     randomized = llListRandomize(randomized, 1);
     return (string)llListRandomize(randomized, 1);
    for(int = 0; int <= length; int++)
    {
        returned += llList2String(randomized, int);
    }       
    return returned;
}
}


default
default
Line 120: Line 109:
<pre>//Simple Alpha Hash for practical floats by Beverly Larkin
<pre>//Simple Alpha Hash for practical floats by Beverly Larkin


list letters = ["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", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "t", "u", "v", "w", "x", "y", "z"];
list letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnoptuvwxyz";
list numbers = [8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.75, 0.5, 0.4, 0.2, 0.1, 0.05, 0.04, 0.02, 0.01, 0.005, 0.004, 0.002, 0.001, 0.0005, 0.0004, 0.0002, 0.0001, 0.00005, 0.00004, 0.00002, 0.00001, 0.000005, 0.000004, 0.000002, 0.000001 ];
list numbers = [8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.75, 0.5, 0.4, 0.2, 0.1, 0.05, 0.04, 0.02, 0.01, 0.005, 0.004, 0.002, 0.001, 0.0005, 0.0004, 0.0002, 0.0001, 0.00005, 0.00004, 0.00002, 0.00001, 0.000005, 0.000004, 0.000002, 0.000001 ];


Line 128: Line 117:
// each pass of data includes an encrypted "next channel command" the channel to be used for the next packet of data is determined by:
// each pass of data includes an encrypted "next channel command" the channel to be used for the next packet of data is determined by:
// integer com_channel = (integer)llFrand((8388608) + 100000) * -1;
// integer com_channel = (integer)llFrand((8388608) + 100000) * -1;
// next channel could be lower, but left to a "smaller" range to keep it practicle for use with this script
// next channel could be lower, but left to a "smaller" range to keep it practical for use with this script


float received_number;
float received_number;


//q = number seperator
//q = number separator
//r = * -1
//r = * -1
//s = channel
//s = channel
Line 139: Line 128:
{
{
     integer position = llSubStringIndex(encrypted, "q");
     integer position = llSubStringIndex(encrypted, "q");
     encrypted = llDeleteSubString(encrypted, position, position);
     return [llDeleteSubString(encrypted, position, -1), llDeleteSubString(encrypted, 0, position)];
    position -= 1;
    list returned;
    returned += llGetSubString(encrypted, 0, position);
    encrypted = llDeleteSubString(encrypted, 0, position);
    returned += llGetSubString(encrypted, 0, -1);
    return returned;
}
}


float Value(string X)
float Value(string X)
{
{
    list find = [X];
     integer position = llSubStringIndex(letters, X);
     integer position = llListFindList(letters, find);
     float returned = llList2Float(numbers, position);
     float returned = llList2Float(numbers, position);
     return returned;
     return returned;
Line 161: Line 143:
     integer channel = FALSE;
     integer channel = FALSE;
     integer position = llSubStringIndex(X, "r");
     integer position = llSubStringIndex(X, "r");
     if(position != -1)
     if(~position)
     {
     {
         negative = TRUE;
         negative = TRUE;
         X = llDeleteSubString(X, position, position);
         X = llDeleteSubString(X, position, position);
        position -= 1;
     }
     }
     position = llSubStringIndex(X, "s");
     if(~(position = llSubStringIndex(X, "s")))
    if(position != -1)
     {
     {
         channel = TRUE;
         channel = TRUE;
         X = llDeleteSubString(X, position, position);
         X = llDeleteSubString(X, position, position);
        position -= 1;
     }
     }
     integer int;
     integer int;
Line 180: Line 159:
         returned += Value(llGetSubString(X, int, int));
         returned += Value(llGetSubString(X, int, int));
     }
     }
     if(negative == TRUE)
     if(negative)
     {
     {
         returned *= -1;
         returned = -returned;
        negative = FALSE;
     }
     }
     if(channel == TRUE)
     if(channel)
     {
     {
         com_channel = (integer)returned;
         com_channel = (integer)returned;
         returned = 0.0;
         returned = 0.0;
        channel = FALSE;
     }
     }
     return returned;
     return returned;
Line 214: Line 191:
         {
         {
             float X = Number_Convert(llList2String(BEER, int));
             float X = Number_Convert(llList2String(BEER, int));
             if(X != 0.000000)
             if(X)
             {
             {
                 received_number = X;
                 received_number = X;
             }                  
             }
         }
         }
         listener = llListen(com_channel, "", "", "");
         listener = llListen(com_channel, "", "", "");
Line 223: Line 200:
         llOwnerSay("Decrypted Number is " + (string)received_number); // decrypted number
         llOwnerSay("Decrypted Number is " + (string)received_number); // decrypted number
         llMessageLinked(LINK_THIS, com_channel, "store", NULL_KEY);
         llMessageLinked(LINK_THIS, com_channel, "store", NULL_KEY);
     }  
     }


     link_message(integer SENDER, integer NUM, string STR, key ID)
     link_message(integer SENDER, integer NUM, string STR, key ID)
Line 232: Line 209:
             listener = llListen(com_channel, "", "", "");
             listener = llListen(com_channel, "", "", "");
         }
         }
     }      
     }
}</pre>
}</pre>



Revision as of 18:21, 5 March 2007


I have posted 3 sections to this code, the encryption, decryption, and also a 3rd file. While the 3rd is unnecessary, since the value for the next channel to communicate on is changed each time data is sent, i found it annoying that when you edited the code to change the number, or tweak minor things, when it was saved, it reloaded the default values. The third script does nothing more than store the next channel to communicate on and pass it to either the encrypter or decypter when that script is reset. If you choose to use it, it should be placed in both prims that you are using.

This code basically takes a float, converts it to an alpha key, randomizes it, than takes a random channel, converts it to an alpha key, randomizes it, appends it to the string for the float, and shouts it to another prim, which decrypts it and returns the value of the float, as well as change it's listen to the next randomly determined channel. In this incarnation the script is not practical for transferring data, however can be incorporated into your code to do so.


Encrypter

//Simple Alpha Hash for practical floats by Beverly Larkin

//WARNING!!!   Before actually using this to transfer any numbers(the principal is the same for strings)  CHANGE THE ORDER OF THE LETTERS! - don't leave them sequential (a,b,c, etc.) it might even be a good idea to perform a simple math function on your number before you send it, (before encrypting it obviously. This is not practice for any use by itself, but i present it here for those that wish to customize it for their own needs (change the list in the Decrypter to match this one too :P

string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnoptuvwxyz";
list numbers = [8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.75, 0.5, 0.4, 0.2, 0.1, 0.05, 0.04, 0.02, 0.01, 0.005, 0.004, 0.002, 0.001, 0.0005, 0.0004, 0.0002, 0.0001, 0.00005, 0.00004, 0.00002, 0.00001, 0.000005, 0.000004, 0.000002, 0.000001 ];  

integer com_channel;

float test_number_to_send = 104.7525; //WARNING FLOATING POINT NUMBERS ARE ROUNDED AUTMATICALLY - excessively large numbers will be changed BEFORE they are encrypted, sorry no easy change for this, as this is just a demo script.

// each pass of data includes an encrypted "next channel command" the channel to be used for the next packet of data is determined by:
// integer com_channel = (integer)llFrand((8388608) + 100000) * -1;
// next channel could be lower, but left to a "smaller" range to keep it practical for use with this script


float received_number;

//q = number seperator
//r = * -1
//s = channel

string Encrypt(float X)
{
    integer negative = (X < 0.0);
    if( negative )
        X = -X;
    string returned;
    integer int = 0;
    for(; int < 49; int++)
    {
        if(X >= llList2Float(numbers, int))
        {
            returned += llGetSubString(letters, int, int);
            X -= llList2Float(numbers, int);
        }
    }
    if( negative )
    {
        returned += "r";
    }
    return returned;
}

string Randomized(string X)
{
    integer length = llStringLength(X);
    integer int;
    list randomized;
    for(int = 0; int < length; ++int)
    {
        randomized += llGetSubString(X, int, int);
    }
    return (string)llListRandomize(randomized, 1);
}

default
{
    on_rez(integer X)
    {
        llResetScript();
    }
    
    state_entry()
    {
        llMessageLinked(LINK_THIS, 0, "retrieve", NULL_KEY);
    }
    
    touch_start(integer Z)
    {
        string encrypted;
        string hashed = Encrypt(test_number_to_send);
        hashed = Randomized(hashed);
        encrypted += hashed;
        encrypted += "q";
        integer next_com_channel = (integer)llFrand((8388608) + 100000) * -1;
        hashed = Encrypt(next_com_channel);
        hashed += "s";
        hashed = Randomized(hashed);
        encrypted += hashed;
        llShout(com_channel, encrypted);
        com_channel = next_com_channel;
        llMessageLinked(LINK_THIS, com_channel, "store", NULL_KEY);
        
    }
    link_message(integer SENDER, integer NUM, string STR, key ID)
    {
        if(STR == "com_channel")
        {
            com_channel = NUM;
        }
    }
}


Decypter

//Simple Alpha Hash for practical floats by Beverly Larkin

list letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnoptuvwxyz";
list numbers = [8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.75, 0.5, 0.4, 0.2, 0.1, 0.05, 0.04, 0.02, 0.01, 0.005, 0.004, 0.002, 0.001, 0.0005, 0.0004, 0.0002, 0.0001, 0.00005, 0.00004, 0.00002, 0.00001, 0.000005, 0.000004, 0.000002, 0.000001 ];

integer listener;
integer com_channel;

// each pass of data includes an encrypted "next channel command" the channel to be used for the next packet of data is determined by:
// integer com_channel = (integer)llFrand((8388608) + 100000) * -1;
// next channel could be lower, but left to a "smaller" range to keep it practical for use with this script

float received_number;

//q = number separator
//r = * -1
//s = channel

list Seperate_Numbers(string encrypted)
{
    integer position = llSubStringIndex(encrypted, "q");
    return [llDeleteSubString(encrypted, position, -1), llDeleteSubString(encrypted, 0, position)];
}

float Value(string X)
{
    integer position = llSubStringIndex(letters, X);
    float returned = llList2Float(numbers, position);
    return returned;
}

float Number_Convert(string X)
{
    integer negative = FALSE;
    integer channel = FALSE;
    integer position = llSubStringIndex(X, "r");
    if(~position)
    {
        negative = TRUE;
        X = llDeleteSubString(X, position, position);
    }
    if(~(position = llSubStringIndex(X, "s")))
    {
        channel = TRUE;
        X = llDeleteSubString(X, position, position);
    }
    integer int;
    float returned;
    for(int = 0; int <= llStringLength(X); int++)
    {
        returned += Value(llGetSubString(X, int, int));
    }
    if(negative)
    {
        returned = -returned;
    }
    if(channel)
    {
        com_channel = (integer)returned;
        returned = 0.0;
    }
    return returned;
}

default
{
    on_rez(integer X)
    {
        llResetScript();
    }
    
    state_entry()
    {
        llMessageLinked(LINK_THIS, 0, "retrieve", NULL_KEY);   
    }
    listen(integer channel, string name, key id, string message)
    {
        llListenRemove(listener);
        llOwnerSay("Message received = " + message); // message received from encryt prim
        list BEER = Seperate_Numbers(message);
        integer int;
        for(int = 0; int < llGetListLength(BEER); int++)
        {
            float X = Number_Convert(llList2String(BEER, int));
            if(X)
            {
                received_number = X;
            }
        }
        listener = llListen(com_channel, "", "", "");
        llOwnerSay("Next message will be received on channel " +(string)com_channel); // next channel to receive data on
        llOwnerSay("Decrypted Number is " + (string)received_number); // decrypted number
        llMessageLinked(LINK_THIS, com_channel, "store", NULL_KEY);
    }

    link_message(integer SENDER, integer NUM, string STR, key ID)
    {
        if(STR == "com_channel")
        {
            com_channel = NUM;
            listener = llListen(com_channel, "", "", "");
        }
    }
}

Channel Storage

integer com_channel = -21747232;
default
{
    link_message(integer SENDER, integer NUM, string STR, key ID)
    {
        if(STR == "store")
        {
            com_channel = NUM;
        }
        else if(STR == "retrieve")
        {
            llMessageLinked(LINK_THIS, com_channel, "com_channel", NULL_KEY);
        }
    }
}