【信息安全】Caesar加密

#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++)//strlen确定字符串长度 

        {

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

            {

                strI[i] = (strI[i]+numB)%26;//numB为偏移量 
             
             // f(a)=(a+3) mod 26 
             
            }

            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)//void Decry(char *strI,int numB,int model) 

{

         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;

}

猜你喜欢

转载自blog.csdn.net/qq_40270751/article/details/82626274