⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 仿射密码.cpp

📁 用c++实现仿射密码
💻 CPP
字号:
#include<iostream>
#include<math.h>
using namespace std;
const int MAX=500;
   int w[26];
    char c[MAX];
    int i,temp,j,flag,k;
    
/*void show(char t[MAX]){   
  for(i=0;i<MAX;i++)
    cout<<t[i];
    cout<<endl;
               }*/
    
void  cal(char s[MAX]){            //求字母概率 
    float count=0.0;
    for(i=0;i<27;i++)
      w[i]=0;
    
    for(i=0;i<MAX;i++){
      
       temp=s[i];
       
       if(temp==' ')
         continue;
       else{
            count++;
            for(j='A';j<='Z';j++){
              if(temp==j)
                w[j-65]++;
              else if(temp==(j+32))
                w[j-65]++;
              else
               continue;}
           }
        }
        
        cout<<"字母出现概率:"<<endl; 
        for(i=0;i<26;i++){
         float lv=w[i]/count;
         char aph=65+i;
          cout<<aph<<'-'<<lv<<' ';}
          cout<<endl;
           cout<<endl;
   
} 

void encryption(char p[MAX]){       //加密 
    int a,b;
    cout<<"Please input the encryption key a and b :"<<endl;
    cin>>a>>b;
    while((fmod(a,2)==0)||(fmod(a,13)==0)){
      cout<<"invalid a(gcd(a,26) must equal 1)"<<endl;
      cout<<"reinput:"<<endl;
      cin>>a;}
      
    cout<<"encryption..."<<endl;
    cout<<"ciphertext:"<<endl;  
    
    
    for(i=0;i<k;i++){
      if(p[i]==' '){
       i++;
        continue;}
      else
        c[i]=(a*p[i]+b)%26+65;
      cout<<c[i];}
    cout<<endl;
    
    }   
               
int main(){
    
    char p[MAX]={"weTellPeopleTheyNeedToBecomePhysicallyActive ButInCertainNeighborhoodsIfYouGetOutAndGoForAWalkYouCouldBePuttingYourselfInHarmsWayFromEitherTrafficThatIsNotWellControlledOrOtherKindsOfThingsLikeViolenceInYourNerghborhood "}; 

    cout<<"plaintext:"<<endl;
    for(i=0;i<MAX;i++)
   {
     if(p[i]=='\0') {
      k=i;  
      break;}
     else
      cout<<p[i];}
      cout<<endl;
      cout<<endl;
    cout<<k<<endl;
    
    cal(p);
    
  flag=1;  
  while(flag){  
    encryption(p);
    cal(c);
    cout<<"input your choice 0(quit) or 1(change key):"<<endl;
    cin>>flag;
} 
    
    
   

    
}  
    
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -