2009年4月4日 周六 22:18 <HTML> <HEAD> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <TITLE>文本加密</TITLE> <SCRIPT LANGUAGE="JavaScript"> function TEAencrypt(plaintext, password) { if (plaintext.length == 0) return(''); // nothing to encrypt // 'escape' plaintext 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 (quick & dirty) var asciitext = escape(plaintext).replace(/%20/g,' '); var v = strToLongs(asciitext); // convert string to array of longs if (v.length <= 1) v[1] = 0; // algorithm doesn't work for n<2 so fudge by adding a null var k = strToLongs(password.slice(0,16)); // simply convert first 16 chars of password as key var n = v.length; 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; p++) { y = v[(p+1)%n]; mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z); z = v[p] += mx; } } var ciphertext = longsToStr(v); return escCtrlCh(ciphertext); } // // TEAdecrypt: Use Corrected Block TEA to decrypt ciphertext using password // function TEAdecrypt(ciphertext, password) { if (ciphertext.length == 0) return(''); var v = strToLongs(unescCtrlCh(ciphertext)); var k = strToLongs(password.slice(0,16)); var n = v.length; 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>0 ? p-1 : n-1]; mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z); y = v[p] -= mx; } sum -= delta; } var plaintext = longsToStr(v); // strip trailing null chars resulting from filling 4-char blocks: plaintext = plaintext.replace(/