I was given the problem to write a class called block cipher to take in a text file and output the same text file after it has been encrypted. I have to use this header file...

#include <iostream>
using namespace std;

class block_cipher 
{
 public:
  block_cipher(); // Constructor
  void finish();
  void output(char x);
 private:
  char c1;
  char c2;
  char c3;
  short count;
  void encrypt();
};

Here is my .cc file...

#include <iostream>
#include "block_cipher.h"

block_cipher::block_cipher()
{
}

void block_cipher::finish()
{
}

void block_cipher::output(char x)
{
  c1 = x;  
}

void block_cipher::encrypt()
{
  char temp1;
  temp1 = c1;
  c1 = c2;
  c2 = c3;
  c3 = temp1;
}

I don't really understand how I can bring in one character at a time and save them as c1, c2, and c3 only using one parameter in output.

Consider how a block cipher works. In this case, you need three inputs to apply the encryption.

I presume (and hope) that your professor has covered a block cipher in class. As you haven't described the cipher here, I'll just pretend that you know how to use it.

So, you have three slots that need to be filled before the encryption can be done. Your professor has named them c1, c2, and c3. Your professor has also provided you with a way to keep count of how many slots are filled.

The plaintext is output one character at a time. Every output() should fill the next slot. Once the slots are filled, encrypt them, output the result, and clear the slots for use again.

The finish() should take care of the case that there are only one or two slots filled when you run out of plaintext to output(). Therefore, finish() should supply the appropriate values for the unfilled slots and encrypt and output as usual. What should finish() do if there are zero slots filled when it is called? Think about it.

Hope this helps.

Ok, I guess what my new question is, when you say "slot" what do you mean? I understand how to manipulate the text, and I could do this program easily without working with a class, I just do not know how to store the plaintext in to the output.

In case this helps, here is my main.cc

#include <iostream>
#include "block_cipher.h"
#include <fstream>
#include <string>

using namespace std;

int main()
{
  ifstream ins;
  long begin,end,length;
  string line;
  {
    ins.open("in_text.dat", ios::in);
    if (ins.fail())
      {
	cerr << "Failed to open in_text.txt.\n";
	exit(1);
      }
  }  
  begin = ins.tellg();
  ins.seekg(0, ios::end);
  end = ins.tellg();
  length = ((end - begin)-1);
  ins.close();
  ins.open("in_text.dat", ios::in);
  getline(ins, line);
  cout << "Line = " << line << endl;
  cout << "Length = " << length << endl;
  cout << "Line position 0-2 :" << line[0] << line[1] << line[2] << endl;
  
  block_cipher cb;
  
  for (int i = 0; i < length; i++)
  {
    cb.output(line[i]);
  }

  cout << "Line now = " << line << endl;
  return (0);
}

How many bits fit in a byte? How many bits to you need for a block to encrypt?

[ p1 ][ p2 ][ p3 ]

Each 'slot' or 'bin' or 'byte' holds a character of the plaintext.

For example:

Imagination is more important than knowledge... --Albert Einstein
123123123123...

Hope this helps.

Accidental post, sorry. See next post.

Ok I understand better how the encryption actually works. Position one goes to three, position three goes to two, and position two goes to one.

char temp1;
temp1 = c1;
c1 = c3;
c3 = c2;
c2 = temp1;

I need to create a loop that brings in 1 plaintext at a time. Each time I bring one in count goes to count++, when count equals 3 call encrypt() from inside output(char x).
I have a loop in the main to bring in 1 index of a string at a time, but I don't know how to store them in variables inside output, only bringing in 1 at a time.

output(char x) brings in line, but if output only has 1 param, wouldn't it get overwritten each time i bring in a new element of line? Or am I way overthinking this whole thing?

The loop belongs in main(), and each time through the loop you need to call output() once.

In the output() method there should not be any loops. I think the assignment would have been more clear had your professor named c1, c2, and c3 as unsigned char c[ 3 ]; So inside output() you can set c[ count ] = x; and count++; Then, if count > 2 you need to encrypt the c[0], c[1], and c[2] (or, as your professor has named them, c1, c2, and c3, and reset count to 0.

Hope this helps.

commented: Extremely patient and helpful. Thanks again. +1

Oh so the char x should be an array. That makes so much sense. Thank you so much I got it now. You were very helpful Duoas. =)

Uh, no. The char x is just a char, not an array of char.

If it is not an array then how can it have an idex " c[0], c[1], c[2]?

c[] is an array.
x is not.

Oh, where did the c[] come from, initialize it in output(char x)

block_cipher::output(char x)
{
char c[];
....
}


That what you mean?

Please pay attention.

I said that your professor should have given you an array of three unsigned chars: unsigned char c[ 3 ]; What he actually gave you was three distinct chars: char c1, c2, c3; In both cases, you have three chars that you need to fill before you can use E(k). Everytime the user uses output(plaintext[current_index]); then the output() method should place x's value into one of c1, c2, or c3. Which one it is depends on count.

Think about it:

c[0]   c[1]   c[2]
c1     c2     c3       <-- one of these gets the value in [B]x[/B]

If count is zero, then c1 gets the value.
If count is one, then c2 gets the value.
Et cetera.

So, what do you do when all the c's have values (that is, what do you do when count becomes three)?

Then you call block_cipher::encrypt() and return the three chars. if i reach end of file before count = 3, then call block_cipher::finish(). Right?

Exactly.

You should actually call finish() no matter what. But it only needs to do something if count is 1 or 2.

Thanks, You have been very helpful, and sorry that I have been a little dense. Thanks for helping.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.