Advertisement
pveselov

CryptoJS example

Aug 31st, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.16 KB | None | 0 0
  1. <html><head><title>heh?</title></head><body>
  2.  
  3. <!--
  4. <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>
  5. <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
  6. -->
  7. <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core.js"></script>
  8. <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/sha256.js"></script>
  9. <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64.js"></script> <!-- only for testing -->
  10. <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/cipher-core.js"></script>
  11. <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/aes.js"></script>
  12. <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/mode-ctr.js"></script>
  13. <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/pad-nopadding.js"></script>
  14. <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/pad-ansix923.js"></script>
  15.  
  16. <script>
  17.  
  18. var CS_256_SHA_AES_CTR = 1;
  19.  
  20. // Note that CryptoJS uses MSB first for word arrays.
  21. // converts J/S Array (not [] array) into
  22. // CryptoJS WordArray.
  23. function makeWA(input) {
  24.  
  25.     var l = input.byteLength;
  26.  
  27.     if (l !== input.length) {
  28.         throw new Error("only 8 byte arrays are allowed!");
  29.     }
  30.  
  31.     var data = [];
  32.     var acc = 0;
  33.     var j = 0;
  34.     var i = 0;
  35.     while (i<l) {
  36.  
  37.        var r = i % 4;
  38.        acc = acc | (input[i] << 8*(3-r));
  39.        i++;
  40.        if (r === 3 || i === l) {
  41.            data[j++] = acc;
  42.            acc = 0;
  43.        }
  44.    }
  45.  
  46.    return new CryptoJS.lib.WordArray.init(data, l);
  47. }
  48.  
  49. // this function takes in WordArray and returns
  50. // back a Uint8Array
  51. function unmakeWA(res) {
  52.    var l = res.sigBytes;
  53.    var data = new Uint8Array(l);
  54.    for (var i=0,j=-1; i<l; i++) {
  55.        var r = i%4;
  56.        if (r === 0) { j++; }
  57.        var word = res.words[j] >> ((3-r)*8);
  58.         data[i] = word;
  59.     }
  60.     return data;
  61. }
  62.  
  63. // I assume that srpKey is Uint8Array
  64. // input is also a Uin8Array, containing IV followed by the data.
  65. function decrypt(input, srpKey, cipherSuite) {
  66.     if (cipherSuite != CS_256_SHA_AES_CTR) {
  67.         throw new Error("Unsupported cipher suite : " + cipherSuite);
  68.     }
  69.     var aesKey = CryptoJS.SHA256(makeWA(srpKey));
  70.     // so that we know the size of the elements,
  71.     // let's convert this to 8-byte array if it's not
  72.     if (input.byteLength !== input.length) {
  73.         input = new Uint8Array(input.buffer);
  74.     }
  75.     // we know that AES always has 128 bit block size, so that's
  76.     // how much we need for IV (128/8 = 16)
  77.     var iv = makeWA(input.subarray(0, 16));
  78.     input = input.subarray(16);
  79.  
  80.     // console.log("decrypting bytes:"+JSON.stringify(input));
  81.     // console.log("decrypting words:"+JSON.stringify(makeWA(input).words));
  82.     // console.log("decrypting "+makeWA(input).sigBytes+" bytes");
  83.  
  84.     return CryptoJS.AES.decrypt({ciphertext:makeWA(input)}, aesKey, { iv: iv, mode: CryptoJS.mode.CTR, padding: CryptoJS.pad.NoPadding} ).toString(CryptoJS.enc.Utf8);
  85. }
  86.  
  87. // input is string
  88. // output is Uint8Array
  89. function encrypt(input, srpKey, cipherSuite) {
  90.     if (cipherSuite != CS_256_SHA_AES_CTR) {
  91.         throw new Error("Unsupported cipher suite : " + cipherSuite);
  92.     }
  93.     var aesKey = CryptoJS.SHA256(makeWA(srpKey));
  94.     // aesKey = new CryptoJS.lib.WordArray.init([0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c ]);
  95.     // generate IV (again, 16 bytes)
  96.     var iv = CryptoJS.lib.WordArray.random(16);
  97.     // var iv = new CryptoJS.lib.WordArray.init([0,0,0,0]);
  98.     // var iv = new CryptoJS.lib.WordArray.init([0xf0f1f2f3, 0xf4f5f6f7, 0xf8f9fafb, 0xfcfdfeff]);
  99.     // input = new CryptoJS.lib.WordArray.init([0x6bc1bee2, 0x2e409f96, 0xe93d7e11, 0x7393172a ])
  100.     var res = CryptoJS.AES.encrypt(input, aesKey, { iv : iv, mode : CryptoJS.mode.CTR, padding: CryptoJS.pad.NoPadding} ).ciphertext;
  101.     // console.log("test decrypt:"+CryptoJS.AES.decrypt({ciphertext:res}, aesKey, {iv:iv, mode : CryptoJS.mode.CTR, padding: CryptoJS.pad.NoPadding}).toString(CryptoJS.enc.Utf8));
  102.     // convert CryptoJS word array to J/S, but we also need to include IV.
  103.     // IV is 4 words.
  104.     // console.log("encrypted words:"+JSON.stringify(res.words));
  105.     res.words.unshift(iv.words[0], iv.words[1], iv.words[2], iv.words[3]);
  106.     res.sigBytes += 16;
  107.     // res = new Uint32Array(res.words);
  108.     // return new Uint8Array(res.buffer);
  109.     // console.log("returning "+data.length+" bytes (cipher text is "+(data.length-16)+")");
  110.     // return new Uint8Array(data);
  111.     return unmakeWA(res);
  112. }
  113.  
  114. // test
  115.  
  116. var srpKey = new Uint8Array([1,2,3,4]);
  117. var testData = "brown fox jumped over things";
  118. console.log("plaintext:"+testData);
  119. var coded = encrypt(testData, srpKey, CS_256_SHA_AES_CTR);
  120. console.log("encoded: "+CryptoJS.enc.Base64.stringify(makeWA(coded)));
  121. var decoded = decrypt(coded, srpKey, CS_256_SHA_AES_CTR);
  122. console.log("decoded  :"+decoded);
  123.  
  124. function base64test(val) {
  125.     console.log(decrypt(unmakeWA(CryptoJS.enc.Base64.parse(val)), srpKey, CS_256_SHA_AES_CTR));
  126. }
  127.  
  128. </script>
  129.  
  130. </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement