#include <bits/stdc++.h>

using namespace std;
string m= "abcdefghijklmnopqrstuvwxyz";

string enc(string s,int k1,int k2){
    int n=s.size();
    string ans;
    for(int i=0;i<n;i++){
        int a=(k1*(s[i]-'a')+k2)%26;
        ans+=m[a];
    }
    return ans;
}
string dec(string s,int k1_,int k2_){
    int n=s.size();
    string ans;
    for(int i=0;i<n;i++){
        int temp=(s[i]-'a');
        if(temp-k2_<=0) temp+=26;
        int a=(k1_*(temp-k2_))%26;
        ans+=m[a];
    }
    return ans;
}
int deckey(int k){
    int t;
    for(int i=0;i<26;i++){
        if((k*i)%26==1){
            t=i;
            break;
        }
    }
    return t;
}

vector<int> cry_anlys(string s,string s1){
    int a[26];
    for(int i=0;i<26;i++) a[i]=i+1;
    int b[12]={1,3,5,7,9,11,15,17,19,21,23,25};
    vector<int> ans;
    for(int i=0;i<12;i++){
        for(int j=0;j<26;j++){
            string temp=dec(s,b[i],a[j]);
            if(temp==s1){
                ans.push_back(deckey(b[i]));
                ans.push_back(a[j]);
                ans.push_back((i+1)*(25)+(j+1));
                break;
            }
        }
    }
    return ans;
}
int main()
{
    string s;
    cout<<"Enter the srting to be encrypted: "<<endl;
    cin>>s;
    int ek1,ek2;
    char ek1alpha;
    cout<<"Enter the key1(should be small english alphabet): "<<endl;
    cin>>ek1alpha;
    cout<<"Enter the key2: "<<endl;
    cin>>ek2;
    ek1=ek1alpha-'a';
    int dk1=deckey(ek1);
    int dk2=ek2;
    string em=enc(s,ek1,ek2);
    string dm=dec(em,dk1,dk2);
    cout<<"entrypted string: ";
    cout<<em<<endl;
    cout<<"decrypted string: ";
    cout<<dm<<endl;
    vector<int> v1=cry_anlys(em,s);
    cout<<"**CRYPTANALYSIS**"<<endl;
    cout<<"analysed encryption key 1 is: "<<m[v1[0]]<<endl;
    cout<<"analysed encryption key 2 is: "<<v1[1]<<endl;
    cout<<"no. of attempt taken is: "<<v1[2]<<endl;
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: