Wednesday, 17 February 2021

Simple substitution cipher - ciphering using a predefined key

This one is different from the previous code I posted because here, as it uses a key file as the key instead of a hard-coded one.


The code:

#include <cstdlib>

#include <iostream>

#include <fstream>

#include <cstring>


using namespace std;


char line[250]={'\0'};                  //For input data

char line_c[250]={'\0'};                //For coded/decoded data

char line_key[250] = {};                //Key

//array storing the alphabet

char code[68]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',

        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',

        '1','2','3','4','5','6','7','8','9','0','_','.',',','!','\'','?'};


/*

 * Ciphers a character.

 */

char get_ciphered(char character){

    char ch = '\0';

    for (int i=0; i<68;i++){

        if ((character==code[i])){

            ch = line_key[i];

            break;

        }

    }

    return ch;

}


/*

 * Deciphers a character

 */

char get_deciphered(char character){

    char ch = '\0';

    for (int i=0; i<68;i++){

        if ((character==line_key[i])){

            ch = code[i];

            break;

        }

    }

    return ch;

}


/*

 * Ciphers a line of input

 */

void encode_simple(){

    int temp=0;

    char temp_1 = '\0';

    for (int i = 0; i<250; i++){

        if (line[i]=='\0') break;

        temp=get_ciphered(line[i]);

        if (temp!='\0'){

            line_c[i]=temp;

        }

        else{

            line_c[i]=line[i];

        }

    }

}


/*

 * Deciphers a line of input

 */

void decode_simple(){

    int temp=0;

    char temp_1 = '\0';

    for (int i = 0; i<250; i++){

        if (line[i]=='\0') break;

        temp=get_deciphered(line[i]);

        if (temp!='\0'){

            line_c[i]=temp;

        }

        else{

            line_c[i]=line[i];

        }

    }

}


/*

 * 

 */

int main(int argc, char** argv) {

    //for input and output files

    ifstream input1;

    fstream output1;

    ofstream output2;

    

    //for key

    ifstream key;

    

    input1.open("input1.txt",ios::in);

    output1.open("output1.txt",ios::out);

    

    //set key array from file

    key.open("key.txt",ios::in);

    key>>line_key;

    for (int i = 0;i<68; i++){

        if (line_key[i]==0){

            cout<<"null character";

            line_key[i] = '+';

        }

    }

    

    //cipher input1 and put to output 1

    while(!(input1.eof())){

        memset(line,'\0',250);

        memset(line_c,'\0',250);

        input1>>line;

        encode_simple();

        output1<<line_c<<endl;;

    }

    input1.close();

    output1.close();

    

    output1.open("output1.txt",ios::in);

    output2.open("output2.txt",ios::out);

    

    //decipher output1 and put to output 2

    while(!(output1.eof())){

        memset(line,'\0',250);

        memset(line_c,'\0',250);

        output1>>line;

        decode_simple();

        output2<<line_c<<endl;

    }

    output1.close();

    output2.close();

    


    return 0;

}

This code needs for files:
input1: your input plaintext
output1: your ciphered text
output2: the deciphered text from output1
key: a text file containing a single line. For example, for the test run, I used
AH3qZd4MCm_VuBlD1X'gvFknyOp2SjU!NoEx5PYa06Jbc8R.zrw9TheQIs0?fi7LKWtG
Basically, the first character here will map to the first character on your code array, and so on.

Speaking of that, the array code[68] contains your alphabet. Here, I've used alphanumeric character plus a few symbols. You can change it as you like.
Try to match the length of the file 'key' to the size of your alphabet. If key_line is not filled completely, the program will warn you, but it will run anyway. All the missing characters will be replaced with '+' in the key. Deciphering will be chaos (say, the key ends at Z. Then everything after Z will be ciphered to +, and deciphered to Z).

The ciphering and deciphering parts of the program are separate, so you can change the program so that it uses one or the other.

For ciphering only, you can comment out everything from 'output1.open' to 'output2.close' (the latter part of the main function).

For deciphering only, you can comment out the two lines at 'input1.open', and then comment out everything from 'while(!input1.eof...' to 'output1.close'.
Make sure you put your ciphered text in output1.txt.

I am fully aware this is not the best code for the purpose out there, mostly because I whipped it up in ten minutes using bits and pieces from previous stuff I've done. (Don't tell me you don't do that, I know for a fact that you do).

I'd love to have your feedback on this. Also, if you have any questions, please ask in the comments below.

You can follow me on Facebook here or on YouTube here.

See you next time!

No comments:

Post a Comment

How to write a character who is smarter than you

We all have that one character (or few) who is significantly smarter than the writer. So, as a writer, how do you write such a character con...