http://www.caole.net/diary/des.html
Table of Contents
OpenSSL - DES
Summary
This library contains a fast implementation of the DES encryption algorithm.
There are two phases to the use of DES encryption.
- The first is the generation of a DESkeyschedule from a key,
- The second is the actual encryption. A DES key is of type DEScblock. This type is consists of 8 bytes with odd parity. The least significant bit in each byte is the parity bit. The key schedule is an expanded form of the key; it is used to speed the encryption process.
DES使用的例子
/**//*
gcc -o des-basic des-basic.c -lcrypto
*/
#include <stdio.h>
#include <openssl/des.h> int main(int argc,char **argv)
{
DES_cblock key;
/**//* DES_random_key(&key); */ /**//* generate a random key */
DES_string_to_key("pass", &key); DES_key_schedule schedule;
DES_set_key_checked(&key, &schedule); const_DES_cblock input = "hehehe";
DES_cblock output; printf("cleartext:%s ", input); DES_ecb_encrypt(&input, &output, &schedule, DES_ENCRYPT);
printf("Encrypted! "); printf("ciphertext:");
int i;
for (i = 0; i < sizeof(input); i++)
printf("%02x", output[i]);
printf(" "); DES_ecb_encrypt(&output, &input, &schedule, DES_DECRYPT);
printf("Decrypted! ");
printf("cleartext:%s ", input); return 0;
}
另一个带注释的例子
// DES encrypt with OpenSSL support
// g++ crypt.cc -lssl -o crypt #include <openssl/des.h>;
#include <iostream>; int main(int argc, char** argv)
{
try
{
std::string keystring("this is my key");// 初始化key(密钥)
DES_cblock key[1]; //具体是什么?不知道也一样可以用
DES_key_schedule key_schedule; //字面意思是密码表 if (NULL != argv[1])
keystring.assign(argv[1]);//如果命令行指定了key,那么就用指定的 // 生成一个 key
DES_string_to_key(keystring.c_str(), key);
if (DES_set_key_checked(key, &key_schedule) != 0)
throw "DES_set_key_schedule"; unsigned char input[] = "this is a text being encrypted by openssl";//需要加密的字符串
size_t len = (sizeof(input)+7)/8 * 8;
unsigned char *output = new unsigned char[len+1];
//加密以后的内容怎么分配内存,照上面这两步走就是了
//错了就干掉贴代码的人:mrgreen:
DES_cblock ivec;
//照这个搞就是了,用别人的代码不一定要知道所由的细节是为什么 // 加密
memset((char*)&ivec, 0, sizeof(ivec));//ivec清0
DES_ncbc_encrypt(input, output, sizeof(input), &key_schedule, &ivec, 1);//这里就进行加密了
std::cout << input << std::endl; //输出加密以后的内容
for (int i = 0; i < len; ++i)
printf("%02x", output[i]);
std::cout << std::endl; // 解密
memset((char*)&ivec, 0, sizeof(ivec));
DES_ncbc_encrypt(output, input, len, &key_schedule, &ivec, 0);
std::cout << input << std::endl; delete[] output;
}
catch(const char* errstr) //对抛出异常的处理
{
std::cerr << errstr << std::endl;
return EXIT_FAILURE;
} return EXIT_SUCCESS;
另一段Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/des.h> #define LEN_OF_KEY 24 int main(void)
{
char tmp_str[512];
int tmp_len; int docontinue = 1;
unsigned char *data = "530160";
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 = "12345678901234567890";
int key_len;
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, j; 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"); 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); for(j = 0; j < sizeof(in); j++){
printf("%02x", in[j]);
}
printf("\n"); 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("[%s]\n", dst);
} if (NULL != src)
{
free(src);
src = NULL;
}
if (NULL != dst)
{
free(dst);
dst = NULL;
} return 0;
}
Author: Le Cao
Date: 2010-10-12 18:19:41 CST
HTML generated by org-mode TAG=7.01g in emacs 23