说明:最近工作中用到3des(Triple DES)加密,网上的资料大部分都是介绍算法原理,没什么兴趣,man了一下查到openssl提供DES_ecb3_encrypt方法,正合我意!提示:openssl库支持很多加密算法哦,如:AES/DES/MD5/RSA...,而且很轻松的支持其他平台,我就在winXP下安装了openssl并在VC中设置头文件目录及库文件目录,用来是用openssl库。 代码下载:linux平台代码:http://pickup.mofile.com/2320229012095812windows平台代码:http://pickup.mofile.com/7098802825381299具体使用时需要注意des算法的加密模式、密钥长度、补齐方式,我这里采用3des的ECB方式、24位密钥(不足右补0)、内容长度以8字节切分,不能被8整除的末尾部分,根据长度不足8字节的部分,填充0x01-0x08
今天修正了一下代码里面一个常识性错误:des加密密钥也是8位来处理的,所以在密钥置换时只取8位。希望没有误导大家,我同时更新了源代码下载地址好代码,标准的位数不足应该都补0x00把另外,如何得到全部的加密后密文的16进制字符?
代码如下:
- #include
- #include
- #include
- #include
-
-
-
-
-
-
-
- int main(void)
- {
- int docontinue = 1;
- char *data = "hello world!";
- int data_len;
- int data_rest;
- unsigned char ch;
- unsigned char *src = NULL;
- unsigned char *dst = NULL;
- int len;
- unsigned char tmp[8];
- unsigned char in[8];
- unsigned char out[8];
- char *k = "01234567899876543210";
- int key_len;
- #define LEN_OF_KEY 24
- unsigned char key[LEN_OF_KEY];
- unsigned char block_key[9];
- DES_key_schedule ks,ks2,ks3;
-
- key_len = strlen(k);
- memcpy(key, k, key_len);
- memset(key + key_len, 0x00, LEN_OF_KEY - key_len);
-
- data_len = strlen(data);
- data_rest = data_len % 8;
- len = data_len + (8 - data_rest);
- ch = 8 - data_rest;
- src = (unsigned char *)malloc(len);
- dst = (unsigned char *)malloc(len);
- if (NULL == src || NULL == dst)
- {
- docontinue = 0;
- }
- if (docontinue)
- {
- int count;
- int i;
-
- memset(src, 0, len);
- memcpy(src, data, data_len);
- memset(src + data_len, ch, 8 - data_rest);
-
- memset(block_key, 0, sizeof(block_key));
- memcpy(block_key, key + 0, 8);
- DES_set_key_unchecked((const_DES_cblock*)block_key, &ks);
- memcpy(block_key, key + 8, 8);
- DES_set_key_unchecked((const_DES_cblock*)block_key, &ks2);
- memcpy(block_key, key + 16, 8);
- DES_set_key_unchecked((const_DES_cblock*)block_key, &ks3);
- printf("before encrypt:\n");
- for (i = 0; i < len; i++)
- {
- printf("0x%.2X ", *(src + i));
- }
- printf("\n");
-
- count = len / 8;
- for (i = 0; i < count; i++)
- {
- memset(tmp, 0, 8);
- memset(in, 0, 8);
- memset(out, 0, 8);
- memcpy(tmp, src + 8 * i, 8);
-
- DES_ecb3_encrypt((const_DES_cblock*)tmp, (DES_cblock*)in, &ks, &ks2, &ks3, DES_ENCRYPT);
-
- DES_ecb3_encrypt((const_DES_cblock*)in, (DES_cblock*)out, &ks, &ks2, &ks3, DES_DECRYPT);
-
- memcpy(dst + 8 * i, out, 8);
- }
- printf("after decrypt :\n");
- for (i = 0; i < len; i++)
- {
- printf("0x%.2X ", *(dst + i));
- }
- printf("\n");
- }
- if (NULL != src)
- {
- free(src);
- src = NULL;
- }
- if (NULL != dst)
- {
- free(dst);
- dst = NULL;
- }
- return 0;
- }
我给整理了一下,方便阅读
#include #include #include #include /************************************************************************ ** 本例采用: ** 3des-ecb加密方式; ** 24位密钥,不足24位的右补0x00; ** 加密内容8位补齐,补齐方式为:少1位补一个0x01,少2位补两个0x02,... ** 本身已8位对齐的,后面补八个0x08。 ************************************************************************/int main(void){ int docontinue = 1; unsigned char *data = "hello world!"; /* 明文 */ int data_len; int data_rest; unsigned char ch; unsigned char *src = NULL; /* 补齐后的明文 */ unsigned char *dst = NULL; /* 解密后的明文 */ int len; unsigned char tmp[8]; unsigned char in[8]; unsigned char out[8]; char *k = "01234567899876543210"; /* 原始密钥 */ int key_len;#define LEN_OF_KEY 24 unsigned char key[LEN_OF_KEY]; /* 补齐后的密钥 */ unsigned char block_key[9]; DES_key_schedule ks,ks2,ks3; /* 构造补齐后的密钥 */ key_len = strlen(k); memcpy(key, k, key_len); memset(key + key_len, 0x00, LEN_OF_KEY - key_len); /* 分析补齐明文所需空间及补齐填充数据 */ data_len = strlen(data); data_rest = data_len % 8; len = data_len + (8 - data_rest); ch = 8 - data_rest; src = malloc(len); dst = malloc(len); if (NULL == src || NULL == dst) { docontinue = 0; } if (docontinue) { int count; int i; /* 构造补齐后的加密内容 */ memset(src, 0, len); memcpy(src, data, data_len); memset(src + data_len, ch, 8 - data_rest); /* 密钥置换 */ memset(block_key, 0, sizeof(block_key)); memcpy(block_key, key + 0, 8); DES_set_key_unchecked((const_DES_cblock*)block_key, &ks); memcpy(block_key, key + 8, 8); DES_set_key_unchecked((const_DES_cblock*)block_key, &ks2); memcpy(block_key, key + 16, 8); DES_set_key_unchecked((const_DES_cblock*)block_key, &ks3); printf("before encrypt:"); for (i = 0; i < len; i++) { printf("0x%.2X ", *(src + i)); } printf("/n"); /* 循环加密/解密,每8字节一次 */ count = len / 8; for (i = 0; i < count; i++) { memset(tmp, 0, 8); memset(in, 0, 8); memset(out, 0, 8); memcpy(tmp, src + 8 * i, 8); /* 加密 */ DES_ecb3_encrypt((const_DES_cblock*)tmp, (DES_cblock*)in, &ks, &ks2, &ks3, DES_ENCRYPT); /* 解密 */ DES_ecb3_encrypt((const_DES_cblock*)in, (DES_cblock*)out, &ks, &ks2, &ks3, DES_DECRYPT); /* 将解密的内容拷贝到解密后的明文 */ memcpy(dst + 8 * i, out, 8); } printf("after decrypt :"); for (i = 0; i < len; i++) { printf("0x%.2X ", *(dst + i)); } printf("/n"); } if (NULL != src) { free(src); src = NULL; } if (NULL != dst) { free(dst); dst = NULL; } return 0;}