Monday, October 22, 2018

Diffie–Hellman key exchange using C programming language


This is a code for Diffie–Hellman key exchange using C programming language.



Diffie–Hellman key exchange:-
Source code:-

#include <stdio.h>
int compute(int a, int m, int n)
{
int r;
int y = 1;

while (m > 0)
{
r = m % 2;

if (r == 1)
y = (y*a) % n;
a = a*a % n;

m = m / 2;
}

return y;
}
int main()
{
int p = 23;
int g = 5;

int a, b;
int A, B;
a = 6;
A = compute(g, a, p);

b = 15;
B = compute(g, b, p);
int keyA = compute(B, a, p);
int keyB = compute(A, b, p);

printf("Alice's Secret Key is %d\nBob's Secret Key is %d", keyA, keyB);

return 0;
}

Output:-


Elliptic curve cryptography using C programming language


This is a code for Elliptic curve cryptography using C programming language.



Elliptic curve cryptography:-
Source code:-

#include <iostream>
#include<math.h>
using namespace std;

int main(){
    int p,a,b,x,y;
    float temp;
    cin>>p>>a>>b;
    x = 0;
    cout<<"x\t"<<"y\t"<<"points"<<endl;
    while(x<p){
        temp = sqrt(((x*x*x) + x + 1) % p);
        y = temp;
        if(y == temp)
        {
            cout<<x<<"\t";
            cout<<y<<"\t";
            cout<<"("<<x<<","<<y<<") ";
            cout<<"("<<x<<","<<"-"<<y<<")";
        }
        else{
            cout<<x<<"\t";
            cout<<y<<"\t";
            cout<<"-";
            cout<<"-";
        }
        cout<<endl;
        x++;
    }
    return 0;
}


Output:-


Euclidean algorithm using C programming language


This is a code for Euclidean algorithm using C programming language.



Euclidean algorithm:-
Source code:-

#include <stdio.h>
int greatestCommonDivisor(int m, int n)
{
    int r;
    if((m == 0) || (n == 0))
        return 0;
    else if((m < 0) || (n < 0))
        return -1;

    do
    {
        r = m % n;
        if(r == 0)
            break;
        m = n;
        n = r;
    }
    while(1);

    return n;
}

int main(void)
{
    int num1 = 600, num2 = 120;
    int gcd = greatestCommonDivisor(num1, num2); 

    printf("The GCD of %d and %d is %d\n", num1, num2, gcd);

    getchar();
   return 0;
}


Output:-


Extended euclidean cipher using C programming language


This is a code for Extended euclidean using C programming language.



Extended euclidean :-
Source code:-

#include<stdio.h>
#include<conio.h>
int main()
{
    int r1,a,r2,b,r,q,s1,s2,t1,t2,s,t;
    printf("Enter the value of a and b:");
    scanf("%d%d",&a,&b);
    r1=a;
    r2=b;
    s1=1,s2=0,t1=0,t2=1;
    while(r2>0)
    {
        q=r1/r2;
        r=r1-q*r2;
        s=s1-q*s2;
        t=t1-q*t2;
        printf(" %d    %d   %d     %d      %d   %d  %d  %d   %d   %d \n",q,r1,r2,r,s1,s2,s,t1,t2,t);
        r1=r2;
        r2=r;
        s1=s2;
        s2=s;
        t1=t2;
        t2=t;
    }
    printf(" %d   *   %d   +   %d    *   %d   = gcd(%d,%d) = %d \n",s,a,t,b,a,b,r1);
    getch();
}


Output:-


OTP ( One-time pad ) using C programming language


This is a code for OTP ( One-time pad )
 using C programming language.




OTP Encryption:-
Source code:-

#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
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';
    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';
    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];
    }
    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");
}

Output:-


Playfair cipher using C programming language


This is a code for Playfair cipher using C programming language.



Playfair cipher:-
Source code:-

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define MX 5  
void playfair(char ch1, char ch2, char key[MX][MX]) {  
int i, j, w, x, y, z;  
    FILE * out;  
    if ((out = fopen("cipher.txt", "a+")) == NULL) {  
printf("File Currupted.");  
    }  
    for (i = 0; i < MX; i++) {  
        for (j = 0; j < MX; j++) {  
            if (ch1 == key[i][j]) {  
                w = i;  
                x = j;  
            } else if (ch2 == key[i][j]) {  
                y = i;  
                z = j;  
            }  
        }  
    }  
    //printf("%d%d %d%d",w,x,y,z);  
    if (w == y) {  
        x = (x + 1) % 5;  
        z = (z + 1) % 5;  
printf("%c%c", key[w][x], key[y][z]);  
fprintf(out, "%c%c", key[w][x], key[y][z]);  
    } else if (x == z) {  
        w = (w + 1) % 5;  
        y = (y + 1) % 5;  
printf("%c%c", key[w][x], key[y][z]);  
fprintf(out, "%c%c", key[w][x], key[y][z]);  
    } else {  
printf("%c%c", key[w][z], key[y][x]);  
fprintf(out, "%c%c", key[w][z], key[y][x]);  
    }  
fclose(out);  
}  
int main() {  
int i, j, k = 0, l, m = 0, n;  
    char key[MX][MX], keyminus[25], keystr[10], str[25] = {  
        0  
    };  
    char alpa[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'  
    };  
printf("\nEnter key:");  
    gets(keystr);  
printf("\nEnter the plain text:");  
    gets(str);  
    n = strlen(keystr);  
    //convert the characters to uppertext
    for (i = 0; i < n; i++) {  
        if (keystr[i] == 'j') keystr[i] = 'i';  
        else if (keystr[i] == 'J') keystr[i] = 'I';  
keystr[i] = toupper(keystr[i]);  
    }  
    //convert all the characters of plaintext to uppertext
    for (i = 0; i<strlen(str); i++) {  
        if (str[i] == 'j') str[i] = 'i';  
        else if (str[i] == 'J') str[i] = 'I';  
str[i] = toupper(str[i]);  
    }  
    // store all characters except key  
    j = 0;  
    for (i = 0; i < 26; i++) {  
        for (k = 0; k < n; k++) {  
            if (keystr[k] == alpa[i]) break;  
            else if (alpa[i] == 'J') break;  
        }  
        if (k == n) {  
keyminus[j] = alpa[i];  
            j++;  
        }  
    }  
    //construct key keymatrix
    k = 0;  
    for (i = 0; i < MX; i++) {  
        for (j = 0; j < MX; j++) {  
            if (k < n) {  
                key[i][j] = keystr[k];  
                k++;  
            } else {  
                key[i][j] = keyminus[m];  
                m++;  
            }  
printf("%c ", key[i][j]);  
        }  
printf("\n");  
    }  
printf("\n\nEntered text :%s\nCipher Text :", str);  
    for (i = 0; i<strlen(str); i++) {  
        if (str[i] == 'J') str[i] = 'I';  
        if (str[i + 1] == '\0') playfair(str[i], 'X', key);  
        else {  
            if (str[i + 1] == 'J') str[i + 1] = 'I';  
            if (str[i] == str[i + 1]) playfair(str[i], 'X', key);  
            else {  
playfair(str[i], str[i + 1], key);  
                i++;  
            }  
        }  
    }  
getch();  
}

Output:-


Rail fence cipher using C programming language


This is a code for Rail fence cipher using C programming language.



Rail fence cipher:-
Source code:-

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ int i,j,k,l;
char a[20],c[20],d[20];
//clrscr();
printf("\n**********RailFence**********");
printf("\nEnter the input string: ");
gets(a);
l=strlen(a);
for(i=0,j=0;i<l;i++)
{ if(i%2==0)
c[j++]=a[i];
}
for(i=0;i<l;i++)
{ if(i%2==1)
c[j++]=a[i];
}
c[j]='\0';
printf("\nCipher text after applying rail fence: ");
printf("\n%s",c);
if(l%2==0)
k=l/2;
else
k=(l/2)+l;
for(i=0,j=0;i<k;i++)
{ d[j]=c[i];
j=j+2;
}
for(i=k,j=1;i<l;i++)
{ d[j]=c[i];
j=j+2;
}

getch();
}

Output:-


Diffie–Hellman key exchange using C programming language

This is a code for  Diffie–Hellman key exchange  using C programming language. Diffie–Hellman key exchange :- Source code:- #in...