Advertisement
Guest User

encryption

a guest
Mar 27th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6.  
  7. char str[100];
  8. int modInverse(int a, int m)
  9. {
  10.     a %= m;
  11.     for(int x = 1; x < m; x++)
  12.     {
  13.         if((a*x) % m == 1) return x;
  14.     }
  15. }
  16.  
  17. void encrypt()
  18. {
  19.     getchar();
  20.     int k1,k2,c,p,x,y,z;
  21.     k1=7;
  22.     k2=2;
  23.     cout<<"Please type your plaintext:  "<<endl;
  24.     gets(str);
  25.     for(int i=0; i<strlen(str); i++)
  26.     {
  27.         if(str[i]==' ') cout<<" ";
  28.         else
  29.         {
  30.             p=str[i]-'a';
  31.             p=p*k1;
  32.             p=p+k2;
  33.             p%=26;
  34.             cout<<char(p+97);
  35.         }
  36.     }
  37.     cout<<endl;
  38. }
  39.  
  40. void decrypt()
  41. {
  42.     getchar();
  43.     int k1,k2,c,p,x,y,z;
  44.     k1=7;
  45.     k2=2;
  46.     cout<<"Please type your ciphertext: "<<endl;
  47.     gets(str);
  48.     for(int i=0; i<strlen(str); i++)
  49.     {
  50.         if(str[i]==' ') cout<<" ";
  51.         else
  52.         {
  53.             p=str[i]-'a';
  54.             p=p+(26-k2);
  55.             c=modInverse(k1,26);
  56.             p=p*c;
  57.             p=p%26;
  58.             cout<<char(p+97);
  59.         }
  60.     }
  61.     cout<<endl;
  62. }
  63.  
  64.  
  65.  
  66. int main()
  67. {
  68.     cout<<"Welcome to Assoscrypto system "<<endl;
  69.     char ch;
  70.     while(1)
  71.     {
  72.         cout<<"What do you want to perform? (encryption=e, decryption=d): "<<endl;
  73.  
  74.         cin>>ch;
  75.  
  76.         if(ch=='e') encrypt();
  77.         else decrypt();
  78.         cout<<"Do you want to continue ? (yes=y, no=n)"<<endl;
  79.         cin>>ch;
  80.         if(ch=='n')
  81.         {
  82.             cout<<"Thank you "<<endl;
  83.             break;
  84.         }
  85.  
  86.     }
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement