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!

Wednesday, 3 February 2021

When should you kill a character?




I've already done a blog post on how not to kill a character. That was focused on one character death I wrote in particular. This one, however, is a more general guideline on when to and when not to send your characters to their grisly ends.

My preferred genres are mystery/thriller and fantasy, both genres that involve a lot of death. In both genres, the writer can remove any character at any time – or at least, find an excuse to do so. So, when should you go ahead with it?

The good reasons

These are the good reasons to write a character death, but that doesn't mean that the character death you write will actually be good. It just means that they are probably necessary to the story.

Reason 1: It is the logical end of that character’s arc. If your character has served his purpose, you can get rid of them. Related to that,

Reason 2: Leaving the character in the story longer would make things too easy for the rest of the cast. Of course you could deactivate a character in other ways (leaving the country, going into a coma, suddenly deciding to be a hermit, going to prison, the possibilities are endless), but death has a finality to it that very few other alternatives have.

Reason 3: To motivate other characters. This is a bit of a cliché at this point, but a character death can motivate others to act. It could be grief, it could be a need for revenge, and it could simply be the loss of attachment. Either way, it can be a great source of motives for other characters.

Reason 4: to further the plot. All you mystery writers know exactly what I’m talking about.


The bad reasons

This is the kind of thing that happens when you take your real world frustrations out on your characters. These are the character deaths that are not necessary to the plot. 

I'm not saying you can't make one of these good. It all depends on execution.

Reason 1: Just for shock value. 

Look, I’ve done this. I’m not saying I’m innocent, I’m saying I’ll not do it again and you shouldn’t either. Besides, if you need to kill a character to generate emotion, your story may be running on fumes in terms of character motivations. Also, if you keep doing this, your readers will grow numb and any serious character deaths that need to land won’t land with them.

Another possible problem with this is, characters have functions. If you get rid of one randomly, other characters will have to cover for them or you’ll have to introduce new characters. That means building their image in the reader's mind, which means more effort and a longer story.

Reason 2: Because you have too many characters and need to reduce the size of your cast herd. Again, I’ve done this as well. First of all, if your character doesn’t need to be there, you shouldn’t have put them in the story in the first place.

You can fix this in a few ways. One is to give your character some function before killing them off, but that, too, has become a bit of a cliché. The second is to economize the number of characters you have, so that none of them are extraneous.

A rule of thumb I like to use is, if you can’t remember the name of the character if you tried, you won’t mind their death at all.

In conclusion,

Think about it before you decide you're done with that character. It's better than rewriting half a book because you realized you're out of characters (I have done that).

Those are my thoughts on the topic, but do tell me what you think in the comments.

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

See you next time!

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