Saturday, 8 September 2018

Write a program to implement Vigenere Cipher. Information and Network Security (2170709) GTU


Algorithm/Steps :
  1. Take plain text from file named input.txt
  2. Take Key as input from key.txt
  3. Add same number of characters as plain text from  key
  4. Do encryption and save Encrypted text in output.txt
  5. Hence decrypt the encrypted message and save decrypted text in input.txt

Code :
Encryption
#include<stdio.h>
#include<string.h>
int main()
{
            char plain[50],ci[50],key[50];
            FILE *key1 = fopen("key.txt","r");
            FILE *plain1 = fopen("input.txt","r");
            FILE *cipher = fopen("output.txt","w");
           
            fscanf(plain1, "%s", plain);
            printf("\n%s",plain);
           
            fscanf(key1, "%s", key);
            printf("\n%s",key);
            int len = strlen(key);
            int len1 = strlen(plain);
            int j=0,i=len;
            while(i != len1)
            {
                        l1:
                        key[len] = key[j];
                        if(j==(len-1))
                        {
                                    j=0;
                                    goto l1;
                        }
                        len++;
                        i++;
                        j++;
            }
            //printf("\nKey is: %s",key);
            i=0;j=0;
            while(plain[i] != '\0')
            {
                        ci[j] = (plain[i] + key[i]) % 26 + 'A';
                        //printf("%c",ci[j]);
                        i++;
                        j++;
            }
            ci[j] = '\0';
            printf("\nEncryted text is: %s",ci);
            fprintf(cipher,"%s",ci);
            return 0;
}
Decryption
#include<stdio.h>
#include<string.h>
int main()
{
            char plain[50],ci[50],key[50];
            FILE *key1 = fopen("key.txt","r");
            FILE *cipher = fopen("output.txt","r");
           
            fscanf(cipher, "%s", ci);
            printf("\n%s",ci);
            fscanf(key1, "%s", key);
           
            int len = strlen(key);
            int len1 = strlen(ci);
            int j=0,i=len;
            while(i != len1)
            {
                        l1:
                        key[len] = key[j];
                        if(j==(len-1))
                        {
                                    j=0;
                                    goto l1;
                        }
                        len++;
                        i++;
                        j++;
            }
            printf("\n%s",key);
            i=0;j=0;
            while(ci[i] != '\0')
            {
                        plain[j] = (((ci[i] - key[i])+26) % 26) + 'A';
                        i++;
                        j++;
            }
            plain[j] = '\0';
            printf("\nDecrypted text is: %s",plain);
            return 0;
}

Write a program to implement Monoalphabetic Cipher. Information and Network Security (2170709) GTU



Algorithm/Steps :
  1. Take plain text from file named input.txt
  2. Take input Mapping string.
  3. Read text from file (Character By Character) then encrypt the text using Mapping String.
  4. Save Encrypted text in input.txt
  5. Use same method and save decrypted text in output.txt

Code :
Encryption
#include<stdio.h>
#include<string.h>
int main()
{
                char plain[50],ci[50];
                FILE *fp = fopen("input.txt","r");
                FILE *fp1 = fopen("output.txt","w");
                       
                        fscanf(fp, "%s", plain);
                        printf("\nPlain Text is:%s",plain);
                        int len = strlen(plain);
                        char plain1[]="abcdefghijklmnopqrstuvwxyz";
                        char cipher[]="ynlkxbshmiwdpjroqvfeaugtzc";         
                       
                        int z,j=0,i=0;
                        char ch;
                        while(plain[i]!='\0')
                        {
                                    ch=plain[i];
                                    for(j=0;j<26;j++)
                                    {
                                                if(plain[i] == plain1[j])
                                                {
                                                            z=j;
                                                            break;
                                                }
                                    }
                                    ci[i]=cipher[z];
                                    i++;
                        }
                        printf("\nCipher Text is:");
                        for(i=0;i!= len;i++)
                        {
                                    printf("%c",ci[i]);
                                    fprintf(fp1,"%c", ci[i]);
                        }
                       
return 0;
}

Decryption
#include<stdio.h>
#include<string.h>
int main()
{
                char ci[50],plain[50];
                FILE *fp1 = fopen("output.txt","r");
                       
                        fscanf(fp1, "%s", ci);
                        printf("\nEncrpted Text is:%s",ci);
                        int len = strlen(ci);
                        char plain1[]="abcdefghijklmnopqrstuvwxyz";
                        char cipher[]="ynlkxbshmiwdpjroqvfeaugtzc";         
                       
                        int z,j=0,i=0;
                        char ch;
                        while(ci[i]!='\0')
                        {
                                    ch=ci[i];
                                    for(j=0;j<26;j++)
                                    {
                                                if(ci[i] == cipher[j])
                                                {
                                                            z=j;
                                                            break;
                                                }
                                    }
                                    plain[i]=plain1[z];
                                    i++;
                        }
                        printf("\nDecryption is:");
                        for(i=0;i!= len;i++)
                        {
                                    printf("%c",plain[i]);
                        }
                        return 0;
}

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...