200119题

在这里插入图片描述

//字符串明文加密及测试

#include<iostream>     
using namespace std;

char* encryption(char str[], int code[], int codenum);
int main()
{
	int code[] = { 5, 9, 8, 4, 3, 2 };			//密钥数组
	char msg[] = "ABuio1987";					//明文字符串
	char *str_encode;							//密文字符串指针
												//添加代码,完成测试
	str_encode = encryption(msg, code, 6);


	cout << "明文:" << msg << endl;			// printf("明文:%s\n", msg);
	cout << "密文:" << str_encode << endl;     // printf("密文:%s\n", str_encode);

	delete str_encode;
	system("pause");
	return 0;
}

//对str加密,返回值为密文字符串的指针
char* encryption(char str[], int code[], int codenum)
{
	char*str_encode = new char[strlen(str)+1];
	for (int i = 0; i < strlen(str); i++)
	{
		int index = i;
		if (index >= codenum)
			index = index%codenum;

		str_encode[i] = code[index]+str[i];
		if (str_encode[i] > 'Z'&&str[i]>='A'&&str[i] <= 'Z')
		{
			str_encode[i] = 'A' + (int)(str_encode[i] - 'Z'-1);
		}
		if (str_encode[i] > 'z'&&str[i] >= 'a'&&str[i] <= 'z')
		{
			str_encode[i] = 'a' + (int)(str_encode[i] - 'z'-1);
		}
		if (str_encode[i] > '9'&&str[i] >= '0'&&str[i] <= '9')
		{
			str_encode[i] = '0' + (int)(str_encode[i] - '9' - 1);
		}
		
	}
	str_encode[strlen(str)] = '\0';
	return str_encode;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值