0% found this document useful (0 votes)
11 views

cnslabtest1

Uploaded by

Chandan To
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

cnslabtest1

Uploaded by

Chandan To
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

#include<stdio.

h>
#include<ctype.h>
void main()
{
char text[500], ch;
int key;
printf("Enter a plain text: ");
scanf("%s", text);
printf("Enter the key: ");
scanf("%d", & key);
for (int i = 0; text[i] != '\0'; ++i) {
ch = text[i];
if (isalnum(ch))
{
if (islower(ch))
ch = (ch - 'a' + key) % 26 + 'a';
if (isupper(ch))
ch = (ch - 'A' + key) % 26 + 'A';
}
text[i] = ch;
}
printf("Encrypted message: %s", text);
for (int i = 0; text[i] != '\0'; ++i){
ch = text[i];
if (isalnum(ch))
{
if (islower(ch))
ch = (ch - 'a' - key + 26) % 26 + 'a';
if (isupper(ch))
ch = (ch - 'A' - key + 26) % 26 + 'A';
}
text[i] = ch;
}
printf("\nDecrypted message: %s", text);
}
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char pt[26] =
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','
Z'};
char ct[26] =
{'Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','
A'};
char p[20] = {'\0'},c[20] = {'\0'},r[20] = {'\0'};
int i,j;
printf("Enter the plain text: ");
gets(p);
for(i=0;i<strlen(p);i++)
for(j=0;j<26;j++)
if(pt[j] == p[i])
c[i] = ct[j];
printf("\nCipher text: %s",c);
for(i=0;i<strlen(c);i++)
for(j=0;j<26;j++)
if(ct[j] == c[i])
r[i] = pt[j];
printf("\nPlain text: %s\n",r);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
void generateKeyTable(char key[], int ks, char keyT[5][5]) {
int dicty[26] = {0}, i = 0, j = 0;
dicty['j' - 'a'] = 1;
for (int k = 0; k < ks; k++) {
if (dicty[key[k] - 'a'] == 0 && key[k] != 'j') {
dicty[key[k] - 'a'] = 1;
keyT[i][j++] = key[k];
if (j == 5) i++, j = 0;
}
}
for (int k = 0; k < 26; k++) {
if (dicty[k] == 0) {
keyT[i][j++] = (char)(k + 'a');
if (j == 5) i++, j = 0;
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%c ",keyT[i][j]);
}
printf("\n");
}
}
int prepare(char str[], int len) {
for (int i = 0; i < len; i += 2) {
if (str[i] == str[i+1]) {
for (int j = len; j > i + 1; j--)
str[j] = str[j - 1];
str[i + 1] = 'x';
len++;
}
}
if (len % 2 != 0) str[len++] = 'x';
str[len] = '\0';
return len;
}
void search(char keyT[5][5], char a, char b, int arr[]) {
if (a == 'j') a = 'i';
if (b == 'j') b = 'i';
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
if (keyT[i][j] == a) arr[0] = i, arr[1] = j;
if (keyT[i][j] == b) arr[2] = i, arr[3] = j;
}
}
void encrypt(char str[], char keyT[5][5], int len) {
int pos[4];
for (int i = 0; i < len; i += 2) {
search(keyT, str[i], str[i + 1], pos);
if (pos[0] == pos[2]) {
str[i] = keyT[pos[0]][(pos[1] + 1) % 5];
str[i + 1] = keyT[pos[0]][(pos[3] + 1) % 5];
}
else if (pos[1] == pos[3]) {
str[i] = keyT[(pos[0] + 1) % 5][pos[1]];
str[i + 1] = keyT[(pos[2] + 1) % 5][pos[1]];
}
else {
str[i] = keyT[pos[0]][pos[3]];
str[i + 1] = keyT[pos[2]][pos[1]];
}
}
}
void PlayfairCrypt(char str[], char key[]) {
char keyT[5][5];
int ks = strlen(key);
int ps = strlen(str);
ps = prepare(str, ps);
generateKeyTable(key, ks, keyT);
encrypt(str, keyT, ps);
printf("\nCipher text: %s\n", str);
}
void main() {
char str[SIZE], key[SIZE];
printf("Enter the key: ");
scanf("%s", key);
printf("Enter the plaintext: ");
scanf("%s", str);
PlayfairCrypt(str, key);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void encryption(int msg[100][100], int key[3][3], int cipher_mat[100][100], int
len1, int order);
void decryption(int cipher_mat[100][100], int key[3][3], int len1, int order);
void main() {
char message[100];
int key[3][3], msg[100][100] = {0}, cipher_mat[100][100] = {0}, order, len,
len1, count = 0;
printf("Enter the message:\n");
gets(message);
printf("Enter the order of the key:\t");
scanf("%d", &order);
printf("Enter the key:\n");
for (int i = 0; i < order; i++)
for (int j = 0; j < order; j++)
scanf("%d", &key[i][j]);
len = strlen(message);
len1 = (len + order - 1) / order;
for (int i = 0; i < len1; i++)
for (int j = 0; j < order; j++)
msg[i][j] = (count < len) ? (message[count++] - 'a') : ('x' - 'a');
printf("Encryption is:");
encryption(msg, key, cipher_mat, len1, order);
decryption(cipher_mat, key, len1, order);
}
void encryption(int msg[100][100], int key[3][3], int cipher_mat[100][100], int
len1, int order) {
char encrypt[100];
int u = 0;
for (int i = 0; i < len1; i++)
for (int j = 0; j < order; j++) {
for (int k = 0; k < order; k++)
cipher_mat[i][j] += msg[i][k] * key[k][j];
cipher_mat[i][j] %= 26;
encrypt[u++] = cipher_mat[i][j] + 'a';
printf("%c", encrypt[u - 1]);
}
encrypt[u] = '\0';
}
void decryption(int cipher_mat[100][100], int key[3][3], int len1, int order) {
int det, inv_det, adj[3][3], inv[3][3], decrypt_mat[100][100] = {0};
char decrypt[100];
int u = 0;
det = key[0][0] * key[1][1] - key[0][1] * key[1][0];
printf("\nDeterminant is : %d", det);
if (det < 0) det = 26 - (-det % 26);
for (inv_det = 1; (det * inv_det) % 26 != 1; inv_det++);
printf("\nInverse of determinant is : %d\n", inv_det);
adj[0][0] = key[1][1];
adj[0][1] = -key[0][1];
adj[1][0] = -key[1][0];
adj[1][1] = key[0][0];
for (int i = 0; i < order; i++)
for (int j = 0; j < order; j++) {
inv[i][j] = (adj[i][j] * inv_det) % 26;
if (inv[i][j] < 0) inv[i][j] += 26;
}
printf("adjoint of 2x2\n");
for (int i = 0; i < order; i++) {
for (int j = 0; j < order; j++) printf("%d\t", adj[i][j]);
printf("\n");
}
printf("Inverse Key:\n");
for (int i = 0; i < order; i++) {
for (int j = 0; j < order; j++) printf("%d\t", inv[i][j]);
printf("\n");
}
for (int i = 0; i < len1; i++)
for (int j = 0; j < order; j++) {
for (int k = 0; k < order; k++)
decrypt_mat[i][j] += cipher_mat[i][k] * inv[k][j];
decrypt_mat[i][j] %= 26;
decrypt[u++] = decrypt_mat[i][j] + 'a';
printf("%c", decrypt[u - 1]);
}
}
#include<stdio.h>
#include<string.h>
void main() {
int i, j, len, rails, count, direction, row;
char str[1000], code[100][1000] = {0};
printf("Enter the secret msg: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0;
len = strlen(str);
printf("Enter the number of rails: ");
scanf("%d", &rails);
count = 0;
row = 0;
direction = 1;
for (j = 0; j < len; j++) {
code[row][j] = str[j];
row += direction;
if (row == 0 || row == rails - 1) {
direction *= -1;
}
}
printf("Cipher: ");
for (i = 0; i < rails; i++) {
for (j = 0; j < len; j++) {
if (code[i][j] != 0) {
printf("%c", code[i][j]);
}
}
}
printf("\n");
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long long int modulo(int base, int pw, int mod) {
long long int i,a=1;
for(i=0;i<pw;i++)
a=a*base;
return a%mod;
}
int Miller(int p) {
long long int c=0,m,b,temp,a=2,k=0;
if (p < 2) {
return 0;
}
if ((p != 2) && (p % 2 == 0)) {
return 0;
}
m = p - 1;
while ((m % 2) == 0) {
m /= 2;
k++;
}
b=modulo(a,m,p);
if(b==1)
return 1;
while(b!=1&&b!=p-1&&k>0){
b=modulo(b,2,p);
if(b==0)
return 0;
k--;
}
if(b==p-1)
return 1;
else
return 0;
}
int main() {
int num;
printf("Enter integer to test primality: ");
scanf("%d", & num);
if (Miller(num))
printf("\n%d is prime\n", num);
else
printf("\n%d is not prime\n", num);
return 0;
}

You might also like