DES加密和解密算法的实现(C语言)

主要是做个记录,害怕以后代码丢了,先放到这里了。

DES再不进行介绍了,可以看上一篇的 DES 的python实现

转载请注明出处:https://www.cnblogs.com/wangyanzhong123/p/13784272.html

说明

这个版本用了很多位运算,也没有添加注释,所以可能阅读起来会有一些困难。其中实现的是 DES 的 CBC 模式下的加解密,可以保证的是结果一定是正确的。

源代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <time.h> const uint8_t IP[] = {
58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
}; const uint8_t invIP[] = {
40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25
}; const uint8_t E[] = {
32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1
}; const uint8_t P[] = {
16, 7, 20, 21, 29, 12, 28, 17,
1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9,
19, 13, 30, 6, 22, 11, 4, 25
}; const uint8_t S[8][64] = {{
/* S1 */
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
},{
/* S2 */
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
},{
/* S3 */
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
},{
/* S4 */
7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
},{
/* S5 */
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
},{
/* S6 */
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
},{
/* S7 */
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
},{
/* S8 */
13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
}}; const uint8_t PC1[] = {
57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
}; const uint8_t PC2[] = {
14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
}; const uint8_t shift_steps[] = {
1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
}; uint64_t pc1(uint64_t key){
uint64_t result = 0;
for (int i = 0; i < 56; i ++){
result = result << 1;
result = result | ((key >> (64-PC1[i])) & 0x0000000000000001);
}
return (result & 0x00ffffffffffffff);
} uint64_t pc2(uint64_t key){
uint64_t result = 0;
for (int i = 0; i < 48; i ++){
result = result << 1;
result = result | (uint64_t)((key >> (56-PC2[i])) & 0x0000000000000001);
}
return (result & 0x0000ffffffffffff);
} uint64_t ip(uint64_t input){
uint64_t result = 0;
for (int i = 0; i < 64; i ++){
result = result << 1;
result = result | ((input >> (64-IP[i])) & 0x0000000000000001);
}
return result;
} uint64_t inv_ip(uint64_t input){
uint64_t result = 0;
for (int i = 0; i < 64; i ++){
result = result << 1;
result = result | ((input >> (64-invIP[i])) & 0x0000000000000001);
}
return result;
} uint64_t extend(uint32_t input){
uint64_t result = 0;
for (int i = 0; i < 48; i ++){
result = result << 1;
result = result | (uint64_t)((input >> (32-E[i])) & 0x00000001);
}
return (result & 0x0000ffffffffffff);
} // uint32_t s(uint64_t input){
// uint32_t result = 0;
// uint8_t *blocks = (uint8_t*)malloc(sizeof(uint8_t)*8);
// uint8_t row, column;
// for (int i = 0; i < 8; i ++){
// blocks[i] = (uint8_t)((input >> (42 - 6*i)) & 0x000000000000003f);
// row = ((blocks[i] >> 4) & 0x02) | (blocks[i] & 0x01);
// column = (blocks[i] >> 1) & 0x0f;
// result = result << 4;
// result = result | (uint32_t)(S[i][row*16+column] & 0x0f);
// }
// free(blocks);
// return result;
// } uint32_t s(uint64_t input) {
uint32_t res = 0;
int row = 0;
int col = 0;
for (int i = 0; i < 8; i++) {
row = (uint8_t) ((input & (0x0000840000000000 >> 6*i)) >> (42-6*i));
row = (row >> 4) | (row & 0x01);
col = (uint8_t) ((input & (0x0000780000000000 >> 6*i)) >> (43-6*i));
res <<= 4;
res |= (uint32_t) (S[i][16*row+col] & 0x0f);
}
return res;
} uint32_t p(uint32_t input){
uint32_t result = 0;
for (int i = 0; i < 32; i ++){
result = result << 1;
result = result | ((input >> (32-P[i])) & 0x00000001);
}
return result;
} void to_uint64(uint8_t *buffer, size_t buffersize, uint64_t *result){
uint32_t tmp1 = 0, tmp2 = 0;
for (int i = 0; i < (buffersize/8); i ++){
tmp1 = (((uint32_t)buffer[8*i] << 24) & 0xff000000) | (((uint32_t)buffer[8*i+1] << 16) & 0x00ff0000) |
(((uint32_t)buffer[8*i+2] << 8) & 0x0000ff00) | (((uint32_t)buffer[8*i+3]) & 0x000000ff);
tmp2 = (((uint32_t)buffer[8*i+4] << 24) & 0xff000000) | (((uint32_t)buffer[8*i+5] << 16) & 0x00ff0000) |
(((uint32_t)buffer[8*i+6] << 8) & 0x0000ff00) | (((uint32_t)buffer[8*i+7]) & 0x000000ff);
result[i] = (((uint64_t)tmp1 << 32) & 0xffffffff00000000) | ((uint64_t)tmp2 & 0x00000000ffffffff);
}
} void to_uint8(uint64_t *buffer, size_t buffersize, uint8_t *result){
for (int i = 0; i < buffersize; i ++){
for (int j = 0; j < 8; j ++){
result[8*i+j] = (uint8_t)(buffer[i] >> (56-8*j));
}
}
} uint64_t hex_2_uint64(char *string){
uint64_t result = 0;
size_t len = 0;
len = strlen(string);
for (int i = 0; i < len; i ++){
if (string[15-i] >= '0' && string[15-i] <= '9') {
result += (uint64_t)(string[15-i]-'0') * ((uint64_t)(1) << (4*i));
} else if (string[15-i] >= 'a' && string[15-i] <= 'f') {
result += (uint64_t)(string[15-i]-'a' + 10) * ((uint64_t)(1) << (4*i));
} else if (string[15-i] >= 'A' && string[15-i] <= 'F') {
result += (uint64_t)(string[15-i]-'A' + 10) * ((uint64_t)(1) << (4*i));
}
}
return result;
} void generateSubkeys(uint64_t key, uint64_t* subkeys){
uint32_t left, right;
uint64_t roundkey = 0;
uint64_t key_tmp = 0;
key_tmp = pc1(key);
left = (uint32_t)((key_tmp >> 28) & 0x000000000fffffff);
right = (uint32_t)(key_tmp & 0x000000000fffffff);
for (int i = 0; i < 16; i ++){
int shift = shift_steps[i];
left = (0x0fffffff & (left << shift)) | (left >> (28-shift));
right = (0x0fffffff & (right << shift)) | (right >> (28-shift));
roundkey = ((uint64_t)left << 28) | (uint64_t)right;
roundkey = pc2(roundkey);
subkeys[i] = roundkey;
}
} uint64_t raw_des(uint64_t text, uint64_t key, uint8_t mode){
uint64_t* subkeys = (uint64_t*)malloc(sizeof(uint64_t)*16);
generateSubkeys(key, subkeys);
uint32_t L = 0, R = 0, SR = 0, PR = 0, tmp = 0;
uint64_t ER = 0, output = 0, subkey = 0;
text = ip(text);
L = (text >> 32) & 0x00000000ffffffff;
R = text & 0x00000000ffffffff;
for (int i = 0; i < 16; i ++){
ER = extend(R);
subkey = (mode == 0) ? subkeys[i] : subkeys[15-i];
ER = ER ^ subkey;
SR = s(ER);
PR = p(SR);
tmp = R;
R = L ^ PR;
L = tmp;
}
free(subkeys);
output = inv_ip(((uint64_t)(R) << 32 ) | (uint64_t)(L));
return output;
} uint64_t des_ede2(uint64_t text, uint64_t key1, uint64_t key2, uint8_t mode){
uint64_t result;
result = (mode == 0) ? raw_des(text, key1, 0) : raw_des(text, key1, 1);
result = (mode == 0) ? raw_des(result, key2, 1) : raw_des(result, key2, 0);
result = (mode == 0) ? raw_des(result, key1, 0) : raw_des(result, key1, 1);
return result;
} void des_cbc_enc(FILE* in, FILE* out, uint64_t key, uint64_t iv){
uint8_t *filebuffer;
size_t filesize = 0, padding_size = 0;
uint64_t *blocks, *output;
clock_t start, end;
fseek(in, 0, SEEK_END);
filesize = ftell(in);
rewind(in);
padding_size = 8 - filesize % 8;
filebuffer = (uint8_t*)malloc(sizeof(uint8_t)*(filesize + padding_size));
blocks = (uint64_t*)malloc(sizeof(uint64_t)*((filesize + padding_size)/8));
output = (uint64_t*)malloc(sizeof(uint64_t)*((filesize + padding_size)/8));
fread(filebuffer, sizeof(uint8_t), filesize, in);
memset(filebuffer+filesize, (uint8_t)padding_size, padding_size);
to_uint64(filebuffer, (filesize+padding_size), blocks);
start = clock();
for (int i = 0; i < (filesize+padding_size)/8; i ++){
output[i] = raw_des((blocks[i] ^ iv), key, 0);
iv = output[i];
}
end = clock();
fwrite(output, sizeof(uint64_t), ((filesize + padding_size)/8), out);
printf("time cost: %f s\n", (double)(end-start)/CLOCKS_PER_SEC);
free(filebuffer);
free(blocks);
free(output);
} void des_cbc_dec(FILE* in, FILE* out, uint64_t key, uint64_t iv){
uint64_t *filebuffer;
uint64_t *outbuffer;
uint8_t *out8_tbuffer;
size_t filesize = 0, padding_size = 0;
clock_t start, end;
fseek(in, 0, SEEK_END);
filesize = ftell(in);
rewind(in);
filebuffer = (uint64_t*)malloc(sizeof(uint64_t)*(filesize/8));
outbuffer = (uint64_t*)malloc(sizeof(uint64_t)*(filesize/8));
out8_tbuffer = (uint8_t*)malloc(sizeof(uint8_t)*filesize);
fread(filebuffer, sizeof(uint64_t), filesize/8, in);
start = clock();
for (int i = 0 ; i < (filesize/8); i ++){
outbuffer[i] = raw_des(filebuffer[i], key, 1) ^ iv;
iv = filebuffer[i];
}
end = clock();
to_uint8(outbuffer, filesize/8, out8_tbuffer);
padding_size = (size_t)out8_tbuffer[filesize-1];
fwrite(out8_tbuffer, sizeof(uint8_t), filesize-padding_size, out);
printf("time cost: %f s\n", (double)(end-start)/CLOCKS_PER_SEC);
free(filebuffer);
free(outbuffer);
free(out8_tbuffer);
} int main(int argc, char **argv){
char *inputfile = "-f";
char *inputstring = "-s";
char *mod = "-enc";
if (argc < 2){
printf("usage: %s 'string'\n", argv[0]);
return 1;
} else {
if (!strcmp(argv[1], inputfile)){
FILE *in, *out;
in = fopen(argv[3], "rb");
if (in == NULL) {
printf("%s not exist\n",argv[3]);
return -1;
} else {
out = fopen(argv[4], "wb+");
(!strcmp(argv[2],mod))? des_cbc_enc(in, out, hex_2_uint64(argv[5]), hex_2_uint64(argv[6]))
: des_cbc_dec(in, out, hex_2_uint64(argv[5]), hex_2_uint64(argv[6]));
fclose(in);
fclose(out);
}
} else if (!strcmp(argv[1], inputstring)) {
uint64_t cipher = 0;
clock_t start, end;
double duration;
start = clock();
for (int i = 0; i < 1000; i ++)
if(!strcmp(argv[2],mod))
cipher = des_ede2(hex_2_uint64(argv[3]), hex_2_uint64(argv[4]), hex_2_uint64(argv[5]), 0);
else
cipher = des_ede2(hex_2_uint64(argv[3]), hex_2_uint64(argv[4]), hex_2_uint64(argv[5]), 1);
end = clock();
duration = (double)(end - start) / (double)(CLOCKS_PER_SEC);
printf("%llx\n", cipher);
printf("enc/dec for 1000 times cost: %f s\n", duration);
}
}
system("pause");
}

DES加解密算法(C语言实现)的更多相关文章

  1. DES加解密算法Qt实现

      算法解密qt加密table64bit [声明] (1) 本文源码 大部分源码来自:DES算法代码.在此基础上,利用Qt编程进行了改写,实现了DES加解密算法,并添加了文件加解密功能.在此对署名为b ...

  2. JavaScript与C#互通的DES加解密算法

    原文地址:传送门 本文提供了一个能使JavaScript与C#互通的DES加解密算法的实现,在前台页面中用JavaScript版本的DES算法将数据加密之后,传到服务器端,在服务器端可用C#版本的DE ...

  3. DES加密解密算法C语言代码实现

    代码: #include<stdio.h> #include<string.h> #include<stdlib.h> /*-------------------- ...

  4. 实验一:C语言实现DES加解密算法

    计算程序执行10万次需要的时间: 总共需要175秒 加解密一次的时间小于:0.00175秒 纯计算加解密的时间会更短 去除IO操作后的时间 也就是说加解密一次的时间为0.07毫秒 /*-------- ...

  5. 实现与JS相同的Des加解密算法【转】

    Java代码 import java.util.ArrayList; import java.util.List; /** * DES加密/解密 * * @Copyright Copyright (c ...

  6. Des加解密算法

    class DesHelper    {        /// <summary>        /// DES加密方法        /// </summary>       ...

  7. C#加解密算法

    先附上源码 加密解密算法目前已经应用到我们生活中的各个方面 加密用于达到以下目的: 保密性:帮助保护用户的标识或数据不被读取. 数据完整性:帮助保护数据不被更改. 身份验证:确保数据发自特定的一方. ...

  8. JavaScript与C#互通的DES加解密算法的实现(转)

    本文提供了一个能使JavaScript与C#互通的DES加解密算法的实现,在前台页面中用JavaScript版本的DES算法将数据加密之后,传到服务器端,在服务器端可用C#版本的DES解密算法将其解密 ...

  9. PHP 基础篇 - PHP 中 DES 加解密详解

    一.简介 DES 是对称性加密里面常见一种,全称为 Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法.密钥长度是64位(bit),超过位数密钥被忽略.所谓对 ...

随机推荐

  1. 盒子上下滚动到js 底部触发的事件

    //html是用法举列子,js亲测有效(把这段js#scro加到你要滚动的盒子) <div id="scro">  <div>1</div> & ...

  2. OC基础--字符串

    前言 做iOS开发有3年了,从当初的小白到现在,断断续续看过很多资料,之前也写过一些博文来记录,但是感觉知识点都比较凌乱.所以最近准备抽时间把iOS开发的相关知识进行一个梳理,主要分为OC基础.UI控 ...

  3. 【pytest】teardown里的yield和addfinalizer

    在之前介绍pytest中的fixture用法的文章中https://zhuanlan.zhihu.com/p/87775743,提到了teardown的实现. 最近在翻pytest官方文档的时候,又发 ...

  4. SQL SERVER管理维护计划错误,备份错误,1053/3041/错误18204,严重性16,状态1

    在sqlserv2008/2012里设置了管理-维护计划-备份计划,前些天遇到报错-1053/3041/错误18204,严重性16,状态1等:分享下解决方法. 1.在服务器执行任务报错 2.解决办法 ...

  5. java初探(1)之秒杀的安全

    在秒杀的场景中还存在着很多的安全问题 暴露秒杀地址 秒杀请求可以很频繁 接口流量大,恶意刷接口 隐藏秒杀接口 为什么需要隐藏,事实上,页面上的所有东西都能被客户端拿到,包括js代码,因此,分析商品详情 ...

  6. css动画是否会被js阻塞

    css动画是否会被js阻塞 css的动画部分是会被js阻塞的,不过transform的动画则不会受影响. 下面举一个margin-left移动的动画下,启动js阻塞动画的性能图表 <style& ...

  7. Docker+Kubernetes/K8s+Jenkins视频资料【干货分享】

    不管你是否意识到,在这几年时间里,技术岗招聘重点关注的是Docker.Kubernetes(以下简称: K8S),面试10家公司得有8家会问你会不会"docker"."k ...

  8. 剑指 Offer 49. 丑数

    题目描述 我们把只包含质因子 2.3 和 5 的数称作丑数(Ugly Number).求按从小到大的顺序的第 n 个丑数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, ...

  9. C011:分数相加

    代码: #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { int up1,down1,up2,down2; do ...

  10. 再试Count(*) 与Count(*) 列

    试问,如果有一张表有两个字段,均可为空,插入两条首个字段为空的记录,再插入两条第二字段为空的记录,问count(*)和count(列)结果如何? 答案:count(*)是正常的四条,而count(列) ...