C++简单加密解密(凯撒密码学习)

C++简单加密解密(凯撒密码学习)

总体概览:先建立KEY和PWD两个密码表,用于大小写转换。Encryption加密函数,Decryption解密函数。加密解密过程中,将每一个字符都先转为int类型,进行判断是否属于大小写字母。之后把下标规定范围为0到25,并使用密码表内的字符进行相应的赋值。read_class函数用去读取文件,write_class函数用于写入文件。

这里变量的命名不太规范,忽略就好QAQ。

要求:利用编程技能实现凯撒密码

  1. 小写字母变成后三个大写字母
  2. 大写字母变成前两个小写字母
  3. 其他符号不变
  4. 使用读取写入文件实现

读取写入文件需要读入空格问题

#include <bits/stdc++.h>
using namespace std;
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
char KEY[26]={'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'};
char PWD[26]={'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'};
string str;
void Encryption (int n);
void Decryption (int n);
int read_class();
void write_class();
int main()
{
	_;
	int length;
	int flag;
	while(true)
	{
		cout<<"请输入你的操作:"<<endl;
		cout<<"1---读取字符串"<<endl<<"2---写入字符串"<<endl<<"3---加密"<<endl<<"4---解密"<<endl<<"5---退出"<<endl;
		cin>>flag;
		if(flag == 1)
		{
			length = read_class();
		}
		else if(flag==3)
			Encryption(length);
		else if(flag ==4)
			Decryption(length);
		else if(flag==5)
			break;
		else if(flag==2)
			write_class();
		else
			cout<<"输入错误,请重新输入!"<<endl;
	}
	return EXIT_SUCCESS;
}
//加密 
void Encryption (int n)
{
	int index;
	for(int i=0;i<n;i++)
	{
		index = (int)str[i];
		if(str[i]>='a'&&str[i]<='z')
		{
			index -=(int)'a';
			index +=3;
			if(index >= 26)
			index-=26;
			str[i] = PWD[index];
		}else if(str[i]>='A'&&str[i]<='Z')
		{
			index -=(int)'A';
			index -=2;
			if(index < 0)
			index +=26;
			str[i] = KEY[index];
		}
	}
	cout<<"加密成功"<<endl;
	cout<<str<<endl;
}
//解密 
void Decryption (int n)
{
	int index;
	for(int i=0;i<n;i++)
	{
		index = (int)str[i];
		if(str[i]>='A'&&str[i]<='Z')
		{
			index -=(int)'A';
			index -=3;
			if(index < 0)
			index +=26;
			str[i] = KEY[index];
		}else if(str[i]>='a'&&str[i]<='z')
		{
			index -=(int)'a';
			index +=2;
			if(index >= 26)
			index -=26;
			str[i] = PWD[index];
		}
	}
	cout<<"解密成功"<<endl; 
	cout<<str<<endl;
}
//读入 
int read_class()
{
	ifstream f;
	f.open("passwd.txt");
	while(getline(f,str))
	cout<<str<<endl;
	f.close();
	int i=(int)str.length();
	cout<<"读取成功!"<<endl;
	return i;
}
//写入 
void write_class()
{
	fstream f;
	f.open("passwd.txt",ios::app);
	f<<str<<endl;
	f.close();
	cout<<"写入成功!"<<endl; 
}

最后实现
1.加密
在这里插入图片描述
包含大写小写字母数字空格和特殊字符
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
此时再查看原文件。
在这里插入图片描述
红框为加密后的代码。

2.解密
在这里插入图片描述
采用上面加密后密文
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
此时再查看原文件。
在这里插入图片描述
进行对比
在这里插入图片描述
在这里插入图片描述
结果正确

  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
密码是一种简单的替换密码,它通过将明文中的每个字母按照一定的偏移量进行替换来实现加密解密。在C++中,可以使用字符的ASCII码值来进行偏移和替换操作。 下面是一个简单密码解密的C++代码示例: ```cpp #include <iostream> using namespace std; // 密码加密函数 string caesarEncrypt(string plaintext, int shift) { string ciphertext = ""; for (int i = 0; i < plaintext.length(); i++) { // 对字母进行偏移 if (isalpha(plaintext[i])) { char shiftedChar = (toupper(plaintext[i]) - 'A' + shift) % 26 + 'A'; ciphertext += shiftedChar; } else { ciphertext += plaintext[i]; } } return ciphertext; } // 密码解密函数 string caesarDecrypt(string ciphertext, int shift) { string plaintext = ""; for (int i = 0; i < ciphertext.length(); i++) { // 对字母进行逆向偏移 if (isalpha(ciphertext[i])) { char shiftedChar = (toupper(ciphertext[i]) - 'A' - shift + 26) % 26 + 'A'; plaintext += shiftedChar; } else { plaintext += ciphertext[i]; } } return plaintext; } int main() { string plaintext = "HELLO WORLD"; int shift = 3; // 加密 string ciphertext = caesarEncrypt(plaintext, shift); cout << "加密后的密文:" << ciphertext << endl; // 解密 string decryptedText = caesarDecrypt(ciphertext, shift); cout << "解密后的明文:" << decryptedText << endl; return 0; } ``` 这段代码中,`caesarEncrypt`函数用于加密明文,`caesarDecrypt`函数用于解密密文。其中,`shift`参数表示偏移量,可以是任意整数。在加密时,将明文中的每个字母按照偏移量进行替换;在解密时,将密文中的每个字母按照逆向偏移量进行替换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七七高7777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值