编程语言
首页 > 编程语言> > 维吉尼亚密码(完整代码,C++实现)

维吉尼亚密码(完整代码,C++实现)

作者:互联网

长时间不登录,今天一看,哟,居然涨粉了,那就发点东西吧,最近忙,等空下来再认真把把思路逻辑一类的内容整理一下,暂且只发代码,有需要的朋友们可以参考借鉴一下。

//vignere密码 
#include<iostream>
#include<cstring>
using namespace std;
//加密 
string encrypt(string p,string k)
{
  string c="";
  int lk=k.size(),pl=p.size(),s=0;
  for(int i=0;i<pl;i++)
  {
  	//其它字符 
  	if(p[i]>='a'&&p[i]<='z')
  	{
  		int j=(i-s)%lk;
		c+=(p[i]-'a'+k[j]-'a')%26+'a';

	} 
	else if(p[i]>='A'&&p[i]<='Z')
	{
		int j=(i-s)%lk;
		c+=(p[i]-'A'+k[j]-'a')%26+'A';
	}
	else
	{
  		c+=p[i];
	    s++;
	}
  }
  return c;
} 
//解密
string decrypt(string c,string k)
{
  string p="";
  int lk=k.size(),cl=c.size(),s=0;
  for(int i=0;i<cl;i++)
  {
	//其它字符 
  	if(c[i]>='a'&&c[i]<='z')
  	{
		int j=(i-s)%lk;
		p+=(c[i]-'a'+26-(k[j]-'a'))%26+'a';
	} 
	else if(c[i]>='A'&&c[i]<='Z')
	{
		int j=(i-s)%lk;
		p+=(c[i]-'A'+26-(k[j]-'a'))%26+'a';
	}
	else
	{
  		p+=c[i];
	    s++;
	}
  }
  return p;
} 
int main()
{
	string a,b;
	cout<<"请输入明文(可以输入任意符号,但仅对大小写字母进行加密):";
    getline(cin,a);
    cout<<"请输入密钥:"; 
	getline(cin,b);

	cout<<"密文为:"<<encrypt(a,b)<<endl;
	cout<<"明文为:"<<decrypt(encrypt(a,b),b); 
}

//today when people talk about red cross organization, they shake their heads and dont trust this organization. people react for it originated from about four years ago, at that time, a girl showed off her luxury in the public media, she told people that her father was a member of the red cross organization. more and more people knew her, they thought they were cheated by the organization, the money they donated was not used in the right way. the incident has a great negative influence on the red cross organization, people dot trust it any more, they are not willing to donate their money. as for me, i will not donate money to the organization, i choose to give money to the person who is in need of help, so the money wont be taken by others, make sure the person get the real money.

最后一行为提供的可以进行加密的文本。

标签:加密,string,int,C++,密码,&&,维吉尼亚,include,size
来源: https://blog.csdn.net/weixin_46342913/article/details/120397002