Professional Documents
Culture Documents
SRC
SRC
}
}
key = hexToByteArray(hs);
}
}
/* Generate a key from the pseudorandom number generator
and stuff it in the key field. The kind of key generated
(text or hexadecimal) is determined by which box is checked
below the key field. */
function Generate_key() {
var i, j, k = "";
var i, j, k = "";
addEntropyTime();
var seed = keyFromEntropy();
var prng = new AESprng(seed);
if (document.key.keytype[0].checked) {
// Text key
var charA = ("A").charCodeAt(0);
for (i = 0; i < 12; i++) {
if (i > 0) {
k += "-";
}
for (j = 0; j < 5; j++) {
k += String.fromCharCode(charA + prng.nextInt(25));
}
}
} else {
// Hexadecimal key
var hexDigits = "0123456789ABCDEF";
for (i = 0; i < 64; i++) {
k += hexDigits.charAt(prng.nextInt(15));
}
}
document.key.text.value = k;
delete prng;
}
function Encrypt_text() {
var v, i;
var prefix = "##### Encrypted: decrypt with http://www.fourmilab.ch/jav
ascrypt/\n",
suffix = "##### End encrypted message\n";
if (document.key.text.value.length == 0) {
alert("Please specify a key with which to encrypt the message.");
return;
}
if (document.plain.text.value.length == 0) {
alert("No plain text to encrypt! Please enter or paste plain text i
n the field above.");
return;
}
document.cipher.text.value = "";
setKey();
addEntropyTime();
prng = new AESprng(keyFromEntropy());
var plaintext = encode_utf8(document.plain.text.value);
// Compute MD5 sum of message text and add to header
md5_init();
for (i = 0; i < plaintext.length; i++) {
md5_update(plaintext.charCodeAt(i));
}
md5_finish();
var header = "";
for (i = 0; i < digestBits.length; i++) {
header += String.fromCharCode(digestBits[i]);
}
// Add message length in bytes to header
i = plaintext.length;
header += String.fromCharCode(i
header += String.fromCharCode(i
header += String.fromCharCode(i
header += String.fromCharCode(i
>>> 24);
>>> 16);
>>> 8);
& 0xFF);
Content
MD5 signature of plaintext
Length of plaintext, big-endian order
Plaintext
Codegroup
Hexadecimal
Base 64
*/
function determineArmourType(s) {
var kt, pcg, phex, pb64, pmin;
pcg = s.indexOf(codegroupSentinel);
phex = s.indexOf(hexSentinel);
pb64 = s.indexOf(base64sent);
if (pcg == -1) {
pcg = s.length;
}
if (phex == -1) {
phex = s.length;
}
if (pb64 == -1) {
pb64 = s.length;
}
pmin = Math.min(pcg, Math.min(phex, pb64));
if (pmin < s.length) {
if (pmin == pcg) {
kt = 0;
} else if (pmin == phex) {
kt = 1;
} else {
kt = 2;
}
} else {
if (document.plain.encoding[0].checked) {
kt = 0;
} else if (document.plain.encoding[1].checked) {
kt = 1;
} else if (document.plain.encoding[2].checked) {
kt = 2;
}
}
return kt;
}
// Decrypt ciphertext with key, place result in plaintext field
function Decrypt_text() {
if (document.key.text.value.length == 0) {
alert("Please specify a key with which to decrypt the message.");
return;
}
if (document.cipher.text.value.length == 0) {
alert("No cipher text to decrypt! Please enter or paste cipher text
in the field above.");
return;
}
document.plain.text.value = "";
setKey();
var ct = new Array(), kt;
kt = determineArmourType(document.cipher.text.value);
if (kt == 0) {
ct = disarm_codegroup(document.cipher.text.value);
} else if (kt == 1) {
ct = disarm_hex(document.cipher.text.value);
} else if (kt == 2) {
ct = disarm_base64(document.cipher.text.value);
}
var result = rijndaelDecrypt(ct, key, "CBC");
var header = result.slice(0, 20);
result = result.slice(20);
/* Extract the length of the plaintext transmitted and
verify its consistency with the length decoded. Note
that in many cases the decrypted messages will include
pad bytes added to expand the plaintext to an integral
number of AES blocks (blockSizeInBits / 8). */
var dl = (header[16] << 24) | (header[17] << 16) | (header[18] << 8) | h
eader[19];
if ((dl < 0) || (dl > result.length)) {
alert("Message (length " + result.length + ") truncated. " +
dl + " characters expected.");
// Try to sauve qui peut by setting length to entire message
dl = result.length;
}
/* Compute MD5 signature of message body and verify
against signature in message. While we're at it,
we assemble the plaintext result string. Note that
the length is that just extracted above from the
message, *not* the full decrypted message text.
AES requires all messages to be an integral number
of blocks, and the message may have been padded with
zero bytes to fill out the last block; using the
length from the message header elides them from
both the MD5 computation and plaintext result. */
var i, plaintext = "";
md5_init();
for (i = 0; i < dl; i++) {
plaintext += String.fromCharCode(result[i]);
md5_update(result[i]);
}
md5_finish();
for (i = 0; i < digestBits.length; i++) {
if (digestBits[i] != header[i]) {
alert("Message corrupted. Checksum of decrypted message does no
t match.");
break;
}
}
// That's it; plug plaintext into the result field
document.plain.text.value = decode_utf8(plaintext);
}