Text To Byte Conversion

From Second Life Wiki
Revision as of 01:59, 3 January 2008 by Dedric Mauriac (talk | contribs) (New page: {{LSL Header}} Here's a function to pass a string of text and receive a list of bytes. A second function is provided to work in reverse. These methods are especially useful for working wit...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Here's a function to pass a string of text and receive a list of bytes. A second function is provided to work in reverse. These methods are especially useful for working with encryption.

Encryptor

<lsl> string base64characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; list text2bytes(string text) {

   string base64 = llStringToBase64(text);
   list bytes;
   integer i;
   integer n = llStringLength(base64);
   for(i = 0; i < n; i += 4)
   {
       string c1 = llGetSubString(base64, i + 0, i + 0);
       string c2 = llGetSubString(base64, i + 1, i + 1);
       string c3 = llGetSubString(base64, i + 2, i + 2);
       string c4 = llGetSubString(base64, i + 3, i + 3);
       integer b1 = llSubStringIndex(base64characters, c1);
       integer b2 = llSubStringIndex(base64characters, c2);
       integer b3 = llSubStringIndex(base64characters, c3);
       integer b4 = llSubStringIndex(base64characters, c4);
       integer bbb = (b1 << 18) + (b2 << 12);
       if(b3 != -1) bbb += (b3 << 6);
       if(b4 != -1) bbb += (b4 << 0);
       bytes += (bbb & 0xFF0000) >> 16;
       if(b3 != -1) bytes += (bbb & 0xFF00) >> 8;
       if(b4 != -1) bytes += bbb & 0xFF;
   }
   return bytes;

} string bytes2text(list bytes) {

   string text;
   integer i;
   integer n = llGetListLength(bytes);
   for(i = 0; i + 3 < n; i += 3)
   {
       integer b1 = llList2Integer(bytes, i);
       integer b2 = llList2Integer(bytes, i + 1);
       integer b3 = llList2Integer(bytes, i + 2);
       integer bbb = (b1 & 0xff) << 16 | (b2 & 0xFF) << 8 | (b3 & 0xFF);
       text += llGetSubString(base64characters, (bbb & 0x00FC0000) >> 18, (bbb & 0x00FC0000) >> 18);
       text += llGetSubString(base64characters, (bbb & 0x0003F000) >> 12, (bbb & 0x0003F000) >> 12);
       text += llGetSubString(base64characters, (bbb & 0x00000FC0) >> 6, (bbb & 0x00000FC0) >> 6);
       text += llGetSubString(base64characters, (bbb & 0x0000003F) >> 0, (bbb & 0x0000003F) >> 0);
   }
   if(n - i > 0 && n - 1 > 3)
   {
       integer two = n - i - 1;
       
       integer b1 = llList2Integer(bytes, i);
       integer b2 = llList2Integer(bytes, i + 1);
       integer bbb = (b1 & 0xff) << 16;
       if(two) bbb = bbb | (b2 & 0xFF) << 8;
       text += llGetSubString(base64characters, (bbb & 0x00FC0000) >> 18, (bbb & 0x00FC0000) >> 18);
       text += llGetSubString(base64characters, (bbb & 0x0003F000) >> 12, (bbb & 0x0003F000) >> 12);
       if(two)
           text += llGetSubString(base64characters, (bbb & 0x00000FC0) >> 6, (bbb & 0x00000FC0) >> 6);
       else
           text += "=";
       text += "=";
   }
   return llBase64ToString(text);

}

default {

   state_entry()
   {
       list bytes = text2bytes("Hello, Avatar!");
       llSay(0, llList2CSV(bytes));
       string text = bytes2text(bytes);
       llSay(0, text);
   }
   touch_start(integer total_number)
   {
       list bytes = text2bytes("Touched.");
       llSay(0, llList2CSV(bytes));
       string text = bytes2text(bytes);
       llSay(0, text);
   }

}

</lsl>

Description

Cryptography is an area of computing focused on encrypting data in order to prevent it from being readable by another (potentially malicious) user. Cryptography has applications all over SL where any form of data-security is required, be it an item vendor, or a game, or a device that processes personal details.

Unfortunately in LSL there are only a few good methods provided for security, beyond the ability to communicate with off-site services using HTTPS. For this reason there have been several efforts to implement encryption/decryption standards in LSL, and more recently take advantage of Mono for greater encryption.

Cryptography Libraries

Test Vectors

For all security libraries reviewed a common set of test-vectors (message and key(s)) should be used. These are the following recommended ones:

Base64 Message (1536-bit)

Remove new-lines

VAgWWFUfAJvIxSwoSUGCCR/QnQvXif9oswq1JaICbrohjfOTiINmRVQkr1q+awPXd2nGncz8Pelb/
2vdx2vhh8UJiF6jBChzNGqr01l7ssgrIKGuXxKHw2JF8bByz7mREbmvxrndkM288HuwJYw0LlnaM1
3h8lzSBrQnLzr/Xwb6l3MfU867b/WtcuwpyXci9KKKYczywNs9Ay8N80xrwaA5sqWYysLrQD43U+l
w9wPpbzf8/kJfwxZL4WL6nfxi=

Hexadecimal Key

42DBE20995628324EB343C6CF9D3C5F4

Base64 Key

Identical to the above hex-key but in Base64

QtviCZVigyTrNDxs+dPF9A==

Symmetric Key Algorithms

Times below are given as a time to encrypt and decrypt the above test-message using the above test-key, in an empty simulator.

Library Security Encrypt/Decrypt Time Summary
AES Very strong 0.891489 Relatively slow compared to other symmetric ciphers, but affords industry-standard security with no known weaknesses. Can encrypt a 2.3kb message with guaranteed 256-bits of security in around 5-10 seconds.
Base64 XOR Very weak 0.006923 Very strong when used as a one-time-pad, but if used with the same key more than once becomes extremely vulnerable to attack. Use where speed is the highest priority, and where a message (if cracked) is useless after a very short time.
Vignére Cipher Weak 0.097980 Stronger than a regular base64 XOR, and can sustain repeated use of the same key for short periods of time. Use only for messages that become useless after a short period of time.