interop.html 2.22 KB
Newer Older
Daniel Hofmann's avatar
Daniel Hofmann committed
1
<!DOCTYPE html>
Daniel Hofmann's avatar
Daniel Hofmann committed
2
<html lang="en">
Daniel Hofmann's avatar
Daniel Hofmann committed
3
4
5
6
    <head>
        <meta charset="utf-8">
        <title>AES (in CBC mode) language interop tester</title>

Daniel Hofmann's avatar
Daniel Hofmann committed
7
        <!-- TODO(daniel) host locally! --!>
Daniel Hofmann's avatar
Daniel Hofmann committed
8
9
10
        <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
    </head>
    <body>
Daniel Hofmann's avatar
Daniel Hofmann committed
11
12
13
14
15
16
17
        <h3>AES-128 CBC, PKCS#7, nosalt</h3>
        <hr>
        <div>
            <input id="plaintext" type="text" name="plaintext" autofocus="autofocus" placeholder="My Secret..">
            <p id="ciphertext"></p>
            <p id="key"></p>
        </div>
Daniel Hofmann's avatar
Daniel Hofmann committed
18
19
20
21
    </body>
    <script>
        "use strict";

22
        var bs = 16;  // aes block size is 16 bytes
Daniel Hofmann's avatar
Daniel Hofmann committed
23

24
25
26
27
28
29
        document.getElementById('plaintext').onkeypress = function(e) {
          var event = e || window.event;
          var charCode = event.which || event.keyCode;
        
          if (charCode == '13') {  // enter pressed
            var plaintext = document.getElementById("plaintext").value;
Daniel Hofmann's avatar
Daniel Hofmann committed
30

31
32
33
            // note: no need for a key derivation functioni, we're _generating_ a key from scratch _every time_
            var key = CryptoJS.lib.WordArray.random(bs);
            var iv = CryptoJS.lib.WordArray.random(bs);
Daniel Hofmann's avatar
Daniel Hofmann committed
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
            var opts = {iv: iv, keySize: bs, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7};
            var enc = CryptoJS.AES.encrypt(plaintext, key, opts);

            console.log('ciphertext: ' + enc.ciphertext.toString(CryptoJS.enc.Hex));
            console.log('key: ' + enc.key.toString(CryptoJS.enc.Hex));
            console.log('iv: ' + enc.iv.toString(CryptoJS.enc.Hex));

            /* note:
                - no need for urlencode, hex is perfectly fine for urls
                - decoding: unhex 'out', first 16 bytes are iv, rest is ciphertext
                - or: split 'out', unhex first 32 bytes for iv, unhex rest for ciphertext
             */
            var out = enc.iv.toString(CryptoJS.enc.Hex) + enc.ciphertext.toString(CryptoJS.enc.Hex);

            document.getElementById("ciphertext").textContent = 'iv + ciphertext: ' + out;
            document.getElementById("key").textContent = 'key: ' + enc.key.toString(CryptoJS.enc.Hex);
          }
        }
Daniel Hofmann's avatar
Daniel Hofmann committed
53
54
55
    </script>
</html>
<!-- vim: set tabstop=4 shiftwidth=4 expandtab: --!>