?? crypto-aes.js
字號:
// convert the nonce to a string to go on the front of the ciphertext var ctrTxt = ''; for (var i=0; i<8; i++) ctrTxt += String.fromCharCode(counterBlock[i]); ctrTxt = escCtrlChars(ctrTxt); // use '-' to separate blocks, use Array.join to concatenate arrays of strings for efficiency return ctrTxt + '-' + ciphertext.join('-');}/* * Use AES to decrypt 'ciphertext' with 'password' using 'nBits' key, in Counter mode of operation * * for each block * - outputblock = cipher(counter, key) * - cipherblock = plaintext xor outputblock */function AESDecryptCtr(ciphertext, password, nBits) { if (!(nBits==128 || nBits==192 || nBits==256)) return ''; // standard allows 128/192/256 bit keys var nBytes = nBits/8; // no bytes in key var pwBytes = new Array(nBytes); for (var i=0; i<nBytes; i++) pwBytes[i] = password.charCodeAt(i) & 0xff; var pwKeySchedule = KeyExpansion(pwBytes); var key = Cipher(pwBytes, pwKeySchedule); key = key.concat(key.slice(0, nBytes-16)); // key is now 16/24/32 bytes long var keySchedule = KeyExpansion(key); ciphertext = ciphertext.split('-'); // split ciphertext into array of block-length strings // recover nonce from 1st element of ciphertext var blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES var counterBlock = new Array(blockSize); var ctrTxt = unescCtrlChars(ciphertext[0]); for (var i=0; i<8; i++) counterBlock[i] = ctrTxt.charCodeAt(i); var plaintext = new Array(ciphertext.length-1); for (var b=1; b<ciphertext.length; b++) { // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) for (var c=0; c<4; c++) counterBlock[15-c] = ((b-1) >>> c*8) & 0xff; for (var c=0; c<4; c++) counterBlock[15-c-4] = ((b/0x100000000-1) >>> c*8) & 0xff; var cipherCntr = Cipher(counterBlock, keySchedule); // encrypt counter block ciphertext[b] = unescCtrlChars(ciphertext[b]); var pt = ''; for (var i=0; i<ciphertext[b].length; i++) { // -- xor plaintext with ciphered counter byte-by-byte -- var ciphertextByte = ciphertext[b].charCodeAt(i); var plaintextByte = ciphertextByte ^ cipherCntr[i]; pt += String.fromCharCode(plaintextByte); } // pt is now plaintext for this block plaintext[b-1] = pt; // b-1 'cos no initial nonce block in plaintext } return plaintext.join('');}/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */function escCtrlChars(str) { // escape control chars which might cause problems handling ciphertext return str.replace(/[\0\t\n\v\f\r\xa0'"!-]/g, function(c) { return '!' + c.charCodeAt(0) + '!'; });} // \xa0 to cater for bug in Firefox; include '-' to leave it free for use as a block markerfunction unescCtrlChars(str) { // unescape potentially problematic control characters return str.replace(/!\d\d?\d?!/g, function(c) { return String.fromCharCode(c.slice(1,-1)); });}/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *//* * if escCtrlChars()/unescCtrlChars() still gives problems, use encodeBase64()/decodeBase64() instead */var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";function encodeBase64(str) { // http://tools.ietf.org/html/rfc4648 var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc=''; str = encodeUTF8(str); // encode multi-byte chars into UTF-8 for byte-array do { // pack three octets into four hexets o1 = str.charCodeAt(i++); o2 = str.charCodeAt(i++); o3 = str.charCodeAt(i++); bits = o1<<16 | o2<<8 | o3; h1 = bits>>18 & 0x3f; h2 = bits>>12 & 0x3f; h3 = bits>>6 & 0x3f; h4 = bits & 0x3f; // end of string? index to '=' in b64 if (isNaN(o3)) h4 = 64; if (isNaN(o2)) h3 = 64; // use hexets to index into b64, and append result to encoded string enc += b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4); } while (i < str.length); return enc;}function decodeBase64(str) { var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc=''; do { // unpack four hexets into three octets using index points in b64 h1 = b64.indexOf(str.charAt(i++)); h2 = b64.indexOf(str.charAt(i++)); h3 = b64.indexOf(str.charAt(i++)); h4 = b64.indexOf(str.charAt(i++)); bits = h1<<18 | h2<<12 | h3<<6 | h4; o1 = bits>>16 & 0xff; o2 = bits>>8 & 0xff; o3 = bits & 0xff; if (h3 == 64) enc += String.fromCharCode(o1); else if (h4 == 64) enc += String.fromCharCode(o1, o2); else enc += String.fromCharCode(o1, o2, o3); } while (i < str.length); return decodeUTF8(enc); // decode UTF-8 byte-array back to Unicode}function encodeUTF8(str) { // encode multi-byte string into utf-8 multiple single-byte characters str = str.replace( /[\u0080-\u07ff]/g, // U+0080 - U+07FF = 2-byte chars function(c) { var cc = c.charCodeAt(0); return String.fromCharCode(0xc0 | cc>>6, 0x80 | cc&0x3f); } ); str = str.replace( /[\u0800-\uffff]/g, // U+0800 - U+FFFF = 3-byte chars function(c) { var cc = c.charCodeAt(0); return String.fromCharCode(0xe0 | cc>>12, 0x80 | cc>>6&0x3F, 0x80 | cc&0x3f); } ); return str;}function decodeUTF8(str) { // decode utf-8 encoded string back into multi-byte characters str = str.replace( /[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars function(c) { var cc = (c.charCodeAt(0)&0x1f)<<6 | c.charCodeAt(1)&0x3f; return String.fromCharCode(cc); } ); str = str.replace( /[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars function(c) { var cc = (c.charCodeAt(0)&0x0f)<<12 | (c.charCodeAt(1)&0x3f<<6) | c.charCodeAt(2)&0x3f; return String.fromCharCode(cc); } ); return str;}function byteArrayToHexStr(b) { // convert byte array to hex string for displaying test vectors var s = ''; for (var i=0; i<b.length; i++) s += b[i].toString(16) + ' '; return s;}/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */var plainText = "ROMEO: But, soft! what light through yonder window breaks?\n\It is the east, and Juliet is the sun.\n\Arise, fair sun, and kill the envious moon,\n\Who is already sick and pale with grief,\n\That thou her maid art far more fair than she:\n\Be not her maid, since she is envious;\n\Her vestal livery is but sick and green\n\And none but fools do wear it; cast it off.\n\It is my lady, O, it is my love!\n\O, that she knew she were!\n\She speaks yet she says nothing: what of that?\n\Her eye discourses; I will answer it.\n\I am too bold, 'tis not to me she speaks:\n\Two of the fairest stars in all the heaven,\n\Having some business, do entreat her eyes\n\To twinkle in their spheres till they return.\n\What if her eyes were there, they in her head?\n\The brightness of her cheek would shame those stars,\n\As daylight doth a lamp; her eyes in heaven\n\Would through the airy region stream so bright\n\That birds would sing and think it were not night.\n\See, how she leans her cheek upon her hand!\n\O, that I were a glove upon that hand,\n\That I might touch that cheek!\n\JULIET: Ay me!\n\ROMEO: She speaks:\n\O, speak again, bright angel! for thou art\n\As glorious to this night, being o'er my head\n\As is a winged messenger of heaven\n\Unto the white-upturned wondering eyes\n\Of mortals that fall back to gaze on him\n\When he bestrides the lazy-pacing clouds\n\And sails upon the bosom of the air.";var password = "O Romeo, Romeo! wherefore art thou Romeo?";var cipherText = AESEncryptCtr(plainText, password, 256);var decryptedText = AESDecryptCtr(cipherText, password, 256);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -