ash_wath

Affine

Nov 7th, 2017
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. using namespace std;
  5. int main()
  6. {
  7.     string plaintext,encrypt,decrypt;
  8.     int inv;
  9.     cout<<"Enter the message to be encrypted:"<<endl;
  10.     getline(cin,plaintext);
  11.     float a,b;
  12.     cout<<"Enter the values for a and b:"<<endl;
  13.     cin>>a>>b;
  14.     int length=(int)plaintext.length();
  15.     for(int i=0;i<length;i++)
  16.     {
  17.         encrypt[i]=(char)(fmod((a*(plaintext[i]-97)+b),26)+97);
  18.     }
  19.     for(int i=0;i<length;i++)
  20.     {
  21.         if(encrypt[i]=='V')
  22.         {
  23.             cout<<" ";
  24.         }
  25.         else
  26.         {
  27.             cout<<encrypt[i];
  28.         }
  29.     }
  30.     cout<<"\n";
  31.     cout<<"The deciphered text is:"<<endl;
  32.     int f=0;
  33.     inv=0;
  34.     for(int i=0;i<26;i++)
  35.     {
  36.         f=fmod((a*i),26);
  37.         if(f==1)
  38.         {
  39.             inv=i;
  40.         }
  41.     }
  42.     for(int i=0;i<length;i++)
  43.     {
  44.         decrypt[i]=(char)(fmod(inv*((encrypt[i]-97)-b),26)+97);
  45.     }
  46.     for(int i=0;i<length;i++)
  47.     {
  48.         if(decrypt[i]=='T')
  49.         {
  50.             cout<<" ";
  51.         }
  52.         else
  53.         {
  54.             cout<<decrypt[i];
  55.         }
  56.     }
  57. }
Add Comment
Please, Sign In to add comment