Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html><head><title>heh?</title></head><body>
- <!--
- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>
- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
- -->
- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core.js"></script>
- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/sha256.js"></script>
- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64.js"></script> <!-- only for testing -->
- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/cipher-core.js"></script>
- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/aes.js"></script>
- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/mode-ctr.js"></script>
- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/pad-nopadding.js"></script>
- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/pad-ansix923.js"></script>
- <script>
- var CS_256_SHA_AES_CTR = 1;
- // Note that CryptoJS uses MSB first for word arrays.
- // converts J/S Array (not [] array) into
- // CryptoJS WordArray.
- function makeWA(input) {
- var l = input.byteLength;
- if (l !== input.length) {
- throw new Error("only 8 byte arrays are allowed!");
- }
- var data = [];
- var acc = 0;
- var j = 0;
- var i = 0;
- while (i<l) {
- var r = i % 4;
- acc = acc | (input[i] << 8*(3-r));
- i++;
- if (r === 3 || i === l) {
- data[j++] = acc;
- acc = 0;
- }
- }
- return new CryptoJS.lib.WordArray.init(data, l);
- }
- // this function takes in WordArray and returns
- // back a Uint8Array
- function unmakeWA(res) {
- var l = res.sigBytes;
- var data = new Uint8Array(l);
- for (var i=0,j=-1; i<l; i++) {
- var r = i%4;
- if (r === 0) { j++; }
- var word = res.words[j] >> ((3-r)*8);
- data[i] = word;
- }
- return data;
- }
- // I assume that srpKey is Uint8Array
- // input is also a Uin8Array, containing IV followed by the data.
- function decrypt(input, srpKey, cipherSuite) {
- if (cipherSuite != CS_256_SHA_AES_CTR) {
- throw new Error("Unsupported cipher suite : " + cipherSuite);
- }
- var aesKey = CryptoJS.SHA256(makeWA(srpKey));
- // so that we know the size of the elements,
- // let's convert this to 8-byte array if it's not
- if (input.byteLength !== input.length) {
- input = new Uint8Array(input.buffer);
- }
- // we know that AES always has 128 bit block size, so that's
- // how much we need for IV (128/8 = 16)
- var iv = makeWA(input.subarray(0, 16));
- input = input.subarray(16);
- // console.log("decrypting bytes:"+JSON.stringify(input));
- // console.log("decrypting words:"+JSON.stringify(makeWA(input).words));
- // console.log("decrypting "+makeWA(input).sigBytes+" bytes");
- return CryptoJS.AES.decrypt({ciphertext:makeWA(input)}, aesKey, { iv: iv, mode: CryptoJS.mode.CTR, padding: CryptoJS.pad.NoPadding} ).toString(CryptoJS.enc.Utf8);
- }
- // input is string
- // output is Uint8Array
- function encrypt(input, srpKey, cipherSuite) {
- if (cipherSuite != CS_256_SHA_AES_CTR) {
- throw new Error("Unsupported cipher suite : " + cipherSuite);
- }
- var aesKey = CryptoJS.SHA256(makeWA(srpKey));
- // aesKey = new CryptoJS.lib.WordArray.init([0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c ]);
- // generate IV (again, 16 bytes)
- var iv = CryptoJS.lib.WordArray.random(16);
- // var iv = new CryptoJS.lib.WordArray.init([0,0,0,0]);
- // var iv = new CryptoJS.lib.WordArray.init([0xf0f1f2f3, 0xf4f5f6f7, 0xf8f9fafb, 0xfcfdfeff]);
- // input = new CryptoJS.lib.WordArray.init([0x6bc1bee2, 0x2e409f96, 0xe93d7e11, 0x7393172a ])
- var res = CryptoJS.AES.encrypt(input, aesKey, { iv : iv, mode : CryptoJS.mode.CTR, padding: CryptoJS.pad.NoPadding} ).ciphertext;
- // console.log("test decrypt:"+CryptoJS.AES.decrypt({ciphertext:res}, aesKey, {iv:iv, mode : CryptoJS.mode.CTR, padding: CryptoJS.pad.NoPadding}).toString(CryptoJS.enc.Utf8));
- // convert CryptoJS word array to J/S, but we also need to include IV.
- // IV is 4 words.
- // console.log("encrypted words:"+JSON.stringify(res.words));
- res.words.unshift(iv.words[0], iv.words[1], iv.words[2], iv.words[3]);
- res.sigBytes += 16;
- // res = new Uint32Array(res.words);
- // return new Uint8Array(res.buffer);
- // console.log("returning "+data.length+" bytes (cipher text is "+(data.length-16)+")");
- // return new Uint8Array(data);
- return unmakeWA(res);
- }
- // test
- var srpKey = new Uint8Array([1,2,3,4]);
- var testData = "brown fox jumped over things";
- console.log("plaintext:"+testData);
- var coded = encrypt(testData, srpKey, CS_256_SHA_AES_CTR);
- console.log("encoded: "+CryptoJS.enc.Base64.stringify(makeWA(coded)));
- var decoded = decrypt(coded, srpKey, CS_256_SHA_AES_CTR);
- console.log("decoded :"+decoded);
- function base64test(val) {
- console.log(decrypt(unmakeWA(CryptoJS.enc.Base64.parse(val)), srpKey, CS_256_SHA_AES_CTR));
- }
- </script>
- </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement