0% found this document useful (0 votes)
29 views8 pages

CN Programs (Rakshith)

The document describes an implementation of the RSA encryption algorithm in C. It begins by explaining the key generation process using prime numbers p and q to calculate the public and private keys. It then walks through encrypting a message by exponentiating each character using the public key, and decrypting the ciphertext by exponentiating with the private key. The code takes a message as input, performs RSA encryption and decryption, and outputs the original plaintext.
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)
29 views8 pages

CN Programs (Rakshith)

The document describes an implementation of the RSA encryption algorithm in C. It begins by explaining the key generation process using prime numbers p and q to calculate the public and private keys. It then walks through encrypting a message by exponentiating each character using the public key, and decrypting the ciphertext by exponentiating with the private key. The code takes a message as input, performs RSA encryption and decryption, and outputs the original plaintext.
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/ 8

FRAMESORT:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include<math.h>
struct frame
{
int no;
char frm[3];
}f1[10],f2[10],temp;
int main()
{
int i,j,len,nof,ord;
char data[100],data2[100];
printf("enter string");
gets(data);
len=strlen(data);
nof=ceil(len/3.0);
for(j=0;j<nof;j++)
{
f1[j].no=j;
f1[j].frm[0]=data[j*3];
f1[j].frm[1]=data[j*3+1];
f1[j].frm[2]=data[j*3+2];
}
for(j=0;j<nof;j++)
printf("%d-%s\n",f1[j].no,f1[j].frm);
printf("enter orders to suffle");
for(j=0;j<nof;j++)
{
scanf("%d",&ord);
f2[j]=f1[ord];
}

RAKSHITH_KULAL
printf("\n after suffling\n");
for(j=0;j<nof;j++)
printf("%d-%s\n",f2[j].no,f2[j].frm);
for(i=0;i<nof;i++)
for(j=0;j<nof-1;j++)
if(f2[j].no>f2[j+1].no)
{
temp=f2[j];
f2[j]=f2[j+1];
f2[j+1]=temp;
}
printf("\n after sorting \n");
strcpy(data2,"");
for(j=0;j<nof;j++)
{
printf("%d-%s\n",f2[j].no,f2[j].frm);
strcat(data2,f2[j].frm);
}
printf("\n data in receiver side :%s",data2);
}
//author: rakshith_kulal
OUTPUT:

RAKSHITH_KULAL
CRC:

#include <stdio.h>
#include <conio.h>
#include <string.h>
int i,j,keylen,msglen,ch,f;
char data[100],key[30],temp[30],rem[30],zero[30],div[30],input[100];
void crc()
{
for (i=0;i<keylen;i++)
temp[i]=data[i];
for (i=0;i<msglen;i++)
{
if(temp[0]=='0')
strcpy(key,zero);
else
strcpy(key,div);
for (j=0;j<keylen-1;j++)
rem[j]=(temp[j+1]==key[j+1])?'0':'1';
rem[j]=data[i+keylen];
strcpy(temp,rem);
}
}
void main()
{
printf("Enter Data: ");
gets(input);
strcpy(data,input);
printf("Enter divisor : ");
gets(div);
keylen=strlen(div);
msglen=strlen(input);
for (i=0;i<keylen-1;i++)
data[msglen+i]='0';
printf("\n data after appending zeros %s",data);
for (i=0;i<keylen;i++)
zero[i]='0';
crc();
strcat(input,rem);
strcpy(data,input);
printf("\nRemainder is %s",rem);
printf("\ndata after appending remainder: %s",data);

RAKSHITH_KULAL
printf("\ndo you want make error(Y/N)?");
scanf("%c",&ch);
if(ch=='Y' || ch=='y')
data[msglen/2]=(data[msglen/2]=='0')?'1':'0';
crc();
printf("\ndata obtained %s",data);
printf("\nRemainder is %s\n",rem);
f=1;
for (i=0;i<keylen-1;i++)
if(rem[i]!='0'){
f=0;
break;
}
if(f==1){
printf("No Error Occurred final data is ");
for (i=0;i<msglen;i++)
printf("%c",data[i]);
}
else
printf("Error Occurred");
}
//author: rakshith_kulal
Output:

Note: input is taken as string don’t give space in between any bits

RAKSHITH_KULAL
HAMMING CODE:

#include<stdio.h>
int main()
{
int i,ham[12],pos,r1,r2,r4,r8,errpos;
char ch;
printf("enter the 7 bit data to be transmitted(0's & 1's)\n");
for(i=11;i>0;i--)
if(i!=1 && i!=2 && i!=4 && i!=8)
scanf("%d",&ham[i]);
ham[1]=(ham[3]+ham[5]+ham[7]+ham[9]+ham[11])%2;
ham[2]=(ham[3]+ham[6]+ham[7]+ham[10]+ham[11])%2;
ham[4]=(ham[5]+ham[6]+ham[7])%2;
ham[8]=(ham[9]+ham[10]+ham[11])%2;
printf("genarated hamming code \n");
for(i=11;i>0;i--)
printf("%d ",ham[i]);
printf("\ndo you want make error(Y/N)?");
ch=getch();
if(ch=='Y' || ch=='y')
{
printf("\nenter the position to make error ");
scanf("%d",&pos);
ham[pos]=(ham[pos]==0)?1:0;
}
printf("\n data Received\n");
for(i=11;i>0;i--)
printf("%d ",ham[i]);
r1=(ham[1]+ham[3]+ham[5]+ham[7]+ham[9]+ham[11])%2;
r2=(ham[2]+ham[3]+ham[6]+ham[7]+ham[10]+ham[11])%2;
r4=(ham[4]+ham[5]+ham[6]+ham[7])%2;
r8=(ham[8]+ham[9]+ham[10]+ham[11])%2;
errpos=(r8*8)+(r4*4)+(r2*2)+(r1*1);
if(errpos==0)
printf("\nNo error Occurred");
else
{
printf("\nError detected in position %d",errpos);
ham[errpos]=(ham[errpos]==0)?1:0;

RAKSHITH_KULAL
printf("\nData after error correction\n");
for(i=11;i>0;i--)
printf("%d ",ham[i]);
}
}
//author: rakshith_kulal

Output:

Note: input is taken as integer give space in between each bit, press enter after 7th bit

RAKSHITH_KULAL
RSA:

#include <stdio.h>
#include <string.h>
int gcd(int a,int b)
{
if(a==b)
return a;
else if(a>b)
gcd(a-b,b);
else
gcd(a,b-a);
}
int expmod(int x, int y, int n)
{
int r=1;
while(y>0)
{
if((int)(y%2)==1)
r=(r*x)%n;
x=(x*x)%n;
y=y/2;
}
return(r);
}
int main()
{
int p,q,n,e,d,i,z,plain[100],cipher[100],plain2[100];
char msg[100];
printf("Enter 2 prime numbers");
scanf("%d%d",&p,&q);
n=p*q;
z=(p-1)*(q-1);
for(i=2;i<=z;i++)
if(gcd(i,z)==1)
printf("%d ",i);
printf("\nChoose any e from above list");
scanf("%d",&e);
d=1;
while((e*d)%z!=1)
d++;
printf("\n public key (%d,%d)",e,n);
printf("\n private key (%d,%d)",d,n);
printf("\nEnter Message");

RAKSHITH_KULAL
scanf("%s",msg);
printf("plaintext \n");
for(i=0;i<strlen(msg);i++)
{
plain[i]=msg[i];
printf("%d\t%c\n",plain[i],plain[i]);
}
printf("Ciphertext after Encryption\n");
for(i=0;i<strlen(msg);i++)
{
cipher[i]=expmod(plain[i],e,n);
printf("%d\t%c\n",cipher[i],cipher[i]);
}
printf("plaintext after Decryption\n");
for(i=0;i<strlen(msg);i++)
{
plain2[i]=expmod(cipher[i],d,n);
printf("%d\t%c\n",plain2[i],plain2[i]);
}
return 0;
}
//author: rakshith_kulal
Output:

RAKSHITH_KULAL

You might also like