-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathcbc.c
60 lines (44 loc) · 1.17 KB
/
cbc.c
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
//
// cbc.c
// Algorithms - DES in CBC mode
//
// Created by YourtionGuo on 22/05/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <stdlib.h>
#include "bit.h"
#include "cbc.h"
#include "encrypt.h"
#pragma mark - Public
void cbc_encipher(const unsigned char *plaintext, unsigned char *ciphertext,
const unsigned char *key, int size)
{
unsigned char temp[8];
int i;
/// 加密初始向量
des_encipher(&plaintext[0], &ciphertext[0], key);
/// 使用 CBC 模式执行 DES 加密
i = 8;
while (i < size) {
bit_xor(&plaintext[i], &ciphertext[i - 8], temp, 64);
des_encipher(temp, &ciphertext[i], NULL);
i = i + 8;
}
return;
}
void cbc_decipher(const unsigned char *ciphertext, unsigned char *plaintext,
const unsigned char *key, int size)
{
unsigned char temp[8];
int i;
/// 解密初始向量
des_decipher(&ciphertext[0], &plaintext[0], key);
/// 使用 CBC 模式执行 DES 解密
i = 8;
while (i < size) {
des_decipher(&ciphertext[i], temp, NULL);
bit_xor(&ciphertext[i - 8], temp, &plaintext[i], 64);
i = i + 8;
}
return;
}