Write a Merkle-Hellman cryptosystem in C++ **Please don\'t just copy/paste some
ID: 3850029 • Letter: W
Question
Write a Merkle-Hellman cryptosystem in C++
**Please don't just copy/paste some code, if you don't know, then don't answer**
Information on cipher can be found here:
https://en.wikipedia.org/wiki/Merkle%E2%80%93Hellman_knapsack_cryptosystem
Instructions for program:
Your program will need to get its input from a file in the following format
First line is an integer saying how many test cases are in the input file
A line containing the plaintext to be encrypted.
A line containing a single integer with the number of items in the sequence
A line containing the entire sequence.
Explanation / Answer
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int gcd( int a, int b)
{
while(a!= b)
{
if(a > b)
m =a - b;
else
b = b -a;
}
return (a); //a or b is GCD
}
void sort(int arr[])
{
int x, j, temp ;
for ( x = 0 ; x <= 7 ; x++ )
{
for ( j = x + 1 ; j <= 8 ; j++ )
{
if ( arr[x] > arr[j] )
{
temp = arr[x] ;
arr[x] = arr[j] ;
arr[j] = temp ;
}
}
}
}
int sum(int m[])
{
int x, sum=0;
for (x=0; x<8; x++)
{
sum = sum + m[i];
}
return(sum);
}
int main()
{
printf(" Initialising the super-increasing sequence ");
int w[8],i,j,qt,q,r;
int b[8],a[8];
int c=0;
for(i=0;i<9;i++)
srand(time(NULL));
for(i=0;i<8;i++)
w[i]=rand()%1000;
qt=sum(w);
q=rand()%10000;
while(q<qt)
{
q=rand()%10000;
}
sort(w);
printf(" The super-increasing sequence is: ");
for(j=0;j<8;j++)
printf("%d ",w[j]);
printf(" The value of sigma W is %d",qt);
printf(" The value of q chosen is %d ",q);
rep:r=rand();
while(gcd(q,r)!=1)
{
r=rand();
}
if(q<r) goto rep;
printf(" The value of r chosen is %d ",r);
for(i=0;i<8;i++)
{
b[i]=(r*w[i])%q;
}
printf(" The public key beta is ");
for(j=0;j<8;j++)
printf("%d ",b[j]);
printf(" Enter the 8-bit ");
for(i=0;i<8;i++)
scanf("%d",&a[i]);
printf(" The Encrypted is ");
for(i=0;i<8;i++)
c+=(a[i]*b[i]);
printf("%d ",c);
scanf("%d");
return 0;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.