Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int expmod(int x, int y, int n)
- {
- int r=1;
- while(y>0)
- {
- if((int)(y%2)==1)
- r=(r*x)%n;
- x=(x*x)%n;
- y=y/2;
- }
- return(r);
- }
- int main(){
- int p,q;
- char msg[100];
- cout << "Enter 2 prime numbers\n";
- cin >> p >> q;
- int n = p*q, z = (p-1)*(q-1);
- cout << "Choose any prime number e lower than " << z << endl;
- int e;
- cin >> e;
- int d = 1;
- while((e*d)%z!=1){
- d++;
- }
- printf("\n public key (%d,%d)",e,n);
- printf("\n private key (%d,%d)",d,n);
- printf("\nEnter Message\n");
- cin >> msg;
- cout << "\nplaintext \n";
- int plain[100],cipher[100],plain2[100];
- for(int i=0; i<strlen(msg); i++){
- plain[i]=msg[i];
- printf("%d\t%c\n",plain[i],plain[i]);
- }
- cout << "\nCiphertext after Encryption\n";
- for(int i=0; i<strlen(msg); i++){
- cipher[i]=expmod(plain[i],e,n);
- printf("%d\t%c\n",cipher[i],cipher[i]);
- }
- cout << "\nplaintext after Decryption\n";
- for(int i=0; i<strlen(msg); i++){
- plain2[i]=expmod(cipher[i],d,n);
- printf("%d\t%c\n",plain2[i],plain2[i]);
- }
- }
- /*
- OUTPUT
- Enter 2 prime numbers
- 17 23
- Choose any prime number e lower than 352
- 3
- public key (3,391)
- private key (235,391)
- Enter Message
- LOVE
- plaintext
- 76 L
- 79 O
- 86 V
- 69 E
- Ciphertext after Encryption
- 274
- 379 {
- 290 "
- 69 E
- plaintext after Decryption
- 76 L
- 79 O
- 86 V
- 69 E
- */
Add Comment
Please, Sign In to add comment