Openssl 之 DES 学习笔记

一、主要API


typedef unsigned char DES_cblock[8];
 
//生成一个随机的 key,必须使用下面的 set_key 函数转换为 schedule 之后才能使用
void DES_random_key(DES_cblock *ret);
 
//功能:设置 key
//主要区别:是否检测 key 的奇偶校检位
//checked 会对奇偶校检位进行检查,如果校检位错误,返回-1,如果key强度比较弱,返回-2;
//unchecked 则不会检查
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);
 

/*
* brief: 使用 ECB 模式进行加密
* parameters:
* 		input: 输入数据(8字节长度)
* 		output: 输出数据(8字节长度)
* 		ks: 密钥
* 		enc: 加密 --> DES_ENCRYPT, 解密 --> DES_DECRYPT
*/
void DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output, DES_key_schedule *ks, int enc);
 

/*
* brief: 使用 CBC 模式进行加密
* parameters:
* 		input: 输入数据(8字节长度)
* 		output: 输出数据(8字节长度)
* 		length: 数据长度(不包括初始向量IV的长度)
* 		schedule: 密钥
* 		ivec: 初始向量(8字节,一般为0)
* 		enc: 加密 --> DES_ENCRYPT, 解密 --> DES_DECRYPT
* note:
* 		length 指明文或者密文长度
* 		如果 length 不是8的倍数,会使用00填充
*/
void DES_ncbc_encrypt(const unsigned char *input, unsigned char *output, long length, DES_key_schedule *schedule, DES_cblock *ivec, int enc);

二、代码示例

1. ECB 模式

#include <iostream>
#include <stdio.h>  
#include <openssl/des.h>

#pragma comment(lib,"libssl.lib")
#pragma comment(lib,"libcrypto.lib")

using namespace std;

int des_ecb_test()
{
	DES_cblock key = "heheheh";  // unsigned char[8] 类型
	//DES_random_key(&key);  // 设置随机密钥 

	DES_key_schedule schedule;
	DES_set_key_checked(&key, &schedule);  // 转换成 schedule  

	const_DES_cblock input = "heheheh";
	DES_cblock output;

	printf("Plaintext: %s\n", input);

	/* 加密  */
	DES_ecb_encrypt(&input, &output, &schedule, DES_ENCRYPT);
	printf("Encrypted!\n");

	printf("Ciphertext: ");
	int i;
	for (i = 0; i < sizeof(input); i++)
		printf("%02x", output[i]);
	printf("\n");

	/* 解密  */
	DES_ecb_encrypt(&output, &input, &schedule, DES_DECRYPT);
	printf("Decrypted!\n");
	printf("Plaintext: %s\n", input);
	// system("pause");
	return 0;
}

2. CBC 模式

int des_cbc_test()
{
	const char* keystring = "this is my key";
	DES_cblock key;
	DES_key_schedule key_schedule;

	/* 生成一个 key  */
	DES_string_to_key(keystring, &key);
	if (DES_set_key_checked(&key, &key_schedule) != 0) {
		printf("convert to key_schedule failed.\n");
		return -1;
	}


	//需要加密的字符串  
	unsigned char input[] = "Lhapy";
	size_t len = (sizeof(input) + 7) / 8 * 8;
	unsigned char* output = new unsigned char[len + 1];

	printf("PlainText: %s\n", input);


	DES_cblock ivec;  // 初始向量,IV  
	memset((char*)&ivec, 0, sizeof(ivec));  // IV 设置为 0x0000000000000000 

	/* 加密  */
	DES_ncbc_encrypt(input, output, sizeof(input), &key_schedule, &ivec, DES_ENCRYPT);

	printf("CipherText: ");
	for (int i = 0; i < len; ++i)
		printf("%02x ", output[i]);  // 输出加密以后的内容  
	printf("\n");


	/* 解密  */
	memset((char*)&ivec, 0, sizeof(ivec));
	DES_ncbc_encrypt(output, input, len, &key_schedule, &ivec, 0);

	printf("PlainText: %s\n", input);


	delete[] output;

	// system("pause");
	return 0;
}

永远别停下朝向更好的脚步!
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值