dcrypt.blockcipher.modes.cbc

Members

Structs

CBC
struct CBC(Cipher)

implements Cipher-Block-Chaining (CBC) mode on top of a simple cipher.

Examples

1 import dcrypt.blockcipher.aes;
2 import std.range;
3 import std.conv: text;
4 
5 CBC!AES cbc;
6 
7 const ubyte[] key = cast(const ubyte[])x"2b7e151628aed2a6abf7158809cf4f3c";
8 const ubyte[] iv = cast(const ubyte[])x"000102030405060708090a0b0c0d0e0f";
9 
10 const ubyte[] plain = cast(const ubyte[])x"
11 	6bc1bee22e409f96e93d7e117393172a
12 	ae2d8a571e03ac9c9eb76fac45af8e51
13 	30c81c46a35ce411e5fbc1191a0a52ef
14 	f69f2445df4f9b17ad2b417be66c3710
15 ";
16 
17 const ubyte[] expected_ciphertext = cast(const ubyte[])x"
18 	7649abac8119b246cee98e9b12e9197d
19 	5086cb9b507219ee95db113a917678b2
20 	73bed6b8e3c1743b7116e69e22229516
21 	3ff1caa1681fac09120eca307586e1a7
22 ";
23 
24 
25 // encryption mode
26 cbc.start(true, key, iv);
27 
28 ubyte[plain.length] buf;
29 buf = plain;
30 
31 foreach(block; chunks(buf[],16)) {
32 	cbc.processBlock(block,block);
33 }
34 
35 assert(buf == expected_ciphertext, text(cbc.name,": encryption failed"));
36 
37 // decryption mode
38 cbc.start(false, key, iv);
39 
40 foreach(block; chunks(buf[],16)) {
41 	cbc.processBlock(block,block);
42 }
43 
44 assert(buf == plain, text(cbc.name,": decryption failed"));

Meta