转载来源http://www.qmailer.net/archives/188.html
DES api:
1、数据结构:
typedef unsigned char DES_cblock[8];
typedef /* const */ unsigned char const_DES_cblock[8];
typedef struct DES_ks
{
union
{
DES_cblock cblock;
DES_LONG deslong[2];
} ks[16];
} DES_key_schedule;
sizeof(DES_cblock) = 8字节
sizeof(const_DES_cblock ) = 8字节
sizeof(DES_key_schedule) = 128字节
2、基本宏定义
#define DES_ENCRYPT 1
#define DES_DECRYPT 0
3、设置密钥函数
//根据字符串生成key
void DES_string_to_key(const char *str, DES_cblock *key);
//will check that the key passed is of odd parity and is not a week or semi-weak key.
//If the parity is wrong, then -1 is returned. If the key is a weak key, then -2 is returned.
//If an error is returned, the key schedule is not generated
//设置密码表,并进行校验
int DES_set_key_checked(const_DES_cblock *key, DES_key_schedule *schedule);
//设置密码表,不需要校验
void DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule);
4、DES ECB模式加解密API
void DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output, DES_key_schedule *ks, int enc);
参数说明:
input:输入数据,8字节
output:输出数据,8字节
ks:密钥
enc:加密-DES_ENCRYPT,解密-DES_DECRYPT
5、DES CBC模式加解密API
void DES_ncbc_encrypt(const unsigned char *input, unsigned char *output, long length, DES_key_schedule *schedule, DES_cblock *ivec, int enc);
参数说明:
input: 输入参数,8字节倍数
output: 输出参数,8字节倍数
length: input的长度
schedule: 密钥
ivec: 初始向量, 8字节
enc: 加密-DES_ENCRYPT,解密-DES_DECRYPT