CNS Exp 1-4
CNS Exp 1-4
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
int i, j, len, depth, count, code[100][1000];
char str[1000];
printf("Enter the message\n");
gets(str);
len=strlen(str);
printf("Enter the depth\n");
scanf("%d", &depth);
for(i=0; i<depth; i++){
for(j=0; j<len; j++){
code[i][j]=0;
}
}
count = 0;
j = 0;
while(j<len){
if(count%2==0){
for(i =0; i<depth; i++){
code[i][j]=(int)str[j];
j++;
}
}
else{
for(i=depth-2; i>0; i--){
code[i][j]=(int)str[j];
j++;
}
}
count++;
}
for(i=0; i<depth; i++){
for(j=0; j<len; j++){
if(code[i][j]!=0){
printf("%c", code[i][j]);
}
}
}
printf("\n");
return 0;
}
/Program to implement Monoalphabetic Cipher
#include<stdio.h>
char monocipher_encr(char);
char alpha[27][3] = {
{'a', 'b'},{'b', 'c'},{'c', 'd'},{'d', 'e'},{'e', 'f'},{'f', 'g'},{'g', 'h'},{'h', 'i'},{'i', 'j'},
{'j', 'k'},{'k', 'l'},{'l', 'm'},{'m', 'n'},{'n', 'o'},{'o', 'p'},{'p', 'q'},{'q', 'r'},{'r', 's'},
{'s', 't'},{'t', 'u'},{'u', 'v'},{'v', 'w'},{'w', 'x'},{'x', 'y'},{'y', 'z'},{'z', 'a'}};
char str[20];
int main(){
char str[20], str2[20];
int i;
printf("\nEnter String :");
gets(str);
for(i=0; str[i]; i++){
str2[i]=monocipher_encr(str[i]);
}
str2[i]='\0';
printf("\nBefore Decription : %s", str);
printf("\nAfter Decription : %s", str2);
}
char monocipher_encr(char a){
int i;
for(i=0; i<27; i++){
if(a==alpha[i][0]){
break;
}
}
return alpha[i][1];
}
/Program to implement Ceaser Cipher
#include<stdio.h>
#include<ctype.h>
#define MAXSIZE 1204
void encrpyt(char*);
void decrpyt(char*);
int menu();
int main(void){
char c, choice[2], s[MAXSIZE];
while(1){
menu();
gets(choice);
if ((choice[0] == 'e') || (choice[0] == 'E')){
puts("Input text to encrypt->");
gets(s);
encrypt(s);
}else if ((choice[0] == 'd') || (choice[0] == 'D')) {
puts("Input text to decrypt->");
gets(s);
decrypt(s);
}else{
break;}
}
return 0;
}
void encrypt(char*str){
int n=0;
char *p = str, q[MAXSIZE];
while (*p) {
if (islower(*p)) {
if ((*p >= 'a') && (*p < 'x')){
q[n] = toupper(*p + (char) 3);
}else if (*p == 'x'){
q[n] = 'A';
}else if (*p == 'y'){
q[n] = 'B';
}else{
q[n] = 'C';
}
} else {
q[n] = *p;
}
n++;
p++;
}
q[n++] = '\0';
puts(q);
}
void decrypt(char*str){
int n = 0;
char *p = str, q[MAXSIZE];
while (*p) {
if (isupper(*p)) {
if ((*p >= 'D') && (*p <= 'Z')){
q[n] = tolower(*p - (char) 3);
}else if (*p == 'A'){
q[n] = 'x';
}else if (*p == 'B'){
q[n] = 'y';
}else{
q[n] = 'z';
}
} else {
q[n] = *p;
}
n++;
p++;
}
q[n++] = '\0';
puts(q);
}
int menu(){
puts("To encrpty input e or E\n");
puts("To decrypt input d or D\n");
puts("To exit input any key\n");
puts("Your choice :\n");
return 0;
}
/Program to implement Affine Cipher
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int CalcGCD(int);
main()
{
int i,j,k,gcd,alpha,beta,numstr[100],numcipher[100];
char str[100],cipher[100];
printf("Enter a string\n");
gets(str);
//converting entered string to Capital letters
for(i=0,j=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j]=toupper(str[i]);
j++;
}
else
{
str[j]=' ';
j++;
}
}
str[j]='\0';
printf("Entered string is : %s \n",str);
printf("Enter Alpha value and must be between 1 and 25 both included\n");
scanf("%d",&alpha);
//Checking consitions
if(alpha<1 || alpha>25)
{
printf("Alpha should lie in between 1 and 25\nSorry Try again !\n");
exit(0);
}
gcd=CalcGCD(alpha);
if(gcd!=1)
{
printf("gcd(alpha,26)=1 but \n gcd(%d,26)=%d\nSorry Try again !\n",alpha,gcd);
exit(0);
}
printf("Enter Beta value and must be between 0 and 25 both included\n");
scanf("%d",&beta);
if(beta<0 || beta>25)
{
printf("Beta value should lie between 0 and 25\nSorry Try again !\n");
exit(0);
}
//Conditions Over
//Program Starts
//Storing string in terms of ascii and to restore spaces I used -20
for(i=0;i<strlen(str);i++)
{
if(str[i]!=' ')
numstr[i]=str[i]-'A';
else
numstr[i]=-20;
}
//Ciphering Process
//If numcipher is more than 25 .We need to convert and ensure that lie in between 0 and 25.
(indicating Alphabets)
//A-0,B-1,C-2,.....Y-24,Z-25
printf("Affine Cipher text is\n");
for(i=0;i<strlen(str);i++)
{
if(numstr[i]!=-20)
{
numcipher[i]=((alpha*numstr[i])+beta)%26;
printf("%c",(numcipher[i]+'A'));
}
else
{
printf(" ");
}
}
printf("\n");
while(temp2!=0)
{
x=temp2;
temp2=temp1%temp2;
temp1=x;
}
return(temp1);
}