Index 1
Index 1
ENCRYPTION
#include<stdio.h>
#include<string.h>
main()
//A-X,B-Y,C-Z,D-A,E-B,F-C... if number=1
int i,number;
char str[1000];
printf("Enter a sentence\n");
gets(str);
scanf("%d",&number);
i=0;
number%=26;
while(str[i]!='\0')
if(!((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
if((str[i]>'a'+number+1)&&(str[i]<='z'))
printf("%c",str[i]-number-2);
else if((str[i]>'A'+number+1)&&(str[i]<='Z'))
printf("%c",str[i]-number-2);
else
printf("%c",str[i]+24-number);
}
if(((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
printf("%c",str[i]);
i++;
printf("\n");
DECRYPTION
#include<stdio.h>
#include<string.h>
main()
//Decryption
//X-A,Y-B,Z-C,A-D,B-E,C-F... if number=1
int i,number;
char str[1000],str1[1000];
printf("Enter a sentence\n");
gets(str);
scanf("%d",&number);
i=0;
if(!((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
if((str[i]>='a')&&(str[i]<'z'-number-1))
printf("%c",str[i]+number+2);
else if(str[i]>='A'&&str[i]<'Z'-number-1)
printf("%c",str[i]+number+2);
else
printf("%c",str[i]-24+number);
if(((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
printf("%c",str[i]);
i++;
printf("\n");
}
2. Implementing One Time Pad Cipher Algorithm
ENCRYPTION
#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
//All the text which ever entered is converted to upper and without spaces
int i,j,len1,len2,numstr[100],numkey[100],numcipher[100];
char str[100],key[100],cipher[100];
printf("Enter a string text to encrypt\n");
gets(str);
for(i=0,j=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j]=toupper(str[i]);
j++;
}
}
str[j]='\0';
//obtaining numerical plain text ex A-0,B-1,C-2
for(i=0;i<strlen(str);i++)
{
numstr[i]=str[i]-'A';
}
printf("Enter key string of random text\n");
gets(key);
for(i=0,j=0;i<strlen(key);i++)
{
if(key[i]!=' ')
{
key[j]=toupper(key[i]);
j++;
}
}
key[j]='\0';
//obtaining numerical one time pad(OTP) or key
for(i=0;i<strlen(key);i++)
{
numkey[i]=key[i]-'A';
}
for(i=0;i<strlen(str);i++)
{
numcipher[i]=numstr[i]+numkey[i];
}
//To loop the number within 25 i.e if addition of numstr and numkey is 27 then numcipher
should be 1
for(i=0;i<strlen(str);i++)
{
if(numcipher[i]>25)
{
numcipher[i]=numcipher[i]-26;
}
}
printf("One Time Pad Cipher text is\n");
for(i=0;i<strlen(str);i++)
{
printf("%c",(numcipher[i]+'A'));
}
printf("\n");
DECRYPTION
#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
//All the text which ever entered is converted to upper and without spaces
int i,j,len1,len2,numstr[100],numkey[100],numcipher[100];
char str[100],key[100],cipher[100];
printf("Enter an Encrypted string text to Decrypt\n");
gets(str);
for(i=0,j=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j]=toupper(str[i]);
j++;
}
}
str[j]='\0';
//obtaining numerical plain text ex A-0,B-1,C-2
for(i=0;i<strlen(str);i++)
{
numstr[i]=str[i]-'A';
}
printf("Enter key string of random text\n");
gets(key);
for(i=0,j=0;i<strlen(key);i++)
{
if(key[i]!=' ')
{
key[j]=toupper(key[i]);
j++;
}
}
key[j]='\0';
//obtaining numerical one time pad(OTP) or key
for(i=0;i<strlen(key);i++)
{
numkey[i]=key[i]-'A';
}
for(i=0;i<strlen(str);i++)
{
numcipher[i]=numstr[i]-numkey[i];//changed from + to - for decryption
if(numcipher[i]<0)
{
numcipher[i]=numcipher[i]+26;//If cipher is negative we have to add 26
}
numcipher[i]=numcipher[i]%26;//To loop within 1 to 26 for alphabets from A-Z
}
}
3. Rail Fence transposition technique
#include<stdio.h>
#include<string.h>
char railMatrix[key][msgLen];
railMatrix[i][j] = '\n';
railMatrix[row][col++] = msg[i];
k= k * (-1);
row = row + k;
if(railMatrix[i][j] != '\n')
printf("%c", railMatrix[i][j]);
}
void decryptMsg(char enMsg[], int key){
char railMatrix[key][msgLen];
railMatrix[i][j] = '\n';
railMatrix[row][col++] = '*';
k= k * (-1);
row = row + k;
if(railMatrix[i][j] == '*')
railMatrix[i][j] = enMsg[m++];
row = col = 0;
k = -1;
k= k * (-1);
row = row + k;
int main(){
int key = 3;
encryptMsg(msg, key);
decryptMsg(enMsg, key);
return 0;
Output
/*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
// Note: All variables are unsigned 32 bit and wrap modulo 2^32 when calculating
uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
// Use binary integer part of the sines of integers (in radians) as constants// Initialize variables:
uint32_t k[] = {
h0 = 0x67452301;
h1 = 0xefcdab89;
h2 = 0x98badcfe;
h3 = 0x10325476;
//append "0" bit until message length in bit ≡ 448 (mod 512)
int new_len;
new_len /= 8;
int offset;
#ifdef DEBUG
puts("");
#endif
uint32_t a = h0;
uint32_t b = h1;
uint32_t c = h2;
uint32_t d = h3;
// Main loop:
uint32_t i;
#ifdef ROUNDS
uint8_t *p;
p=(uint8_t *)&a;
p=(uint8_t *)&b;
p=(uint8_t *)&c;
p=(uint8_t *)&d;
#endif
uint32_t f, g;
if (i < 16) {
g = i;
g = (5*i + 1) % 16;
f = b ^ c ^ d;
g = (3*i + 5) % 16;
} else {
f = c ^ (b | (~d));
g = (7*i) % 16;
#ifdef ROUNDS
#endif
uint32_t temp = d;
d = c;
c = b;
a = temp;
}
h0 += a;
h1 += b;
h2 += c;
h3 += d;
// cleanup
free(msg);
if (argc < 2) {
return 1;
// int i;
md5(msg, len);
// }
uint8_t *p;
// display result
p=(uint8_t *)&h0;
p=(uint8_t *)&h1;
p=(uint8_t *)&h2;
p=(uint8_t *)&h3;
puts("");
return 0;
}
5. RSA ALGORITHM
#include<stdio.h>
#include<math.h>
int temp;
while(1)
temp = a%h;
if(temp==0)
return h;
a = h;
h = temp;
int main()
double p = 3;
double q = 7;
double n=p*q;
double count;
double e=2;
while(e<totient){
count = gcd(e,totient);
if(count==1)
break;
else
e++;
//private key
double d;
double k = 2;
d = (1 + (k*totient))/e;
double c = pow(msg,e);
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);
printf("Message data = %lf",msg);
printf("\np = %lf",p);
printf("\nq = %lf",q);
printf("\nn = pq = %lf",n);
printf("\ntotient = %lf",totient);
printf("\ne = %lf",e);
printf("\nd = %lf",d);
return 0;
Output