T1:關(guān)于3DES的分析 和 庫(kù)函數(shù)的思考——完全領(lǐng)悟了!?。?/p>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/des.h>
/************************************************************************
** 本例采用:
** 3des-ecb加密方式;
** 24位密鑰,不足24位的右補(bǔ)0x00;
** 加密內(nèi)容8位補(bǔ)齊,補(bǔ)齊方式為:少1位補(bǔ)一個(gè)0x01,少2位補(bǔ)兩個(gè)0x02,...
** 本身已8位對(duì)齊的,后面補(bǔ)八個(gè)0x08。
************************************************************************/
int main(void)
{
int docontinue = 1;
char *data = "hello world!"; /* 明文 */
int data_len;
int data_rest;
unsigned char ch;
unsigned char *src = NULL; /* 補(bǔ)齊后的明文 */
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]; /* 補(bǔ)齊后的密鑰 */
unsigned char block_key[9];
DES_key_schedule ks,ks2,ks3;
/* 構(gòu)造補(bǔ)齊后的密鑰 */
key_len = strlen(k);
memcpy(key, k, key_len);
memset(key + key_len, 0x00, LEN_OF_KEY - key_len);
/* 分析補(bǔ)齊明文所需空間及補(bǔ)齊填充數(shù)據(jù) */
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;
/* 構(gòu)造補(bǔ)齊后的加密內(nè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");
/* 循環(huán)加密/解密,每8字節(jié)一次 */
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);
/* 將解密的內(nèi)容拷貝到解密后的明文 */
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;
}
分析過程:
T(2):
使用openssl庫(kù)函數(shù) 實(shí)現(xiàn) DES加密:
使用函數(shù)DES_ecb_encrypt來進(jìn)行數(shù)據(jù)加解密
void DES_ecb_encrypt(const_DES_cblock *input,DES_cblock *output, DES_key_schedule *ks,int enc);
函數(shù)功能說明:DES ECB計(jì)算
參數(shù)說明:
input: 輸入數(shù)據(jù);(8字節(jié)長(zhǎng)度)
output: 輸出數(shù)據(jù);(8字節(jié)長(zhǎng)度)
ks: 密鑰;
enc:加密:DES_ENCRYPT , 解密:DES_DECRYPT;
解密:
使用函數(shù)DES_ncbc_encrypt來進(jìn)行數(shù)據(jù)加解密
void DES_ncbc_encrypt(const unsigned char *input,unsigned char *output,
long length,DES_key_schedule *schedule,DES_cblock *ivec,
int enc);
參數(shù)說明:
input: 輸入數(shù)據(jù);(8字節(jié)長(zhǎng)度)
output: 輸出數(shù)據(jù);(8字節(jié)長(zhǎng)度)
length: 數(shù)據(jù)長(zhǎng)度;(這里數(shù)據(jù)長(zhǎng)度不包含初始化向量長(zhǎng)度)
schedule:密鑰;
ivec: 初始化向量;(一般為8個(gè)字節(jié)0)
enc:加密:DES_ENCRYPT , 解密:DES_DECRYPT;
————————————————
版權(quán)聲明:本文為CSDN博主「寧?kù)o致遠(yuǎn)2021」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/m0_46577050/article/details/121711502文章來源:http://www.zghlxwxcb.cn/news/detail-437232.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-437232.html
到了這里,關(guān)于3DES實(shí)驗(yàn) 思考與練習(xí):的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!