AES LSL Examples

= 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));
 }
}