Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<bits/stdc++.h>
- using namespace std;
- char str[100];
- int modInverse(int a, int m)
- {
- a %= m;
- for(int x = 1; x < m; x++)
- {
- if((a*x) % m == 1) return x;
- }
- }
- void encrypt()
- {
- getchar();
- int k1,k2,c,p,x,y,z;
- k1=7;
- k2=2;
- cout<<"Please type your plaintext: "<<endl;
- gets(str);
- for(int i=0; i<strlen(str); i++)
- {
- if(str[i]==' ') cout<<" ";
- else
- {
- p=str[i]-'a';
- p=p*k1;
- p=p+k2;
- p%=26;
- cout<<char(p+97);
- }
- }
- cout<<endl;
- }
- void decrypt()
- {
- getchar();
- int k1,k2,c,p,x,y,z;
- k1=7;
- k2=2;
- cout<<"Please type your ciphertext: "<<endl;
- gets(str);
- for(int i=0; i<strlen(str); i++)
- {
- if(str[i]==' ') cout<<" ";
- else
- {
- p=str[i]-'a';
- p=p+(26-k2);
- c=modInverse(k1,26);
- p=p*c;
- p=p%26;
- cout<<char(p+97);
- }
- }
- cout<<endl;
- }
- int main()
- {
- cout<<"Welcome to Assoscrypto system "<<endl;
- char ch;
- while(1)
- {
- cout<<"What do you want to perform? (encryption=e, decryption=d): "<<endl;
- cin>>ch;
- if(ch=='e') encrypt();
- else decrypt();
- cout<<"Do you want to continue ? (yes=y, no=n)"<<endl;
- cin>>ch;
- if(ch=='n')
- {
- cout<<"Thank you "<<endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement