auth-tea.html

<!DOCTYPE html>
<html>
<head>
	<!-- Source: http://www.hw-group.com/support/tea/index_en.html -->
	<meta charset="utf-8">
	<title>Authentication - TEA Encryption [The Tiny Encryption Algorithm]</title>
	<style type="text/css">
	body {
		padding: 1em 1em 5em 1em;
	}
	</style>
	<script>
	/**
	 * Base64
	 * JavaScript implementation: Copyright (c) 2000 by Robert Graham (RobertGraham.com)
	 * Tiny Encryption Algorithm
	 * Algorithm: David Wheeler, Roger Needham, Cambridge University
	 * www.ftp.cl.cam.ac.uk/ftp/papers/djw-rmn/djw-rmn-tea.html
	 * JavaScript implementation: Radek Benedikt (benedikt@lphard.cz)
	 */
	// Begin key
	// Hardcoded TEA key...
	var keyA = 0x01020304; // key 0.. 3
	var keyB = 0x05060708; // key 4.. 7
	var keyC = 0x090A0B0C; // key 8.. 11
	var keyD = 0x0D0E0F10; // key 12.. 15

	function base64ToAscii(c) {
		var result = 0;
		if(0 <= c && c <= 25) {
			result = String.fromCharCode(c + 65); // 65 = "A"
		}
		else if(26 <= c && c <= 51) {
			result = String.fromCharCode(c - 26 + 97); // 97 = "a"
		}
		else if(52 <= c && c <= 61) {
			result = String.fromCharCode(c - 52 + 48); // 48 = "0"
		}
		else if(c == 62) {
			result = "+";
		}
		else if(c == 63) {
			result = "/";
		}
		else {
			result = "=";
		}
		return result;
	}

	function base64Decode(str) {
		var result = "";
		var i = 0;
		var x = 0;
		var y = 0;
		var z = 0;
		var count = -1;
		for(i = 0; i < str.length; i++) {
			c = str.charAt(i);
			if("A" <= c && c <= "Z") {
				x = str.charCodeAt(i) - 65; // 65 = "A"
			}
			else if("a" <= c && c <= "z") {
				x = str.charCodeAt(i) - 97 + 26; // 97 = "a"
			}
			else if("0" <= c && c <= "9") {
				x = str.charCodeAt(i) - 48 + 52; // 48 = "0"
			}
			else if(c == "+") {
				x = 62;
			}
			else if(c == "/") {
				x = 63;
			}
			else {
				continue;
			}
			count++;
			switch(count % 4) {
				case 0:
					y = x;
					continue;
				case 1:
					z = (y << 2)|(x >> 4);
					y = x & 0x0F;
					break;
				case 2:
					z = (y << 4)|(x >> 2);
					y = x & 0x03;
					break;
				case 3:
					z = (y << 6)|x;
					y = 0;
					break;
			}
			result = result + String.fromCharCode(z);
		}
		return result.toString();
	}

	function base64Encode(str) {
		var result = "";
		var i = 0;
		var sextet = 0;
		var leftovers = 0;
		var octet = 0;

		for(i = 0; i < str.length; i++) {
			octet = str.charCodeAt(i);
			switch(i % 3) {
				case 0:
					sextet = (octet & 0xFC) >> 2;
					leftovers = octet & 0x03;
					break;
				case 1:
					sextet = (leftovers << 4)|((octet & 0xF0) >> 4);
					leftovers = octet & 0x0F;
					break;
				case 2:
					sextet = (leftovers << 2)|((octet & 0xC0) >> 6);
					leftovers = (octet & 0x3F);
					break;
			}
			result = result + base64ToAscii(sextet);
			if((i & 3) == 2) {
				result = result + base64ToAscii(leftovers);
			}
		}
		return result.toString();
	}

	function encipher(str) {
		var sum = 0;
		var delta = 0x9E3779B9;
		var n = 32;
		var s = base64Decode(str);
		var y = s.charCodeAt(3) + (s.charCodeAt(2) << 8) + (s.charCodeAt(1) << 16) + (s.charCodeAt(0) << 24);
		var z = s.charCodeAt(7) + (s.charCodeAt(6) << 8) + (s.charCodeAt(5) << 16) + (s.charCodeAt(4) << 24);
		var w;

		if(str.length != 12) {
			return "Invalid length: string must be 12 char";
		}
		else if(
			(
				(
				s.charCodeAt(0) + s.charCodeAt(1) + s.charCodeAt(2) + s.charCodeAt(3) + s.charCodeAt(4) + s.charCodeAt(5) + s.charCodeAt(6) + s.charCodeAt(7) + s.charCodeAt(8)
				) & 0xFF
			) != 0
		) {
			return "Checksum Error";
		} else {
			while(n-- > 0) {
				sum += delta
				y += (((z << 4) + keyA) ^ (z + sum) ^ (((z >> 5) & 0x07FFFFFF) + keyB))
				z += (((y << 4) + keyC) ^ (y + sum) ^ (((y >> 5) & 0x07FFFFFF) + keyD))
			}
			w = (y >> 24 & 0xFF) + (y >> 16 & 0xFF) + (y >> 8 & 0xFF) + (y & 0xFF) + (z >> 24 & 0xFF) + (z >> 16 & 0xFF) + (z >> 8 & 0xFF) + (z & 0xFF);
			w &= 0xFF;
			w = 0x100 - w;
			s = (String.fromCharCode(y >> 24 & 0xFF, y >> 16 & 0xFF, y >> 8 & 0xFF, y & 0xFF) + String.fromCharCode(z >> 24 & 0xFF, z >> 16 & 0xFF, z >> 8 & 0xFF, z & 0xFF) + String.fromCharCode(w & 0xFF));
			return base64Encode(s);
		}
	}
	</script>
</head>
<body>


<form id="frmCipher" action="auth-tea.html">
<input type="text" name="login" size="16">
<input type="submit" value="Login" onclick="this.form.login.value=encipher(this.form.login.value)">
</form>


</body>
</html>