Difference between revisions of "AES LSL Examples"

From Second Life Wiki
Jump to navigation Jump to search
(New page: = AES LSL Examples = == Description == The following LSL code snippets show examples of using the AES engine by Haravikk Mistral. These...)
 
m (→‎AES LSL Examples: <lsl> to <source>)
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{LSL Header}}
= AES LSL Examples =
= AES LSL Examples =
== Description ==
== Description ==
The following LSL code snippets show examples of using the [[AES LSL Implementation|AES engine]] by [[User:Haravikk Mistral|Haravikk Mistral]]. These examples require that you add any relevant functions and constants from the [[AES LSL Helpers|helpers]] code where noted.
The following LSL code snippets show examples of using the [[AES LSL Implementation|AES engine]] by [[User:Haravikk Mistral|Haravikk Mistral]]. These examples require that you add any relevant functions and constants from the [[AES LSL Helpers|helpers]] code where noted.


States are used in these scripts for clarity, but are not necessary to the operation the scripts themselves.
States are used in these scripts for clarity, but are not necessary to the operation of the scripts themselves.


== Examples ==
== Examples ==
=== Encrypt a message ===
=== Encrypt a message ===
<lsl>string  myKey  = "1234567890ABCDEF0123456789ABCDEF"; // 128-bit key in hex
<source lang="lsl2">string  myKey  = "1234567890ABCDEF0123456789ABCDEF"; // 128-bit key in hex
string  myMsg  = "Hello world! I am a lovely message waiting to be encrypted!";
string  myMsg  = "Hello world! I am a lovely message waiting to be encrypted!";
string  myIV    = "89ABCDEF0123456789ABCDEF01234567";
string  myIV    = "89ABCDEF0123456789ABCDEF01234567";
Line 93: Line 95:
             llOwnerSay("Encrypted: "+msg);
             llOwnerSay("Encrypted: "+msg);
     }
     }
}</lsl>
}</source>


=== Decrypt a message ===
=== Decrypt a message ===
<lsl>string  myKey  = "1234567890ABCDEF0123456789ABCDEF"; // 128-bit key in hex
<source lang="lsl2">string  myKey  = "1234567890ABCDEF0123456789ABCDEF"; // 128-bit key in hex
string  myMsg  = "8HyYDoni+u2O83/YxFAfQThtRWhupovubeb7+YQZZNJoENfv1sgRwkQSzrUveevWNLL7G6bU4ZSofoVERSWygA=";
string  myMsg  = "slihkO6t9I/yfvfUpI0Rthagd/z8j1s5qh/PSbKGBg4N3PoQgUFdcCVnqOYku53cVx+IDgo8d0gPGaBR5YzORQ=";
string  myIV    = "89ABCDEF0123456789ABCDEF01234567";
string  myIV    = "89ABCDEF0123456789ABCDEF01234567";


Line 166: Line 168:
state decrypt {
state decrypt {
     state_entry() { // Send our message
     state_entry() { // Send our message
         lslAESEncryptBase64ToBase64(
         lslAESDecryptBase64ToBase64(
             LINK_THIS,
             LINK_THIS,
             myMsg,
             myMsg,
Line 182: Line 184:
             llOwnerSay("Decrypted: "+llBase64ToString(msg));
             llOwnerSay("Decrypted: "+llBase64ToString(msg));
     }
     }
}</lsl>
}</source>

Latest revision as of 15:42, 14 September 2015

AES LSL Examples

Description

The following LSL code snippets show examples of using the AES engine by Haravikk Mistral. These examples require that you add any relevant functions and constants from the helpers code where noted.

States are used in these scripts for clarity, but are not necessary to the operation of the scripts themselves.

Examples

Encrypt a message

string  myKey   = "1234567890ABCDEF0123456789ABCDEF"; // 128-bit key in hex
string  myMsg   = "Hello world! I am a lovely message waiting to be encrypted!";
string  myIV    = "89ABCDEF0123456789ABCDEF01234567";

// Add necessary functions from https://wiki.secondlife.com/wiki/AES_LSL_Helpers here

default {
    state_entry() { // Setup the engine for use
        lslAESSetup(
            LINK_THIS,
            "MODE_CBC,"+     // CBC mode is a good, strong mode
            "PAD_NULLS,"+    // Pad with null-chars (zero-bytes)
            "PAD_SIZE,512",  // Pad into blocks of 512-bits
            llGetKey()
        );
    }
 
    link_message(integer x, integer y, string msg, key id) {
        if (!lslAESIsReply(y, id)) return;
        
        y = lslAESGetReplyMode(y);
        if (y == LSLAES_COMMAND_ERROR) 
            llOwnerSay("ERROR: "+msg);
        else if (y == LSLAES_COMMAND_SETUP) 
            state prime;
    }
}
    
state prime {
    state_entry() { // First prime the engine with a key
        lslAESPrimeHexKey(
            LINK_THIS,
            myKey,
            llGetKey()
        );
    }
 
    link_message(integer x, integer y, string msg, key id) {
        if (!lslAESIsReply(y, id)) return;
        
        y = lslAESGetReplyMode(y);
        if (y == LSLAES_COMMAND_ERROR) 
            llOwnerSay("ERROR: "+msg);
        else if (y == LSLAES_COMMAND_PRIME) 
            state init;
    }
}

state init {
    state_entry() { // Now init the engine with an input vector
        lslAESInitHexIV(
            LINK_THIS,
            myIV,
            llGetKey()
        );
    }
 
    link_message(integer x, integer y, string msg, key id) {
        if (!lslAESIsReply(y, id)) return;
        
        y = lslAESGetReplyMode(y);
        if (y == LSLAES_COMMAND_ERROR) 
            llOwnerSay("ERROR: "+msg);
        else if (y == LSLAES_COMMAND_INIT) 
            state encrypt;
    }
}
 
state encrypt {
    state_entry() { // Send our message
        lslAESEncryptBase64ToBase64(
            LINK_THIS,
            llStringToBase64(myMsg),
            llGetOwner()
        );
    }
 
    link_message(integer x, integer y, string msg, key id) {
        if (!lslAESIsReply(y, id)) return;
        
        y = lslAESGetReplyMode(y);
        if (y == LSLAES_COMMAND_ERROR) 
            llOwnerSay("ERROR: "+msg);
        else if (y == LSLAES_COMMAND_ENCRYPT) 
            llOwnerSay("Encrypted: "+msg);
    }
}

Decrypt a message

string  myKey   = "1234567890ABCDEF0123456789ABCDEF"; // 128-bit key in hex
string  myMsg   = "slihkO6t9I/yfvfUpI0Rthagd/z8j1s5qh/PSbKGBg4N3PoQgUFdcCVnqOYku53cVx+IDgo8d0gPGaBR5YzORQ=";
string  myIV    = "89ABCDEF0123456789ABCDEF01234567";

// Add necessary functions from https://wiki.secondlife.com/wiki/AES_LSL_Helpers here

default {
    state_entry() { // Setup the engine for use
        lslAESSetup(
            LINK_THIS,
            "MODE_CBC,"+     // CBC mode is a good, strong mode
            "PAD_NULLS,"+    // Pad with null-chars (zero-bytes)
            "PAD_SIZE,512",  // Pad into blocks of 512-bits
            llGetKey()
        );
    }
 
    link_message(integer x, integer y, string msg, key id) {
        if (!lslAESIsReply(y, id)) return;
        
        y = lslAESGetReplyMode(y);
        if (y == LSLAES_COMMAND_ERROR) 
            llOwnerSay("ERROR: "+msg);
        else if (y == LSLAES_COMMAND_SETUP) 
            state prime;
    }
}
    
state prime {
    state_entry() { // First prime the engine with a key
        lslAESPrimeHexKey(
            LINK_THIS,
            myKey,
            llGetKey()
        );
    }
 
    link_message(integer x, integer y, string msg, key id) {
        if (!lslAESIsReply(y, id)) return;
        
        y = lslAESGetReplyMode(y);
        if (y == LSLAES_COMMAND_ERROR) 
            llOwnerSay("ERROR: "+msg);
        else if (y == LSLAES_COMMAND_PRIME) 
            state init;
    }
}

state init {
    state_entry() { // Now init the engine with an input vector
        lslAESInitHexIV(
            LINK_THIS,
            myIV,
            llGetKey()
        );
    }
 
    link_message(integer x, integer y, string msg, key id) {
        if (!lslAESIsReply(y, id)) return;
        
        y = lslAESGetReplyMode(y);
        if (y == LSLAES_COMMAND_ERROR) 
            llOwnerSay("ERROR: "+msg);
        else if (y == LSLAES_COMMAND_INIT) 
            state decrypt;
    }
}
 
state decrypt {
    state_entry() { // Send our message
        lslAESDecryptBase64ToBase64(
            LINK_THIS,
            myMsg,
            llGetOwner()
        );
    }
 
    link_message(integer x, integer y, string msg, key id) {
        if (!lslAESIsReply(y, id)) return;
        
        y = lslAESGetReplyMode(y);
        if (y == LSLAES_COMMAND_ERROR) 
            llOwnerSay("ERROR: "+msg);
        else if (y == LSLAES_COMMAND_DECRYPT) 
            llOwnerSay("Decrypted: "+llBase64ToString(msg));
    }
}