mitao
2024-04-30 ab4ea7b8f10c9b66aed9c2ea161a08b25c3851a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*!
 * Crypto-JS v1.1.0
 * http://code.google.com/p/crypto-js/
 * Copyright (c) 2009, Jeff Mott. All rights reserved.
 * http://code.google.com/p/crypto-js/wiki/License
 */
Crypto.mode.CBC = {
 
    encrypt: function (cipher, m, iv) {
 
        var blockSizeInBytes = cipher._blocksize * 4;
 
        // Pad
        m.push(0x80);
 
        // Encrypt each block
        for (var offset = 0; offset < m.length; offset += blockSizeInBytes) {
 
            if (offset == 0) {
                // XOR first block using IV
                for (var i = 0; i < blockSizeInBytes; i++)
                    m[i] ^= iv[i];
            }
            else {
                // XOR this block using previous crypted block
                for (var i = 0; i < blockSizeInBytes; i++)
                    m[offset + i] ^= m[offset + i - blockSizeInBytes];
            }
 
            // Encrypt block
            cipher._encryptblock(m, offset);
 
        }
 
    },
 
    decrypt: function (cipher, c, iv) {
 
        var blockSizeInBytes = cipher._blocksize * 4;
 
        // Decrypt each block
        for (var offset = 0; offset < c.length; offset += blockSizeInBytes) {
 
            // Save this crypted block
            var thisCryptedBlock = c.slice(offset, offset + blockSizeInBytes);
 
            // Decrypt block
            cipher._decryptblock(c, offset);
 
            if (offset == 0) {
                // XOR first block using IV
                for (var i = 0; i < blockSizeInBytes; i++)
                    c[i] ^= iv[i];
            }
            else {
                // XOR decrypted block using previous crypted block
                for (var i = 0; i < blockSizeInBytes; i++)
                    c[offset + i] ^= prevCryptedBlock[i];
            }
 
            // This crypted block is the new previous crypted block
            var prevCryptedBlock = thisCryptedBlock;
 
        }
 
        // Strip padding
        while (c.pop() != 0x80) ;
 
    }
 
};