有一种技巧可以对数据进行加密,它使用一个单词作为它的密匙。下面是它的工作原理:首先,选择一个单词作为密匙,如TRAILBLAZERS。如果单词中包含有重复的字母,只保留第1个,其余几个丢弃。现在,修改过的那个单词属于字母表的下面,如下所示:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
T R A I L B Z E S C D F G H J K M N O P Q U V W X Y
上面其他用字母表中剩余的字母填充完整。在对信息进行加密时,信息中的每个字母被固定于顶上那行,并用下面那行的对应字母一一取代原文的字母(字母字符的大小写状态应该保留)。因此,使用这个密匙,Attack AT DAWN(黎明时攻击)就会被加密为Tpptad TP ITVH。
请实现下述接口,通过指定的密匙和明文得到密文。
本题有多组输入数据。
输入描述:
先输入key和要加密的字符串
输出描述:
返回加密后的字符串
示例1
输入:
nihao
ni
复制
输出:
le
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
void fun(string str1,string str2)
{
char strr[27]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string str=str1+strr;//将两个字符串合并到一起
int len=str.length();
char*p=(char*)str.data();
int sum=0,i,j,n=26;
for(i=0;i<len-1;i++)
{
for(j=i+1;j<len;j++)
{
if((p[i]==p[j])||(p[i]==p[j]+32)||(p[i]==p[j]-32))//去除重复的字符串
p[j]=' ';
}
}
char buff[26]={0};
j=0;
for(i=0;i<len;i++)
{
if(p[i]!=' ')
{
buff[j++]=p[i];
}
}
p=buff;
char *p_str2=(char*)str2.data();
char tmp;
for(i=0;i<str2.length();i++)
{
if(p_str2[i]>='a'&&p_str2[i]<='z')
{
tmp=p[p_str2[i]-'a'];
if(tmp>='a')
printf("%c",tmp);
else if(tmp<='Z')
printf("%c",tmp+32);
}else if(p_str2[i]>='A'&&p_str2[i]<='Z')
{
tmp=p[p_str2[i]-'A'];
if(tmp<='Z')
printf("%c",tmp);
else if(tmp>='a')
printf("%c",tmp-32);
}
}
printf("\n");
}
int main()
{
string str1;
string str2;
while(1)
{
cin>>str1;
if(str1.empty())
break;
cin>>str2;
fun(str1,str2);
str1.clear();
str2.clear();
}
return 0;
}

京公网安备 11010502036488号