-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes-cbc-encrypt-decrypt.html
executable file
·196 lines (166 loc) · 7.48 KB
/
aes-cbc-encrypt-decrypt.html
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<html>
<head>
<script type="text/javascript" src="DJS/encoding.js"></script>
<script type="text/javascript" src="DJS/hashing.js"></script>
<script type="text/javascript" src="DJS/aes.js"></script>
<script type="text/javascript" src="DJS/rsa.js"></script>
<script type="text/javascript" src="asn1JS.js"></script>
<script type="text/javascript" src="functions.js"></script>
<script type="text/javascript" src="algorithms.js"></script>
<script type="text/javascript" src="Key.js"></script>
<script type="text/javascript" src="generateKey.js"></script>
<script type="text/javascript" src="sign.js"></script>
<script type="text/javascript" src="decrypt.js"></script>
<script type="text/javascript" src="encrypt.js"></script>
<script type="text/javascript" src="exportKey.js"></script>
<script type="text/javascript" src="importKey.js"></script>
<script type="text/javascript" src="verify.js"></script>
<script type="text/javascript" src="digest.js"></script>
<script type="text/javascript" src="deriveKey.js"></script>
<script type="text/javascript" src="wrapKey.js"></script>
<script type="text/javascript" src="unwrapKey.js"></script>
<script type="text/javascript" src="resources/common.js"></script>
<script type="text/javascript" src="resources/js-test.js"></script>
<script type="text/javascript" src="subtleinriacrypto.js"></script>
<script type="text/javascript">
function init(){
var start = new Date().getTime();
window.crypto = inriacrypto;
var kAesCbcSuccessVectors = [
// 128-bit key with plaintext that is an exact multiple of block size.
// Derived from [1] F.2.1 (CBC-AES128.Encrypt), by adding padding block.
{
key: "2b7e151628aed2a6abf7158809cf4f3c",
iv: "000102030405060708090a0b0c0d0e0f",
plainText: "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
cipherText: "7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7" +
// Padding block.
"8cb82807230e1321d3fae00d18cc2012"
},
// 192-bit key, where final block of plaintext has to pad by 15.
// Derived from [1] F.2.3 (CBC-AES192.Encrypt), by stripping 15 bytes off
// plaintext and adding padding block.
{
key: "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
iv: "000102030405060708090a0b0c0d0e0f",
plainText: "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff6",
cipherText: "4f021db243bc633d7178183a9fa071e8b4d9ada9ad7dedf4e5e738763f69145a571b242012fb7ae07fa9baac3df102e0" +
// Padding block.
"288c6f9ec554652e50ab55e121f099ae"
},
// 256-bit key, where final block of plaintext has to pad by 3.
// Derived from [1] F.2.6 CBC-AES256.Decrypt, by stripping 3 bytes off
// plaintext and adding padding block.
{
key: "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
iv: "000102030405060708090a0b0c0d0e0f",
plainText: "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be6",
cipherText: "f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d39f23369a9d9bacfa530e26304231461c9aaf02a6a54e9e242ccbf48c59daca6"
},
// 128-bit key, with empty plaintext.
// Derived from Chromium's EncryptorTest.EmptyEncrypt() (encryptor_unittest.cc)
{
key: "3132383d5369787465656e4279746573",
iv: "5377656574205369787465656e204956",
plainText: "",
cipherText: "8518b8878d34e7185e300d0fcc426396"
},
];
function runAesCbcSuccessTestCase(testCase)
{
var algorithm = {name: 'AES-CBC', iv: hexStringToUint8Array(testCase.iv)};
var key = null;
var keyData = hexStringToUint8Array(testCase.key);
var usages = ['encrypt', 'decrypt'];
var extractable = false;
// (1) Import the key
return inriacrypto.subtle.importKey('raw', keyData, algorithm, extractable, usages).then(function(result) {
key = result;
// shouldBe() can only resolve variables in global context.
tmpKey = key;
//shouldEvaluateAs("tmpKey.type", "secret");
//shouldEvaluateAs("tmpKey.extractable", false);
//shouldEvaluateAs("tmpKey.algorithm.name", "AES-CBC");
//shouldEvaluateAs("tmpKey.algorithm.length", keyData.byteLength * 8);
//shouldEvaluateAs("tmpKey.usages.join(',')", "encrypt,decrypt");
// (2) Encrypt.
return inriacrypto.subtle.encrypt(algorithm, key, hexStringToUint8Array(testCase.plainText));
}).then(function(result) {
var finish = new Date().getTime();
console.log(finish-start);
//bytesShouldMatchHexString("Encryption", testCase.cipherText, result);
// (3) Decrypt
return inriacrypto.subtle.decrypt(algorithm, key, hexStringToUint8Array(testCase.cipherText));
}).then(function(result) {
//bytesShouldMatchHexString("Decryption", testCase.plainText, result);
});
}
var lastPromise = Promise.resolve(null);
kAesCbcSuccessVectors.forEach(function(test) {
lastPromise = lastPromise.then(runAesCbcSuccessTestCase.bind(null, test));
});
lastPromise.then(finishJSTest, failAndFinishJSTest);
//var key;
//var dataPart1 = convertPlainTextToArrayBufferView("hello,");
//var dataPart2 = convertPlainTextToArrayBufferView(" world!");
//console.log(dataPart1);
/*var algorithmKeyGen = {
name: "HMAC",
hash: {
name: "SHA-256",
},
length : 10
};
var algorithmSign = {
name: "HMAC",
hash: {
name: "SHA-256"
}
};
window.inriacrypto.subtle.generateKey(algorithmKeyGen, false, ["sign"]).then(
function(key) {
var dataPart1 = convertPlainTextToArrayBufferView("hello, world!");
return window.inriacrypto.subtle.sign(algorithmSign, key, dataPart1);
},
console.error.bind(console, "Unable to generate a key")
).then(
console.log.bind(console, "The signature is: "),
console.error.bind(console, "Unable to sign")
);
var clearDataArrayBufferView = convertPlainTextToArrayBufferView("Plain Text Data");
// TODO: create example utility function that converts text -> ArrayBufferView
var aesAlgorithmKeyGen = {
name: "AES-CBC",
// AesKeyGenParams
length: 128
};
var aesAlgorithmEncrypt = {
name: "AES-CBC",
// AesCbcParams
iv: window.inriacrypto.getRandomValues(new Uint8Array(16))
};
// Create a keygenerator to produce a one-time-use AES key to encrypt some data
window.inriacrypto.subtle.generateKey(aesAlgorithmKeyGen, false, ["encrypt"]).then(
function(aesKey) {
return window.inriacrypto.subtle.encrypt(aesAlgorithmEncrypt, aesKey,clearDataArrayBufferView);
}
).then(console.log.bind(console, "The ciphertext is: "),
console.error.bind(console, "Unable to encrypt"));
*/
/*var key = {
algorithm : null,
type : "secret",
data : "12345678",
extractable : false,
usages : ["sign"]
};
window.inriacrypto.subtle.sign(algorithmSign, key, "Hello").then(
console.log.bind(console, "The signature is: "),
console.error.bind(console, "Unable to sign")
);*/
}
</script>
</head>
<body onload="init()">
</body>
</html>