维吉尼亚密码(完整代码,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.

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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值