Encrypt/Decrypt a file using any Ciphers in C program in Linux & Project |

 Write a ‘C’ program to encrypt/decrypt a file using any one of the given ciphers:

An offset cipher: In an offset cipher each character from the source file is offset with a fixed value and then written to the target file. For example, if the character read from the source file is ‘A’, then convert this into a new character by offsetting ‘A’ by a fixed value, say 128, and then writing the new character to the target file.


Encryption: -

Code

#include<stdio.h>

#include<stdlib.h>

int main()

{

FILE *fdr,*fdw ;

char ch, fileName[20], output[20];

int op;

printf("Enter file name to be read:");

scanf("%s",fileName);

printf("Enter the name of output file:");

scanf("%s",output);

fdr=fopen(fileName,"r");

fdw=fopen(output,"w");

if(fdr==NULL)

{

    printf("\nFile does not exist...!\n"); exit(0);

}

ch=fgetc(fdr);

while(ch!=EOF)

{

    op=(int)ch;

    op=op+5;

    fprintf(fdw,"%c",op);

    ch=fgetc(fdr);

}

fclose(fdw);

fclose(fdr);

printf("Encrypted file %s is created..\n",output);

return 0;

}


Linux command


Encrypt/Decrypt a file using any Ciphers in C program in Linux & Project |


 Geditor

Encrypt/Decrypt a file using any Ciphers in C program in Linux & Project |


 Text file

Encrypt/Decrypt a file using any Ciphers in C program in Linux & Project |



Output

Encrypt/Decrypt a file using any Ciphers in C program in Linux & Project |



 Description

 

 Code

 #include<stdio.h>

#include<stdlib.h>

int main()

{

    FILE *fdr,*fdw;

    Char ch, fileName[20], output[20]; 

    int op;

    printf("Enter file name to be read:");

    scanf("%s",fileName);'

    printf("Enter the name of output file:");

    scanf("%s",output);

    fdr=fopen(fileName,"r");

    fdw=fopen(output,"w");

    if(fdr==NULL)

    {

        printf("\nFile does not exist...!\n"); exit(0);

    }

    ch=fgetc(fdr);

    while(ch!=EOF)

    {

        op=(int)ch;

        op=op-5;

        fprintf(fdw,"%c",op);

        ch=fgetc(fdr);

    }

    fclose(fdw);

    fclose(fdr);

    printf("File is decrypted with name %s.\n",output);

    return 0;

}


Linux command

Encrypt/Decrypt a file using any Ciphers in C program in Linux & Project |

 Geditor

Encrypt/Decrypt a file using any Ciphers in C program in Linux & Project |


Output

Encrypt/Decrypt a file using any Ciphers in C program in Linux & Project |



Read More:- Linux MakeFile

1 Comments

Previous Post Next Post