c++程序设计教程(钱能)字符串的加密与解密

记录学习c++的点滴!

编制程序,将输入的一行字符以加密的形式输出, 然后将其解密,解密的字符序列与输入的正文进行比较, 吻合时输出解密的正文, 否则输出解密失败。

加密时,将每个字符的 ASCII码依次反复加上“4962873”中的数字,并在32(' ')~122('z') 之间做模运算。解密与加密的顺序相反。例如,对于输入正文“the result of 3 and 2 is not 8”, 则运行结果为:

xqk口zlvyuzqn口6$jtf(9#m! &pw #

the result of 3  and 2  is not 8

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;

void code(string & s,const int *a);
void code(string & s, const int *a)
{
	for (int i = 0; i < s.size(); i++)
	{
		s[i] += a[i % 7];
		if (s[i]>122)
		{
			s[i] = s[i] % 91;
		}
		cout<<s[i];
	}
}
void encode(string & s, const int *a)
{
	for (int i = 0; i < s.size(); i++)
	{
		s[i] -= a[i % 7];
		if (s[i]<32)
		{
			s[i] = s[i] + 91;
		}
		cout << s[i];
	}
}

int main()
{
	string s;
	int key[7] = { 4, 9, 6, 2, 8, 7, 3 };
	cout << "please input the string:" << endl;
	getline(cin, s);
	//cout << s<<endl<<s.size();
	code(s, key);
	cout << endl;
	encode(s, key);
	system("pause");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值