Question

In C++ or C ONLY PLEASE 1.thefunctionsforRSA: (a)Mappingfunction from todecimaldigits(SeetextbFigure9.7.(b)andformapping).willtoausedinRSAencryptionanddecryption. (b)Keythattheinputsofprimeand,andgeneratetheparameters(n, ?(n), e, d),andtheforRSA.Thesub-r

In C++ or C ONLY PLEASE

1.thefunctionsforRSA:

(a)Mappingfunction from todecimaldigits(SeetextbFigure9.7.(b)andformapping).willtoausedinRSAencryptionanddecryption.

(b)Keythattheinputsofprimeand,andgeneratetheparameters(n, ?(n), e, d),andtheforRSA.Thesub-routines

GCD function to find e: gcd(?(n), e) = 1; 1 < e < ?(n). Since there could be many such e values, you may use gcd to find the first five such e values.

Multiplicative inverse: d = e?1(mod ?(n)). You may use extended Euclidean algorithm to find the first five such d values corresponding to each of five e values (Note: Your multiplicative inverse value should be within [0, ?(n))).

(c)Encryptionfunctionthatofandpublic

(d)functionthatofciphertextand

(e)functionfromdecimaldigitsto

2.Experimentthecorrectnessofprogramusingtheplaintextandprimep =73andq =151:

(a)text:

(b)plaintext:

For each test of your RSA algorithm, you need to output the following information:

(a)RSAinformation:n, ?(n),thefirste,andd

(b)plaintext

(c)setsof:

Public key

Ciphertext

Private key

Plaintext

(d)fromdecimaldigitstoTheoutputfortheinputofisinFigure1.

In C++ or C ONLY PLEASE 1.thefunctionsforRSA: (a)M

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. /*
    
  2.  * C++ Program to Implement the RSA Algorithm
    
  3.  */
    
  4. #include<iostream>
    
  5. #include<math.h>
    
  6. #include<string.h>
    
  7. #include<stdlib.h>
    
  8.  
    
  9. using namespace std;
    
  10.  
    
  11. long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100], en[100], i;
    
  12. char msg[100];
    
  13. int prime(long int);
    
  14. void ce();
    
  15. long int cd(long int);
    
  16. void encrypt();
    
  17. void decrypt();
    
  18. int prime(long int pr)
    
  19. {
    
  20.     int i;
    
  21.     j = sqrt(pr);
    
  22.     for (i = 2; i <= j; i++)
    
  23.     {
    
  24.         if (pr % i == 0)
    
  25.             return 0;
    
  26.     }
    
  27.     return 1;
    
  28. }
    
  29. int main()
    
  30. {
    
  31.     cout << "\nENTER FIRST PRIME NUMBER\n";
    
  32.     cin >> p;
    
  33.     flag = prime(p);
    
  34.     if (flag == 0)
    
  35.     {
    
  36.         cout << "\nWRONG INPUT\n";
    
  37.         exit(1);
    
  38.     }
    
  39.     cout << "\nENTER ANOTHER PRIME NUMBER\n";
    
  40.     cin >> q;
    
  41.     flag = prime(q);
    
  42.     if (flag == 0 || p == q)
    
  43.     {
    
  44.         cout << "\nWRONG INPUT\n";
    
  45.         exit(1);
    
  46.     }
    
  47.     cout << "\nENTER MESSAGE\n";
    
  48.     fflush(stdin);
    
  49.     cin >> msg;
    
  50.     for (i = 0; msg[i] != NULL; i++)
    
  51.         m[i] = msg[i];
    
  52.     n = p * q;
    
  53.     t = (p - 1) * (q - 1);
    
  54.     ce();
    
  55.     cout << "\nPOSSIBLE VALUES OF e AND d ARE\n";
    
  56.     for (i = 0; i < j - 1; i++)
    
  57.         cout << e[i] << "\t" << d[i] << "\n";
    
  58.     encrypt();
    
  59.     decrypt();
    
  60.     return 0;
    
  61. }
    
  62. void ce()
    
  63. {
    
  64.     int k;
    
  65.     k = 0;
    
  66.     for (i = 2; i < t; i++)
    
  67.     {
    
  68.         if (t % i == 0)
    
  69.             continue;
    
  70.         flag = prime(i);
    
  71.         if (flag == 1 && i != p && i != q)
    
  72.         {
    
  73.             e[k] = i;
    
  74.             flag = cd(e[k]);
    
  75.             if (flag > 0)
    
  76.             {
    
  77.                 d[k] = flag;
    
  78.                 k++;
    
  79.             }
    
  80.             if (k == 99)
    
  81.                 break;
    
  82.         }
    
  83.     }
    
  84. }
    
  85. long int cd(long int x)
    
  86. {
    
  87.     long int k = 1;
    
  88.     while (1)
    
  89.     {
    
  90.         k = k + t;
    
  91.         if (k % x == 0)
    
  92.             return (k / x);
    
  93.     }
    
  94. }
    
  95. void encrypt()
    
  96. {
    
  97.     long int pt, ct, key = e[0], k, len;
    
  98.     i = 0;
    
  99.     len = strlen(msg);
    
  100.     while (i != len)
    
  101.     {
    
  102.         pt = m[i];
    
  103.         pt = pt - 96;
    
  104.         k = 1;
    
  105.         for (j = 0; j < key; j++)
    
  106.         {
    
  107.             k = k * pt;
    
  108.             k = k % n;
    
  109.         }
    
  110.         temp[i] = k;
    
  111.         ct = k + 96;
    
  112.         en[i] = ct;
    
  113.         i++;
    
  114.     }
    
  115.     en[i] = -1;
    
  116.     cout << "\nTHE ENCRYPTED MESSAGE IS\n";
    
  117.     for (i = 0; en[i] != -1; i++)
    
  118.         printf("%c", en[i]);
    
  119. }
    
  120. void decrypt()
    
  121. {
    
  122.     long int pt, ct, key = d[0], k;
    
  123.     i = 0;
    
  124.     while (en[i] != -1)
    
  125.     {
    
  126.         ct = temp[i];
    
  127.         k = 1;
    
  128.         for (j = 0; j < key; j++)
    
  129.         {
    
  130.             k = k * ct;
    
  131.             k = k % n;
    
  132.         }
    
  133.         pt = k + 96;
    
  134.         m[i] = pt;
    
  135.         i++;
    
  136.     }
    
  137.     m[i] = -1;
    
  138.     cout << "\nTHE DECRYPTED MESSAGE IS\n";
    
  139.     for (i = 0; m[i] != -1; i++)
    
  140.         printf("%c", m[i]);
    
  141. }
    
Add a comment
Know the answer?
Add Answer to:
In C++ or C ONLY PLEASE 1.thefunctionsforRSA: (a)Mappingfunction from todecimaldigits(SeetextbFigure9.7.(b)andformapping).willtoausedinRSAencryptionanddecryption. (b)Keythattheinputsofprimeand,andgeneratetheparameters(n, ?(n), e, d),andtheforRSA.Thesub-r
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3) Out of the following, name which kind of attack you carried out in part 1...

    3) Out of the following, name which kind of attack you carried out in part 1 and part2: a. ciphertext only, b. known plaintext, c. chosen plaintext, d. chosen ciphertext. Explain your answer Problem 3 10 points] A 4-bit long message was encrypted using one-time pad to yield a cipher-text “1010” Assuming the message space consists of all 4-bit long messages, what is the probability that the corresponding plaintext was “1001”? Explain your answer. Problem 4 Assume we perform a...

  • Computing RSA by hand. Let p = 13, q = 23, e = 17 be your...

    Computing RSA by hand. Let p = 13, q = 23, e = 17 be your initial parameters. You may use a calculator for this problem, but you should show all intermediate results. Key generation: Compute N and Phi(N). Compute the private key k_p = d = e^-1 mod Phi(N) using the extended Euclidean algorithm. Show all intermediate results. Encryption: Encrypt the message m = 31 by applying the square and multiply algorithm (first, transform the exponent to binary representation)....

  • Use C++ forehand e receiver creates a public key and a secret key as follows. Generate...

    Use C++ forehand e receiver creates a public key and a secret key as follows. Generate two distinct primes, p andq. Since they can be used to generate the secret key, they must be kept hidden. Let n-pg, phi(n) ((p-1)*(q-1) Select an integer e such that gcd(e, (p-100g-1))-1. The public key is the pair (e,n). This should be distributed widely. Compute d such that d-l(mod (p-1)(q-1). This can be done using the pulverizer. The secret key is the pair (d.n)....

  • For the RSA encryption algorithm , do the following (a) Use p=257,q=337(n=pq=86609),b=(p-1)(q-1)=86016. Gcd(E,b)=1, choose E=17, find...

    For the RSA encryption algorithm , do the following (a) Use p=257,q=337(n=pq=86609),b=(p-1)(q-1)=86016. Gcd(E,b)=1, choose E=17, find D, the number which has to be used for decryption, using Extended Euclidean Algorithm (b) One would like to send the simple message represented by 18537. What is the message which will be sent? (c) Decrypt this encrypted message to recover the original message.

  • Crypotography Question 1. A message m was encrypted using the RSA algorithm with n=899 and e=13....

    Crypotography Question 1. A message m was encrypted using the RSA algorithm with n=899 and e=13. The ciphertext is 706. Find the message m. Show all the work from the scratch, including finding 1/e(using the extended Euclidean algorithm) and the resulting modular exponentiation...

  • Problem 4. The plaintext P has been encrypted with RSA n = 65, e = 29...

    Problem 4. The plaintext P has been encrypted with RSA n = 65, e = 29 to yield the ciphertext C = 3 = P29 mod 65. Find P using the decryption key d, and prove the congruence class of P that solves this congruence is unique.

  • Write code for RSA encryption package rsa; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class RSA...

    Write code for RSA encryption package rsa; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class RSA {    private BigInteger phi; private BigInteger e; private BigInteger d; private BigInteger num; public static void main(String[] args) {    Scanner keyboard = new Scanner(System.in); System.out.println("Enter the message you would like to encode, using any ASCII characters: "); String input = keyboard.nextLine(); int[] ASCIIvalues = new int[input.length()]; for (int i = 0; i < input.length(); i++) { ASCIIvalues[i] = input.charAt(i); } String ASCIInumbers...

  • If a public key has the value (e, n)-(13,77) (a) what is the totient of n,...

    If a public key has the value (e, n)-(13,77) (a) what is the totient of n, or (n)? (b) Based on the answer from part (a), what is the value of the private key d? (Hint: Remember that d * e-1 mod (n), and that d < ф(n)) You may use an exhaustive search or the Modified Euclidean Algorithm for this. Show all steps performed. For both (c) and (d), use the Modular Power Algorithm, showing all steps along the...

  • O-8. (15 points) Bob's simple toy RSA eryptosystem has public key kyub(n, e) (65,5), where n =p,-...

    o-8. (15 points) Bob's simple toy RSA eryptosystem has public key kyub(n, e) (65,5), where n =p,-5x13-65 and e-5. I. Describe the key pair generation procedure for Bob to generate his private key kor- d. With the above given parameters, use EEA to calculate d 2. Describe RSA encryption procedure that Alice uses to encrypt her plaintext message x to its above given parameters, what will be y? ciphertext y before sending the message to Bob. Suppose Alice's message x-...

  • Write a program in Python implement the RSA algorithm for cryptography. Set up: 1.Choose two large...

    Write a program in Python implement the RSA algorithm for cryptography. Set up: 1.Choose two large primes, p and q. (There are a number of sites on-line where you can find large primes.) 2.Compute n = p * q, and Φ = (p-1)(q-1). 3.Select an integer e, with 1 < e < Φ , gcd(e, Φ) = 1. 4.Compute the integer d, 1 < d < Φ such that ed ≡ 1 (mod Φ). The numbers e and d are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT