Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For lab 3, write a program to decode a message using public key cryptography based on the RSA cipher. You can use the code you

For lab 3, write a program to decode a message using public key cryptography based on the RSA cipher. You can use the code you wrote last week to calculate the residues.

The input will be a list of 1, 2 or 3 digit numbers with spaces in between Each number will correspond to a letter. Words will be separated by a code for a "space" The list may contain up to 100 numbers.

Your program should prompt the user for the name of a file which will contain the encrypted message.

The output of this program will be the decrypted message. Decrypt the message using formula 8.4.9 on page 491. The parameters are provided below in the file "More Info on Lab 3".

In the decoded message 1 will correspond to A, 2 to B, 3 to C etc. I will use the number 27 to correspond to a space between words.

The parameters for lab 3 are:

pq = 323

e =35

d = 107

Here is formula 8.4.9

https://i.gyazo.com/7dca26c3da61c4f91650b29a1f02c75a.png

Here is formula 8.4.10 (Used in decrypting)

https://i.gyazo.com/1b06aee65c851839a7ef06f8bf04b676.png

This lab is a continuation of the previous lab (lab 2). Here is the sorce code for lab 2.

Here is what lab 2 is doing: https://i.gyazo.com/ee936213169a369a7400ecdf627eb954.png

#include #include #include #include #include

using namespace std;

string binary(long long num); long long getModPower(long long a,string k,long long n);

int main() {

/* code */

for(int i=0;i<4;i++)

{

long long a,k,n;

cout<<"enter a: ";

cin>>a;

cout<<"enter k: ";

cin>>k;

cout<<"enter n: ";

cin>>n;

//take binary of k

string binary_of_power=binary(k);

//cout<

long long mod=getModPower(a,binary_of_power,n);

cout<<"ans is "<

}

return 0;

}

string binary(long long num)

{

string s="";

while(num>0)

{

s+=to_string(num%2);

num= num/2;

}

reverse(s.begin(),s.end());

return s;

}

long long getModPower(long long a,string k,long long n)

{

long long mod=1;

//maximum power of 2 will be length(k)-first_position_of_1_in_String_k

//position staring from 1

//first pos of 1 in k

int first_one=0;

for(int i=0;i

{

if(k[i]=='1')

{

first_one=i+1;

break;

}

}

int max_pow_of_two=k.size()-first_one;

//now find power and mod

//lower power to higher power

//for a^(2^0) mod n=a^1 mod n = a mod n = l1

// for a^(2^1) mod n=a^2 mod n = l1^2 mod n = l2

// for a^(2^2) mod n=(a^2)^2 mod n=l2^2 mod n = l3

//and so on

//here caculating power

vector a_pow_pow_of_two(max_pow_of_two+1,1);

long long power=a%n;

a_pow_pow_of_two[0]=power;

for(int i=1;i<=max_pow_of_two;i++)

{

a_pow_pow_of_two[i]=(a_pow_pow_of_two[i-1]*a_pow_pow_of_two[i-1])%n;

}

//after that take only those power of 2 which contains 1 in binary representation of k and multiply all the values and take mod

//final calculation is here and using mod property

//start from msb side

for(int i=0;i

{

if(k[i]=='1')

{

//take only those valuse who takes part in power calculations

mod=(mod*a_pow_pow_of_two[k.size()-i-1])%n;

}

}

return mod;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions