对文本简单加密解密算法

//单个字符异或运算
ENCRYPTDLL_API char MakecodeChar(char c, int key)
{
	return c = c^key;
}

//单个字符解密
ENCRYPTDLL_API char CutcodeChar(char c, int key)
{
	return c^key;
}

//加密
ENCRYPTDLL_API void Makecode(char *pstr, int *pkey)
{
	int len = strlen(pstr);//获取长度
	for (int i = 0; i<len; i++)
		*(pstr + i) = MakecodeChar(*(pstr + i), pkey[i % 5]);
}

//解密
 void Cutecode(char *pstr, int *pkey)
{
	int len = strlen(pstr);
	for (int i = 0; i<len; i++)
		*(pstr + i) = CutcodeChar(*(pstr + i), pkey[i % 5]);
}

//加密
 string Makecode(string &str, int *pkey)
{
	size_t bufSize = ((str.length()) + 1);
	char* p = new char[bufSize];
	memset(p, 0, bufSize);
	memcpy_s(p, bufSize, str.c_str(), str.length());
	Makecode(p, pkey);//加密
	string strResult = string(p);
	delete p;
	return strResult;
}

//解密
 string Cutecode(string &str, int *pkey)
{
	size_t bufSize = ((str.length()) + 1);
	char* p = new char[bufSize];
	memset(p, 0, bufSize);
	memcpy_s(p, bufSize, str.c_str(), str.length());
	Cutecode(p, pkey);//加密
	string strResult = string(p);
	delete p;
	return strResult;
}


int main()
{
	int key[] = { 1,2,3,4,5 };//加密字符
	string str = "新华社北京8月15日电(中国证券报记者倪铭娅)国家统计局新闻发言人付凌晖8月14日表示,总的来看,7月国民经济继续稳定恢复。从全年来看,保持经济稳定增长有很多有利条件。\
		分析人士认为,下阶段消费、投资等主要经济指标有望继续改善,经济复苏将更多地依赖需求回升。宏观政策有望协调发力,重点是扩内需。(小标题)需求恢复提速	付凌晖表示,7月经济运行情况可以概括为两个特点:一是经济稳定恢复,二是向好态势持续。";

	cout << "加密前:" << str.c_str() << endl;
	string jmResult = Makecode(str, key);//加密
	cout << "加密后:" << jmResult.c_str() << endl;
	string mResult = Cutecode(jmResult, key);//解密
	cout << "解密后:" << mResult.c_str() << endl;

	
	char c;
	cin >> c;
	return 0;

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值