Difference between revisions of "AES LSL Examples"

From Second Life Wiki
Jump to navigation Jump to search
m (→‎AES LSL Examples: <lsl> to <source>)
 
Line 9: Line 9:
== 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 95: 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  = "slihkO6t9I/yfvfUpI0Rthagd/z8j1s5qh/PSbKGBg4N3PoQgUFdcCVnqOYku53cVx+IDgo8d0gPGaBR5YzORQ=";
string  myMsg  = "slihkO6t9I/yfvfUpI0Rthagd/z8j1s5qh/PSbKGBg4N3PoQgUFdcCVnqOYku53cVx+IDgo8d0gPGaBR5YzORQ=";
string  myIV    = "89ABCDEF0123456789ABCDEF01234567";
string  myIV    = "89ABCDEF0123456789ABCDEF01234567";
Line 184: Line 184:
             llOwnerSay("Decrypted: "+llBase64ToString(msg));
             llOwnerSay("Decrypted: "+llBase64ToString(msg));
     }
     }
}</lsl>
}</source>

Latest revision as of 14: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));
    }
}