c 实现des算法

程序分三部分,des头文件,des类实现,main函数调用。

 

//panda
//2013-4-13
//des

//des.h

class DES
{
private:
	//public:
	//明文
	char msg[8];
	bool bmsg[64];
	//密钥
	char key[8];
	bool bkey[64];
	//16个子密钥
	bool subkey[16][48];
	//l0 r0中间变量
	bool rmsgi[32],lmsgi[32];//第i个
	bool rmsgi1[32],lmsgi1[32];//第i+1个
	//密文
	bool bcryptedmsg[64];
	char cryptedmsg[8];
	//解密的结果
	bool bdecipher[64];
	char decipher[8];
private:
	//静态常量

	//不允许在类内初始化
	//初始值换ip
	const static int ip[64];
	//子密钥
	//置换选择1
	const static int c0[28];
	const static int d0[28];
	//循环左移表
	const static int keyoff[16];
	//置换选择2
	const static int di[48];
	//加密函数
	//e运算
	const static int e_operate[48];
	//sbox
	const static int sbox[8][64];
	//置换运算p
	const static int p_operate[32];
	//逆初始置换ip
	const static int back_ip[64];
	//位掩码
	const static char bitmask[8];
public:
	//设置明文和密钥
	//_length要小于或等于8
	void SetMsg(char* _msg,int _length);
	void SetKey(char* _msg,int _length);
	//生产子密钥
	void ProduceSubKey();
	//总的的加密流程
	void Crypte();
	//解密
	void Decipher();
	//输出密文
	void OutPutCryptedMsg();
	//二进制转成字符
	void Bit2Char(bool* _barray,char* _carray);//length=64
	//输出解密后的明文
	void OutPutDecipher();
private:
	//字符转成二进制,并保存到64位bool数组中
	void Char2Bit(char* _carray,bool* _barray,int length);
	二进制转成字符
	//void Bit2Char(bool* _barray,char* _carray);//length=64
	//初始置换
	void InitSwap(bool in[64]);
	//初始逆置换
	void InitReSwap(bool out[64]);
	//循环左移
	void SubKeyOff(bool* _subkey,int _off);
	//e运算操作函数
	void EOperation(bool a[32],bool b[48]);
	//模2相加
	//相同为0 不同为1
	void Mode2Add(bool a[],bool b[],bool c[],int length);
	//sbox
	void DealSBox(bool in[48],bool out[32]);
	void _DealSBox(bool in[6],bool out[4],int box);
	//p opraration
	void POperation(bool temp[32],bool result[32]);
	//加密函数
	void CrypteFunction(bool in[32],int isubkey,bool out[32]);

	//数组之间赋值
	void CopyArray(bool array1[],bool array2[],int size);
};



//2013-4-13
//panda
//des
#include<string>
#include<iostream>
#include"des.h"
using namespace std;
//静态常量
const int DES::ip[64]={
	58,50,42,34,26,18,10,2,
	60,52,44,36,28,20,12,4,
	62,54,46,38,30,22,14,6,
	64,56,48,40,32,24,16,8,
	57,49,41,33,25,17,9,1,
	59,51,43,35,27,19,11,3,
	61,53,45,37,29,21,13,5,
	63,55,47,39,31,23,15,7
};
const int DES::c0[28]={
	57,49,41,33,25,17,9,
	1,58,50,42,34,26,18,
	10,2,59,51,43,35,27,
	19,11,3,60,52,44,36
};
const int DE
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DES算法是对称加密算法,可以用于对图片进行加密。以下是使用C语言实现DES算法加密图片的示例代码: ```c #include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/des.h> #define BLOCK_SIZE 8 // 加密图片文件 void encrypt_file(char *input_file, char *output_file, char *key) { FILE *fin, *fout; int i, j, len, remainder; unsigned char indata[BLOCK_SIZE], outdata[BLOCK_SIZE]; DES_key_schedule ks; DES_cblock key2; fin = fopen(input_file, "rb"); fout = fopen(output_file, "wb"); // 生成DES密钥 memset(key2, 0, sizeof(key2)); strncpy((char *)key2, key, sizeof(key2) - 1); DES_set_key_checked(&key2, &ks); while ((len = fread(indata, 1, BLOCK_SIZE, fin)) > 0) { // 补全数据块 remainder = BLOCK_SIZE - len; for (i = len; i < BLOCK_SIZE; i++) { indata[i] = remainder; } // 加密数据块 DES_ecb_encrypt(&indata, &outdata, &ks, DES_ENCRYPT); // 写入加密后的数据 fwrite(outdata, 1, BLOCK_SIZE, fout); } fclose(fin); fclose(fout); } int main() { char *key = "01234567"; char *input_file = "input.jpg"; char *output_file = "output.jpg"; encrypt_file(input_file, output_file, key); printf("加密完成\n"); return 0; } ``` 在上面的示例中,我们用到了OpenSSL库中的DES加密算法实现。程序读取输入文件,每次读取8个字节的数据块,对不足8个字节的数据块进行补全,然后使用DES算法对数据块进行加密,并将加密后的数据块写入输出文件中。 需要注意的是,由于DES算法加密后的数据块长度为8个字节,因此对于较大的图片文件,可能需要对数据块进行分组加密处理。此外,在实际应用中,为了提高加密强度,通常会使用更加安全的加密算法,如AES、RSA等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值