Saturday, 8 September 2018

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;
}

No comments:

Post a Comment

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