凯撒加密方法将文件内容进行加密

 

#include <iostream>
#include <fstream>
using namespace std;
 
int main()
{
	ifstream iFile("D:\\测试1.txt");   //建立输入文件流对象
	iFile.seekg(0, ios::end);		
	int nFileLen = iFile.tellg();
	iFile.seekg(0, ios::beg);
	char *str = new char[nFileLen + 1];
	iFile.read(str, nFileLen);
	iFile.close();
	str[nFileLen] = 0;
	int nCaesar = 5;   //设置偏移量
	int n = 0;         //记录回车数目
	for (int i = 0; i < nFileLen; i++)
	{
		if ((str[i] >= 'a') && (str[i] <= 'z'))
		{
			str[i] += nCaesar;
			if (str[i] > 'z')
				str[i] -= 26;
		}
		else if ((str[i] >= 'A') && (str[i] <= 'Z'))
		{
			str[i] += nCaesar;
			if (str[i] > 'Z')
				str[i] -= 26;
		}
		else if (str[i] == '\n')
			n++;
	}
	ofstream oFile("D:\\测试2.txt");
	oFile.write(str, nFileLen - n);
	oFile.close();
	delete[] str;
	str = nullptr;
	return 0;
} 

这里有个凯撒加密方法例子参考一下!

#include<stdio.h>

#include<iostream>

#include<stdlib.h>

#include <string.h>

 

#define N 100

using namespace std;

//凯撒密码

//加密公式 f(a)=(a+3) mod 26

//解密公式 f(a)=(a+23) mod 26

 

void Encry(char *strI,int numB,int model);//加密公式函数

void Decry(char *strI,int numB,int model);//解密公式函数

int FileOut(char *strI);

int main()

{
         char str[N];

         int model;

         int numB;

        

         while(1)

         {
                   cout<<"凯撒密码:请选择模式:\n";

                   cout<<"1.加密\n";

                   cout<<"2.解密\n";

                   cout<<"3.退出\n";

                   cin>>model;

                   cout<<endl;

                  

                   switch(model)

                   {
                            case 1:

                                  cout<<"请输入要加密的字符串:";

                                     cin>>str;

                                     cout<<"请输入该密码算法的偏移数量:";

                                     cin>>numB;

                                     Encry(str,numB,model);

                                     cout<<endl;

                            break;

                          case 2:

                                  cout<<"请输入要解密的字符串:";

                                  cin>>str;

                                  cout<<"请输入原密码算法的偏移数量:";

                                  cin>>numB;

                                     Decry(str,numB,model);

                                  cout<<endl;

                         break;

                         case 3:

                                  return 0;

                         break;

                          default:

                                  break;

                   }

         }

        

         return 0;

}

 

void Encry(char *strI,int numB,int model)

{//明文串 秘钥 功能模式

         if(model==1)

         {
                   for(int i=0; i<strlen(strI); i++)

        {
            if(strI[i] >= 'A' && strI[i] <= 'Z')

            {
                strI[i] = ((strI[i]-'A')+numB)%26+'A';

            }

            else if(strI[i] >= 'a' && strI[i] <= 'z')

            {
                strI[i] = ((strI[i]-'a')+numB)%26+'a';

            }

        }

        cout<<"加密完成:"<<strI<<endl;

                   FileOut(strI);

                   cout<<"已输出到文件!"<<endl;

         }

         else

         {
                   cout<<"该模式不支持此项功能!"<<endl;

         }       

}

 

void Decry(char *strI,int numB,int model)

{
         if(model==2)

         {
                   int num;

                   num=26-numB;

                   for(int i=0; i<strlen(strI); i++)

        {
            if(strI[i] >= 'A' && strI[i] <= 'Z')

            {
                strI[i] = ((strI[i]-'A')+num)%26+'A';

            }

            else if(strI[i] >= 'a' && strI[i] <= 'z')

            {
                strI[i] = ((strI[i]-'a')+num)%26+'a';

            }

        }

        cout<<"解密完成:"<<strI<<endl;

         }

         else

         {
                   cout<<"该模式不支持此项功能!"<<endl;

         }       

}

 

int FileOut(char *strI)

{
         FILE *fp = NULL;

         int iWrite=0;

         int len=strlen(strI);

 

         if( strI == NULL || len ==0 )

                   return false;

 

         //! 打开文件句柄

         if( (fp = fopen( "密文.txt","w" )) == NULL )      // 文本模式写

                   return false;

 

         iWrite = fwrite(strI, 1, len, fp );

         fclose(fp);

 

         if( iWrite>0 )

                   return true;

         else

                   return false;
}

代码如上,万望采纳。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632