Crypto Lab Solution
Crypto Lab Solution
Source Code :
#include <iostream>
using namespace std;
else
result += char(int(text[i]+s-97)%26 +97);
}
return result;
}
int main()
{
string text="PRABINKARKI";
int s = 4;
cout << "Text : " << text;
cout << "\nShift: " << s;
cout << "\nCipher: " << encrypt(text, s);
return 0;
}
Output :
Prabin Karki
2
Source Code :
#include<bits/stdc++.h>
using namespace std;
x += 'A';
cipher_text.push_back(x);
}
return cipher_text;
}
Prabin Karki
3
x += 'A';
orig_text.push_back(x);
}
return orig_text;
}
int main()
{
string str = "PRABINKARKI";
string keyword = "SIMPLE";
Output :
Prabin Karki
4
Source Code :
import string
dict1 = {}
key = 4
all_letters= string.ascii_letters
for i in range(len(all_letters)):
dict1[all_letters[i]] = all_letters[(i+key)%len(all_letters)]
cipher_txt= "".join(cipher_txt)
print("Cipher Text is: ",cipher_txt)
dict2 = {}
for i in range(len(all_letters)):
dict2[all_letters[i]] = all_letters[(i-key)%(len(all_letters))]
decrypt_txt = []
Prabin Karki
5
else:
temp = char
decrypt_txt.append(temp)
decrypt_txt = "".join(decrypt_txt)
print("Recovered plain text :", decrypt_txt)
Output :
Prabin Karki
6
Source Code :
#include <bits/stdc++.h>
using namespace std;
rail[row][col++] = text[i];
dir_down?row++ : row--;
}
return result;
}
Prabin Karki
7
bool dir_down;
rail[row][col++] = '*';
dir_down?row++ : row--;
}
int index = 0;
for (int i=0; i<key; i++)
for (int j=0; j<cipher.length(); j++)
if (rail[i][j] == '*' && index<cipher.length())
rail[i][j] = cipher[index++];
string result;
row = 0, col = 0;
for (int i=0; i< cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;
if (rail[row][col] != '*')
result.push_back(rail[row][col++]);
dir_down?row++: row--;
}
return result;
}
Prabin Karki
8
int main()
{
cout << encryptRailFence("attack at once", 2) << endl;
cout << encryptRailFence("Prabinkarki ", 3) << endl;
cout << encryptRailFence("defend the east wall", 3) << endl;
return 0;
}
Output :
Prabin Karki
9
Source Code :
#include <iostream>
using namespace std;
Prabin Karki
10
int messageVector[3][1];
int cipherMatrix[3][1];
string CipherText;
HillCipher(message, key);
return 0;
}
Output :
Prabin Karki
11
Source Code :
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 10, b = 15;
cout << "GCD(" << a << ", "
<< b << ") = " << gcd(a, b)
<< endl;
a = 35, b = 10;
cout << "GCD(" << a << ", "
<< b << ") = " << gcd(a, b)
<< endl;
a = 31, b = 2;
cout << "GCD(" << a << ", "
<< b << ") = " << gcd(a, b)
<< endl;
return 0;
}
Output :
Prabin Karki
12
Source Code :
#include <iostream>
using namespace std;
int main()
{
int a = 3, m = 11;
Output :
Prabin Karki
13
Source Code :
#include <bits/stdc++.h>
using namespace std;
int gcdExtended(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtended(b%a, a, &x1, &y1);
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
int main() {
int x, y;
int a = 35, b = 15;
cout<<"gcd "<<gcdExtended(a, b, &x, &y);
return 0;
}
Output :
Prabin Karki
14
Source Code :
// C program to check if a
// number is prime
#include <stdio.h>
#include<math.h>
int main()
{
int n, i, flag = 1;
scanf("%d", &n);
if (n % i == 0) {
flag = 0;
break;
}
}
if(n<=1)
flag=0;
else if(n==2)
flag=1;
if (flag == 1) {
printf("%d is a prime number", n);
}
else {
printf("%d is not a prime number", n);
}
return 0;
}
Prabin Karki
15
Output :
Prabin Karki
16
Source Code :
#include <bits/stdc++.h>
using namespace std;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
if (x == 1 || x == n-1)
return true;
while (d != n-1)
{
x = (x * x) % n;
d *= 2;
if (x == 1) return false;
if (x == n-1) return true;
}
Prabin Karki
17
return false;
}
int d = n - 1;
while (d % 2 == 0)
d /= 2;
return true;
}
int main()
{
int k = 4;
return 0;
}
Output :
Prabin Karki
18
Source Code :
#include <iostream>
using namespace std;
int main()
{
int n;
for (n = 1; n <= 10; n++)
cout << "phi("<<n<<") = " << phi(n) << endl;
return 0;
}
Prabin Karki
19
Output :
Prabin Karki