Sie sind auf Seite 1von 6

1. Write a program to Generate primitive roots of Multiplicative Group Z29*.

#include<bits/stdc++.h>
using namespace std;
int main(){
int z=29;
cin>>z;
int ar[z];
int full=(z*(z-1))/2,a=1,s;
for(int i=1;i<z;i++){
a=1;
s=0;
for(int j=0;j<z;j++) ar[j]=0;
for(int k=1;k<z;k++){
a=(a*i)%z;
ar[a]=1;
}
if(ar[0]==0){
for(int j=1;j<z;j++) s+=ar[j];
if(s==z-1) cout<<i<<endl;
}
}
return 0;
}
2. In Multiplicative group < Z421*,x >
a. Encrypt the Input: “Learning is Studying” (Consider ASCII Values) with Elgamal
CryptoSystem.
b. Decrypt the above generated Cipher.

#include<bits/stdc++.h>
using namespace std;

int power(int a,int b,int prime)


{
int res=1;
for(int i=0;i<b;i++)
res=(res%prime*a%prime)%prime;
return res;
}

int gcdextended(int a,int b,int *x,int *y)


{
if(a==0)
{
*x=0;
*y=1;
return b;
}
int x1,y1;
int g=gcdextended(b%a,a,&x1,&y1);
*x=y1-(b/a)*x1;
*y=x1;
return g;
}

int mod_inv(int n,int mod)


{
int x,y;
int g=gcdextended(n,mod,&x,&y);
if(g!=1)
cout<<"Inverse doesn't exist"<<endl;
else
{
int res=(x%mod+mod)%mod;
return res;
}
}

void getprimitiveroots(int prime,vector<int> &a)


{
for(int r=1;r<prime;r++)
{
bool isprimitive=true;
map<int,bool> m;
for(int x=0;x<prime-1;x++)
{
int t=power(r,x,prime);
if(m.find(t)!=m.end())
{
isprimitive=false;
break;
}
m[t]=true;
}
if(isprimitive)
a.push_back(r);
m.clear();
}
}

vector<int> encryption(string text,int e1,int d,int prime,int &c1)


{
int r=rand()%prime;
int e2=power(e1,d,prime);
c1=power(e1,r,prime);

vector<int> c2;

for(int i=0;i<text.length();i++)
{
int temp=(power(e2,r,prime)*text[i])%prime;
c2.push_back(temp);
}
return c2;
}

string decryption(vector<int> ans,int d,int c1,int prime)


{
string temp="";
for(int i=0;i<ans.size();i++)
{
int t=(ans[i]*mod_inv(power(c1,d,prime),prime))%prime;
temp.push_back(char(t));
}
return temp;
}

int main()
{
cout<<"Input"<<endl;
int prime;
cin>>prime;

string text;
cin>>text;

vector<int> rootset;
getprimitiveroots(prime,rootset);
int n=rootset.size();

srand (time(NULL));
int e1=rootset[rand()%n];
int d=rootset[rand()%n];
int c1;
vector<int> ans=encryption(text,e1,d,prime,c1);

cout<<"Encrypted Text"<<endl;
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<" ";
cout<<endl;
string decrypted=decryption(ans,d,c1,prime);

cout<<"Decrypted Text"<<endl;
//cout<<ans<<endl;
cout<<decrypted<<endl;
}
3. Given the super increasing tuple b = [7, 11, 23, 43, 87, 173, 357], r = 41, and modulus n =
1001, write a program to encrypt and decrypt the letter “a” using knapsack cryptosystem. Use [7 6 5
1 2 3 4] as the permutation table.

#include<bits/stdc++.h>
using namespace std;

int tobin(int n, int tup[]){


int i = 6;
while(n>0){
int j = n%2;
tup[i--] = j;
n/=2;
}
}
int main(){
int m;
cout<<"No. of terms: ";
cin>>m;
cout<<"Enter items: ";
int b[m];
for(int i=0;i<m;i++)
cin>>b[i];
int r, n;
cout<<"Enter r and n";
cin>>r>>n;
int t[m];
for(int i=0;i<m;i++){
t[i] = (b[i]*r)%n;
}
int a[m];
int perm[m];
cout<<"Enter permutation matrix: ";
for(int i=0;i<m;i++) cin>>perm[i];
for(int i=0;i<m;i++){
a[i] = t[perm[i]-1];
}
char c;
cout<<"Enter the message to encrypt: ";
cin>>c;
int tup[7];
tobin((int)(c), tup);
for(int i=0;i<7;i++)
cout<<tup[i];
cout<<endl;
int sum = 0;
for(int i=0;i<m;i++)
sum+=(tup[i]*a[i]);
cout<<sum<<endl;
int rinv;
for(int i=1;i<n;i++){
if((r*i)%n == 1) rinv = i;
}
int sdash = (sum*rinv)%n;
cout<<sdash<<endl;

for(int i=6;i>=0;i--){
if(sdash>=b[i]){
tup[i] = 1;
sdash = sdash-b[i];
}
else tup[i] = 0;
}
for(int i=0;i<7;i++)
cout<<tup[i];
cout<<endl;
int a1[7];
for(int i=0;i<m;i++){
a1[i] = tup[perm[i]-1];
}
for(int i=0;i<7;i++)
cout<<a1[i];
cout<<endl;
int ascii = 0;
int j=0;
for(int i=6;i>=0;i--){
ascii += a1[i]*pow(2,j);
j++;
}
cout<<ascii<<endl;
cout<<(char)(ascii-97+'a')<<endl;
}

Das könnte Ihnen auch gefallen