Saturday, 8 September 2018

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

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

Code :
Encryption
#include<stdio.h>

int main()
{
            int key,i=0;
            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);
            printf("\nEnter Key:");
            scanf("%d",&key);
           
            while(plain[i]!='\0')
            {
                        if(plain[i] >= 97 && plain[i]<= 122 )
                                    ci[i] = ((plain[i] + key-97) % 26) + 97 ;
                        else
                                    ci[i] = ((plain[i] + key-65) % 26) + 65 ;
                       
                        i++;
            }
            ci[i]='\0';
            i=0;
            printf("Encryption is:");
            while(ci[i]!='\0')
            {
                        printf("%c",ci[i]);
                        i++;
            }
            fprintf(fp1, "%s", ci);
}

Decryption
#include<stdio.h>

int main()
{
            int key,i=0;
            char ci[50];
            FILE *fp1 = fopen("output.txt","r");
           
            fscanf(fp1, "%s",ci);

            printf("\nEnter Key:");
            scanf("%d",&key);
           
            printf("\nDecryption is:");
           
            while(ci[i]!='\0')
            {
                        if(ci[i] >= 97 && ci[i]<= 122 )
                                    printf("%c",((((ci[i] - key)-97+26) % 26) + 97 ));
                        else
                                    printf("%c",((((ci[i] - key)-65+26) % 26) + 65 ));
                       
                        i++;
            }
}

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...