How to resolve the algorithm Substitution cipher step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Substitution cipher step by step in the C programming language

Table of Contents

Problem Statement

Substitution Cipher Implementation - File Encryption/Decryption

Encrypt an input/source file by replacing every upper/lower case alphabets of the source file with another predetermined upper/lower case alphabets or symbols and save it into another output/encrypted file and then again convert that output/encrypted file into original/decrypted file. This type of Encryption/Decryption scheme is often called a Substitution Cipher.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Substitution cipher step by step in the C programming language

This C code implements a program that encrypts or decrypts a text file using a simple character substitution cipher and writes the result to a new file.

A breakdown of the code:

  • It includes the necessary header files <stdlib.h>, <stdio.h>, and <wchar.h> for input/output and wide character manipulation.

  • It defines some constants for the ASCII character range (ALPHA and OMEGA) and for the encryption/decryption flag (ENCRYPT and DECRYPT).

  • The wideStrLen function calculates the length of a wide character string (a string of wchar_t characters).

  • The processFile function takes a filename, two characters (plainKey and cipherKey), and a flag flag (ENCRYPT or DECRYPT) as input.

    • It opens the input file for reading and creates an output file with a "_ENC" or "_DEC" suffix, depending on the flag.
    • It reads the input file line by line and processes each line.
    • For each line, it converts it to a wide character string (str) and calculates its length (len).
    • It allocates memory for the output wide character string (outStr).
    • It iterates over each wide character in the input string:
      • If it's an alphabetic character within the specified character range (ALPHA to OMEGA), it applies the encryption or decryption transformation based on the flag.
      • Otherwise, it preserves the character as it is.
    • It writes the transformed wide character string (outStr) to the output file.
    • It frees the memory allocated for outStr.
  • The main function:

    • Checks if the number of command-line arguments is correct (5).
    • Calls the processFile function with the appropriate arguments based on the command-line inputs.
    • Notifies the user about the creation of the encrypted or decrypted file.

In summary, this program reads a text file, applies a character substitution encryption or decryption using keys, and writes the result to a new file. It assumes that the input is a wide character string and handles the character transformations accordingly.

Source code in the c programming language

#include<stdlib.h>
#include<stdio.h>
#include<wchar.h>

#define ENCRYPT 0
#define DECRYPT 1
#define ALPHA 33
#define OMEGA 126

int wideStrLen(wchar_t* str){
	int i = 0;
	while(str[i++]!=00);
	
	return i;
}

void processFile(char* fileName,char plainKey, char cipherKey,int flag){
	
	FILE* inpFile = fopen(fileName,"r");
	FILE* outFile;
	
	int i,len, diff = (flag==ENCRYPT)?(int)cipherKey - (int)plainKey:(int)plainKey - (int)cipherKey;
	wchar_t str[1000], *outStr;
	char* outFileName = (char*)malloc((strlen(fileName)+5)*sizeof(char));

	sprintf(outFileName,"%s_%s",fileName,(flag==ENCRYPT)?"ENC":"DEC");
	
	outFile = fopen(outFileName,"w");
	
	while(fgetws(str,1000,inpFile)!=NULL){
		len = wideStrLen(str);
		
		outStr = (wchar_t*)malloc((len + 1)*sizeof(wchar_t));
		
		for(i=0;i<len;i++){
			if((int)str[i]>=ALPHA && (int)str[i]<=OMEGA && flag == ENCRYPT)
				outStr[i] = (wchar_t)((int)str[i]+diff);
			 else if((int)str[i]-diff>=ALPHA && (int)str[i]-diff<=OMEGA && flag == DECRYPT)
				outStr[i] = (wchar_t)((int)str[i]-diff);
			else
				outStr[i] = str[i];
		}
		outStr[i]=str[i];
		
		fputws(outStr,outFile);
		
		free(outStr);
	}
	
	fclose(inpFile);
	fclose(outFile);
}

int main(int argC,char* argV[]){
	if(argC!=5)
		printf("Usage : %s <file name, plain key, cipher key, action (E)ncrypt or (D)ecrypt>",argV[0]);
	else{
		processFile(argV[1],argV[2][0],argV[3][0],(argV[4][0]=='E'||argV[4][0]=='e')?ENCRYPT:DECRYPT);
		
		printf("File %s_%s has been written to the same location as input file.",argV[1],(argV[4][0]=='E'||argV[4][0]=='e')?"ENC":"DEC");
	}
	
	return 0;
}


  

You may also check:How to resolve the algorithm General FizzBuzz step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Heronian triangles step by step in the D programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the Julia programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the Action! programming language
You may also check:How to resolve the algorithm Animate a pendulum step by step in the C# programming language