使用CryptoPP实现DES加密

DesUtil.h
#ifndef DESUTIL_H_
#define DESUTIL_H_

#include <string>

class DesUtil {
private:
	const char *keystr;
	void Base64en(const unsigned char *ming, unsigned char* enstr);
	void Base64de(const unsigned char* mi, unsigned char* destr);
public:
	DesUtil(const char *key);
	std::string Encrypt(const std::string &content);
	std::string Decrypt(const std::string &content);
};

#endif /* DESUTIL_H_ */

DesUtil.cpp


#include "DesUtil.h"
#include <iostream>
#include <des.h>
#include <base64.h>
#include <osrng.h>
#include <string>
#include <cstdlib>
#include <cryptlib.h>
#include <hex.h>
#include <filters.h>
#include <modes.h>
#include <secblock.h>

DesUtil::DesUtil(const char *key) : keystr(key) {
	// TODO Auto-generated constructor stub

}

void DesUtil::Base64en(const unsigned char* ming, unsigned char* enstr) {
	using CryptoPP::Base64Encoder;
	Base64Encoder base64en;
	base64en.Put(ming, strlen((const char *) ming));

	base64en.MessageEnd();
	//	base64en.MessageSeriesEnd();

	unsigned int size = base64en.MaxRetrievable();
	byte* temp = new byte[size + 1];
	temp[size] = '\0';

	base64en.Get(temp, size);

	//	cout << "Base64en: " << temp << endl;
	memcpy(enstr, temp, size + 1);
	delete temp;
}

void DesUtil::Base64de(const unsigned char* mi, unsigned char* destr) {
	using CryptoPP::Base64Decoder;
	Base64Decoder base64de;
	base64de.Put(mi, strlen((const char *) mi));
	base64de.MessageEnd();
	unsigned int size = base64de.MaxRetrievable();
	byte* temp = new byte[size + 1];
	temp[size] = '\0';
	base64de.Get(temp, size);
	//	cout << "Base64de: " << temp << endl;
	memcpy(destr, temp, size + 1);
	delete temp;
}

std::string DesUtil::Encrypt(const std::string & content) {
	using namespace CryptoPP;
	std::string cipher;
	try {
		ECB_Mode<DES>::Encryption e;
		e.SetKey((const unsigned char *) keystr, DES::DEFAULT_KEYLENGTH);

		// The StreamTransformationFilter adds padding
		//  as required. ECB and CBC Mode must be padded
		//  to the block size of the cipher.
		StringSource(content, true,
				new StreamTransformationFilter(e, new StringSink(cipher)) // StreamTransformationFilter
						);// StringSource
		byte * mi = new byte[strlen(content.c_str())*8];
		Base64en((unsigned char*) cipher.c_str(), mi);
		std::string result((char*) mi);
		delete mi;
		return result;
	} catch (const CryptoPP::Exception& e) {
		std::cerr << e.what() << std::endl;
		exit(1);
	}
	return NULL;
}

std::string DesUtil::Decrypt(const std::string & content) {
	using namespace CryptoPP;
	std::string recovered;
	byte * ming = new byte[strlen(content.c_str())*8];
	Base64de((unsigned char*) content.c_str(), ming);
	std::string temp((char *) ming);
	delete ming;
	try {
		ECB_Mode<DES>::Decryption d;
		d.SetKey((const unsigned char *) keystr, DES::DEFAULT_KEYLENGTH);

		// The StreamTransformationFilter removes
		//  padding as required.
		StringSource s(temp, true,
				new StreamTransformationFilter(d, new StringSink(recovered)) // StreamTransformationFilter
						);// StringSource
		return recovered;
//			std::cout << "recovered text: " << recovered << std::endl;
	} catch (const CryptoPP::Exception& e) {
		std::cerr << e.what() << std::endl;
		exit(1);
	}
	return NULL;
}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值