Professional Documents
Culture Documents
NTC File
NTC File
NTC File
INDEX
S.No. Experiment
#include<bits/stdc++.h>
using namespace std;
int main() {
int value, mod;
cout << "Enter value and mod"<<endl;
cin >> value >> mod;
if(isGcd(mod , value))
{
cout << "The multiplicative inverse of " << value << ": " <<
inverse(value, mod)<<endl;
}
else
{
cout << "No multiplicative inverse!"<<endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define N 4
int r1=n;
int r2=b;
while(r2>0){
int q=r1/r2;
int r=r1-q*r2;
int t=t1-q*t2;
r1=r2;
r2=r;
t1=t2;
t2=t;
}
if(r1==1){
t1=t1<0?t1+n:t1;
return t1;
}
return -1;
}
void getCofactor(int A[N][N], int temp[N][N], int p, int q, int n)
{
int i = 0, j = 0;
if (j == n - 1)
{
j = 0;
i++;
}
}
}
}
}
int temp[N][N];
int sign = 1;
sign = -sign;
}
return D;
}
int detInverse=findInverseMod(det,r);
int adj[N][N];
adjoint(A, adj);
return true;
}
int main()
{
int arr[N][N] = { {3, -8, 2, 1},
{1, -5, 2, 4},
{6, 0, 2, 1},
{3, -2, -3, 2}};
int inv[N][N];
return 0;
}
IMPLEMENTATION OF CIPHERS
Shift Cipher
#include<bits/stdc++.h>
using namespace std;
int main()
{
unordered_map<char,int> map;
for(int i=0;i<26;i++)
map[char(97+i)]=i;
string plaintext;
int n;
int k;
string cipher;
for(int i=0;i<plaintext.size();i++)
{
int C=(map[plaintext[i]]+k)%26;
cipher+=char(C+97);
}
cout<<"\n Ciphertext: "<<cipher;
return(0);
}
Multiplicative Cipher
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<char,int> map;
for(int i=0;i<26;i++)
map[char(97+i)]=i;
string plain;
int n;
int k;
string cipher;
for(int i=0;i<plain.size();i++)
{
int C=(map[plain[i]]*k)%26;
cipher+=char(C+97);
}
#include<bits/stdc++.h>
using namespace std;
main()
{
map<char,int> map;
for(int i=0;i<26;i++)
map[char(97+i)]=i;
string plain;
int n;
int k1,k2;
string cipher;
for(int i=0;i<plain.size();i++)
{
int t = ((map[plain[i]]*k1)+k2)%26;
cipher += 'a'+t;
}
cout<<"\n Ciphertext: "<<cipher;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
char key[5][5]=
{
{'a','v','e','n','g'},
{'r','s','b','c','d'},
{'f','h','i','k','l'},
{'m','o','p','q','t'},
{'u','w','x','y','z'}
};
unordered_map<char, pair<int,int>> map;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
map[key[i][j]] = make_pair(i,j);
}
string plain,temp,cipher;
cout<<"\n Enter Plain Text: ";
cin>>plain;
for(int i=0;i<plain.size();i++)
{
if(plain[i]=='j') plain[i]='i';
temp+=plain[i];
}
if((temp.size())%2 != 0) temp+='x';
for(int t=0;t<temp.size()-1;t+=2)
{
int x1= map[temp[t]].first;
int y1= map[temp[t]].second;
int x2= map[temp[t+1]].first;
int y2= map[temp[t+1]].second;
if(x1==x2)
{
cipher+=key[x1][(y1+1)%5];
cipher+=key[x1][(y2+1)%5];
}
else if(y1==y2)
{
cipher+=key[(x1+1)%5][y1];
cipher+=key[(x2+1)%5][y1];
}
else
{
cipher+=key[x1][y2];
cipher+=key[x2][y1];
}
}
cout<<"\n converted Ciphertext is: "<<cipher;
return(0);
}
Hill Cipher
#include<bits/stdc++.h>
using namespace std;
void getKeyMatrix()
{
int i, j;
char mes[3];
cout<<"Enter 3x3 matrix for key: "<<endl;
void encrypt()
{
int i, j, k;
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
encrymatrix[i][j] = encrymatrix[i][j] + a[i][k] *
msg[k][j];
int main()
{
getKeyMatrix();
encrypt();
return(0);
}
Vignere Cipher
#include<bits/stdc++.h>
using namespace std;
int main()
{
unordered_map<char,int> map;
for(int i=0;i<26;i++)
{
map[i+'a'] = i;
}
int i=0,j=0;
while(i!=plain.size())
{
if(j==key.size()) j=0;
#include <bits/stdc++.h>
using namespace std;
int main() {
string text = "avengers assemble", cipher;
int key = 2;
cout << "Plain text: " << text<<endl;
cout << "Key: " << key<<endl;
cipher = encryptRailFence(text, key);
cout <<"\nCipher text: " << cipher << endl;
cout <<"Deciphered text: " << decryptRailFence(cipher,key)
<< endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
string hex2bin(string s) {
unordered_map<char, string> map;
map['0'] = "0000";
map['1'] = "0001";
map['2'] = "0010";
map['3'] = "0011";
map['4'] = "0100";
map['5'] = "0101";
map['6'] = "0110";
map['7'] = "0111";
map['8'] = "1000";
map['9'] = "1001";
map['A'] = "1010";
map['B'] = "1011";
map['C'] = "1100";
map['D'] = "1101";
map['E'] = "1110";
map['F'] = "1111";
string bin = "";
for (int i = 0; i < s.size(); i++) {
bin += map[s[i]];
}
return bin;
}
string bin2hex(string s) {
unordered_map<string, string> map;
map["0000"] = "0";
map["0001"] = "1";
map["0010"] = "2";
map["0011"] = "3";
map["0100"] = "4";
map["0101"] = "5";
map["0110"] = "6";
map["0111"] = "7";
map["1000"] = "8";
map["1001"] = "9";
map["1010"] = "A";
map["1011"] = "B";
map["1100"] = "C";
map["1101"] = "D";
map["1110"] = "E";
map["1111"] = "F";
string hex = "";
for (int i = 0; i < s.length(); i += 4) {
string ch = "";
ch += s[i];
ch += s[i + 1];
ch += s[i + 2];
ch += s[i + 3];
hex += map[ch];
}
return hex;
}
int main() {
string pt, key;
pt = "123456ABCD132536";
key = "AABB09182736CCDD";
key = hex2bin(key);
int keyp[56] = { 57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4 };
key = permute(key, keyp, 56);
int shift_table[16] = { 1, 1, 2, 2,
2, 2, 2, 2,
1, 2, 2, 2,
2, 2, 2, 1 };
#include <bits/stdc++.h>
using namespace std;
#define Nb 4
int Nr = 0;
int Nk = 0;
int Rcon[255] = {
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d,
0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91,
};
void KeyExpansion()
{
int i, j;
unsigned char temp[4], k;
temp[0] = getSBoxValue(temp[0]);
temp[1] = getSBoxValue(temp[1]);
temp[2] = getSBoxValue(temp[2]);
temp[3] = getSBoxValue(temp[3]);
void SubBytes()
{
int i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < Nb; j++)
{
state[i][j] = getSBoxValue(state[i][j]);
}
}
}
void ShiftRows()
{
unsigned char temp;
temp = state[1][0];
state[1][0] = state[1][1];
state[1][1] = state[1][2];
state[1][2] = state[1][3];
state[1][3] = temp;
temp = state[2][0];
state[2][0] = state[2][2];
state[2][2] = temp;
temp = state[2][1];
state[2][1] = state[2][3];
state[2][3] = temp;
temp = state[3][0];
state[3][0] = state[3][3];
state[3][3] = state[3][2];
state[3][2] = state[3][1];
state[3][1] = temp;
}
void MixColumns()
{
int i;
unsigned char Tmp, Tm, t;
for (i = 0; i < Nb; i++)
{
t = state[0][i];
Tmp = state[0][i] ^ state[1][i] ^ state[2][i] ^ state[3][i];
Tm = state[0][i] ^ state[1][i];
Tm = xtime(Tm);
state[0][i] ^= Tm ^ Tmp;
Tm = state[1][i] ^ state[2][i];
Tm = xtime(Tm);
state[1][i] ^= Tm ^ Tmp;
Tm = state[2][i] ^ state[3][i];
Tm = xtime(Tm);
state[2][i] ^= Tm ^ Tmp;
Tm = state[3][i] ^ t;
Tm = xtime(Tm);
state[3][i] ^= Tm ^ Tmp;
}
}
void Cipher()
{
int i, j, round = 0;
AddRoundKey(0);
SubBytes();
ShiftRows();
AddRoundKey(Nr);
if (argc != 2)
{
cerr << "Usage: " << argv[0] << " <keysize: 1=128, 2=192,
3=256>\n";
exit(0);
}
switch (atoi(argv[1]))
{
case 1:
Nk = 4;
break;
case 2:
Nk = 6;
break;
case 3:
Nk = 8;
break;
default:
Nk = 4;
break;
}
Nr = Nk + 6;
Key[0] = 0x2b;
Key[1] = 0x7e;
Key[2] = 0x15;
Key[3] = 0x16;
Key[4] = 0x28;
Key[5] = 0xae;
Key[6] = 0xd2;
Key[7] = 0xa6;
Key[8] = 0xab;
Key[9] = 0xf7;
Key[10] = 0x15;
Key[11] = 0x88;
Key[12] = 0x09;
Key[13] = 0xcf;
Key[14] = 0x4f;
Key[15] = 0x3c;
char str[1024];
fgets(str, 1024, stdin);
KeyExpansion();
int sz = 0;
Cipher();
for (i = 0; i < Nb * 4; i++)
cout << (int)out[i] << " ";
}
printf("\n\n");
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
double p = 3;
double q = 7;
double n=p*q;
double count;
double totient = (p-1)*(q-1);
double e=2;
while(e<totient)
{
count = gcd(e,totient);
if(count==1)
break;
else
e++;
}
double d;
double k = 2;
d = (1 + (k*totient))/e;
double msg = 12;
double c = pow(msg,e);
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);
import random
from math import pow
a = random.randint(2, 10)
def gen_key(q):
return key
def power(a, b, c):
x=1
y=a
while b > 0:
if b % 2 != 0:
x = (x * y) % c;
y = (y * y) % c
b = int(b / 2)
return x % c
en_msg = []
k = gen_key(q)
s = power(h, k, q)
p = power(g, k, q)
return en_msg, p
dr_msg = []
h = power(p, key, q)
for i in range(0, len(en_msg)):
dr_msg.append(chr(int(en_msg[i]/h)))
return dr_msg
def main():
key = gen_key(q)
h = power(g, key, q)
print("g used : ", g)
print("g^a used : ", h)
en_msg, p = encrypt(msg, q, h, g)
dr_msg = decrypt(en_msg, p, key, q)
dmsg = ''.join(dr_msg)
print("Decrypted Message :", dmsg);
if __name__ == '__main__':
main()
Implementation of Rabin Algorithm
#include <bits/stdc++.h>
using namespace std;
int p = 151, q = 43;
int n = p * q;
int main()
{
vector<int>e;
vector<int>d;
if n == 0:
return m
else:
r=m%n
return euclid(n, r)
r1 = a
r2 = b
s1 = int(1)
s2 = int(0)
t1 = int(0)
t2 = int(1)
while r2 > 0:
q = r1//r2
r = r1-q * r2
r1 = r2
r2 = r
s = s1-q * s2
s1 = s2
s2 = s
t = t1-q * t2
t1 = t2
t2 = t
if t1 < 0:
t1 = t1 % a
p = 823
q = 953
n=p*q
Pn = (p-1)*(q-1)
key = []
gcd = euclid(Pn, i)
if gcd == 1:
key.append(i)
e = int(313)
r, d = exteuclid(Pn, e)
if r == 1:
d = int(d)
print("decryption key is: ", d)
else:
print("Multiplicative inverse does not exist.")
M = 19070
S = (M**d) % n
M1 = (S**e) % n
if M == M1:
print("Accept the message sent by Alice")
else:
print("Do not accept the message sent by Alice")