If you are a player of BlogNomic and wish to edit this Wiki, you must first log in.

EncryptionDevice

HomePage | RecentChanges | Preferences

This is the code for the encrypter used in the FirstDynastyOfRodney. The code goes into the template for the Blog.
//---------------------------------------------------------------------------
// 'Block' Tiny Encryption Algorithm
//
// Algorithm: David Wheeler & Roger Needham, Cambridge University Computer Lab
//            http://www.cl.cam.ac.uk/ftp/papers/djw-rmn/djw-rmn-tea.html (1994)
//            http://www.cl.cam.ac.uk/ftp/users/djw3/xtea.ps (1997)
//            http://www.cl.cam.ac.uk/ftp/users/djw3/xxtea.ps (1998)
//
// JavaScript implementation: Chris Veness, Movable Type Ltd: www.movable-type.co.uk
//
// encrypt: Use 128 bits (16 chars) of string 'key' to encrypt string 'val'
//          (note key & val must be strings not string objects)
//           - return encrypted text as string
//
function encrypt(val, key)
{
    // 'escape' val so chars outside ISO-8859-1 work in single-byte packing, but 
    // keep spaces as spaces (not '%20') so encrypted text doesn't grow too long!
    var v = strToLongs(escape(val).replace(/%20/g,' '));
    var k = strToLongs(key.slice(0,16)); var n = v.length;

    if (n == 0) return("");  // nothing to encrypt
    if (n == 1) v[n++] = 0;  // algorithm doesn't work for n<2 so fudge by adding nulls

    // TEA routine as per Wheeler & Needham, Oct 1998

    var z = v[n-1], y = v[0], delta = 0x9E3779B9;
    var mx, e, q = Math.floor(6 + 52/n), sum = 0;

    while (q-- > 0) {  // 6 + 52/n operations gives between 6 & 32 mixes on each word
        sum += delta;
        e = sum>>>2 & 3;
        for (var p = 0; p < n-1; p++) {
            y = v[p+1];
            mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z)
            z = v[p] += mx;
        }
        y = v[0];
        mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z)
        z = v[n-1] += mx;
    }

    // note: unsigned right-shift '>>>' is used in place of original '>>', 
    // due to lack of 'unsigned' type declaration in JavaScript

    return(escape(longsToStr(v)));
}


//
// decrypt: use 128 bits of string 'key' to decrypt string 'val' encrypted as per above
//
function decrypt(val, key)
{
    var v = strToLongs(unescape(val)); var k = strToLongs(key.slice(0,16)); var n = v.length;

    if (n == 0) return("");

    // TEA routine as per Wheeler & Needham, Oct 1998

    var z = v[n-1], y = v[0], delta = 0x9E3779B9;
    var mx, e, q = Math.floor(6 + 52/n), sum = q*delta;

    while (sum != 0) {
        e = sum>>>2 & 3;
        for (var p = n-1; p > 0; p--) {
            z = v[p-1];
            mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z)
            y = v[p] -= mx;
        }
        z = v[n-1];
        mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z)
        y = v[0] -= mx;
        sum -= delta;
    }

    var s = longsToStr(v);
    if (s.indexOf("\x00") != -1) {
        // strip trailing null chars resulting from filling 4-char blocks
        s = s.substr(0, s.indexOf("\x00"));
    }

    return unescape(s);
}


// supporting functions

function strToLongs(s) {  // convert string to array of longs, each containing 4 chars
    // note chars must be within ISO-8859-1 (with Unicode code-point < 256) to fit 4/long
    var l = new Array(Math.ceil(s.length/4))
    for (var i=0; i<l.length; i++) {
        l[i] = s.charCodeAt(i*4) + (s.charCodeAt(i*4+1)<<8) + 
               (s.charCodeAt(i*4+2)<<16) + (s.charCodeAt(i*4+3)<<24);
    }
    return l;  // note running off the end of the string generates nulls since 
}              // bitwise operators treat NaN as 0

function longsToStr(l) {  // convert array of longs back to string
    var a = new Array(l.length);
    for (var i=0; i<l.length; i++) {
        a[i] = String.fromCharCode(l[i] & 0xFF, l[i]>>>8 & 0xFF, 
                                 l[i]>>>16 & 0xFF, l[i]>>>24 & 0xFF);
    }



    return a.join('');  // use Array.join() rather than repeated string appends for efficiency
}
// end Encryption Algorithm
//---------------------------------------------------------------------------
//The following goes into the sidebar portion of the template

	<!-- Secret Decoder Ring -->
	<h2 class="sidebar-title">Encryption Tool</h2>
	<div class="tooltext">
		<form name="et" action="none!">
      <table>
        <tr>
          <td><input type="text" name="key" size="12" value="key"></td>
        </tr>
        <tr>
          <td><textarea name="val" rows="1">value</textarea></td>
        </tr>
        <tr>
          <td><script type="text/javascript">var ciferText;</script> 
            <!-- use 'ciferText' because <input> field will truncate at null chars -->
            <input type="button" value="Encrypt it"
              onClick='et.encrypted.value = ciferText = encrypt(et.val.value, et.key.value)' style="width: auto;background-color: #999;"></td>
        </tr>
        <tr>
          <td><textarea name="encrypted" rows="1"></textarea></td>
        </tr>
        <tr>
          <td><input type="button" value="Decrypt it" onClick='et.decrypted.value = decrypt(et.val.value, et.key.value)' style="width: auto;background-color: #999;"></td>
        </tr>
        <tr>
          <td><textarea name="decrypted" rows="1"></textarea></td>
        </tr>
      </table>
    </form>
	</div>	
	

HomePage | RecentChanges | Preferences
This page is read-only | View other revisions | Text Formatting Rules
Last edited July 18, 2005 6:11 pm by Excalabur (diff)
Search: