-1

Here is the updated code with encryption algorithm:

#include<iostream>
#include<string>
#include<fstream>

int main()
{
    char ch;
    char cch[128];
    char pch[128];
    std::string str;
    std::string key = "01100011 01100001 01110100";
    std::fstream pFile;
    std::fstream cFile;

    pFile.open("plaintext.txt", std::ios::in);
    if (pFile.is_open())
    {
        //Use the key to encrypt the plain text.
        for(int i=0; i<str.size(); i++)
            cch[i] = (pch[i] ^ key[i]) % key.size();
    }
    else
    {
        std::cout<<"Error!"<<std::endl;
        pFile.close();
    }
    cFile.open("ciphertext.txt", std::ios::binary | std::ios::out);
        while (pFile.get(ch))
        {
            cFile.put(ch);
        }

    pFile.close();
    cFile.close();

    return 0;
}

I still get the original text in the cipher text file. The problems I am having is I do not know how to get the plain text into binary so I can xor it using the key and I am not exactly sure where to place the encryption algorithm in the code. I also understand what people are saying about str being empty, but I do not know what to do to fix that.

bward
  • 3
  • 3
  • I can't see you reading and/or writing ciphered text to the files in the entire code, you are just opening them and closing them the entire time. – Arsenic Feb 16 '21 at 07:14
  • 1
    Some advice: Don't include `` (https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and don't write `using namespace std;` (https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – mediocrevegetable1 Feb 16 '21 at 07:15
  • `char x = ...` Where is this used after being calculated here? – dxiv Feb 16 '21 at 07:15
  • I also don't see `char c` being used anywhere. – mediocrevegetable1 Feb 16 '21 at 07:16
  • Where do you read from `pFile`? `str` is empty – Alan Birtles Feb 16 '21 at 07:30
  • I need to read the whole file, not just a string. And I need to write the whole plaintext in binary into the cipher text. I thought that was what I was doing, but I now understand what you all are saying, I just do not know how to do that. Thank you, I will go and fix what everyone said. – bward Feb 16 '21 at 21:09
  • @bward `I just have no idea how to do that with files.` But you don't have to do that with files, you have to do it to the characters in the file. There are three tasks here, reading characters from a file, encrypting characters, writing characters to a file. Each requires a separate piece of code. This is fundamental to programming, you split a complex task into simpler tasks. Now, of those three tasks which are you stuck with? The code above seems to be reading and writing characters successfully. – john Feb 17 '21 at 08:36
  • @john Okay, I see what you are saying, thank you! I am stuck on encrypting the characters. I have to encrypt the file by using xor with a key and put the encrypted file in binary or hex. – bward Feb 17 '21 at 16:04
  • @bward I'm still a little unsure. Is the problem that you know what you have to do but you don;t know how to do it. Or is the problem, you don't really understand what you've been asked to do. Can you describe in detail what `xor with a key` means in this case? – john Feb 17 '21 at 16:09
  • @bward For example looking at the formula above `cch = pch ^ k[i % k.size()];`, `cch` is the encrypted character, `pch` is the plain character, `k` is the key and `i` presumably is the index of the character in the file. If that's right then don't you have your solution? What is stopping you applying that formula to the code you have? – john Feb 17 '21 at 16:15
  • @John I know what I am supposed to do, I just do not know how to do it. By saying XOR with a key I mean in order to encrypt the characters in the plain text file with the key I have to use XOR (^). I do not know how to go about putting that in the code. And I have tried the formula, but for some reason it would not work. I will try again though. Thank you! – bward Feb 17 '21 at 20:01
  • @bward Well to help further I'd have to see your code using the formula, and what happened with that code. When you ask questions on SO you'll get a better response if you show failed efforts. I can understand why people are reluctant to show these but if you are trying to figure out what someone doesn't understand it really helps to see exactly what they fried. – john Feb 18 '21 at 07:58
  • @John Thank you, the code has been updated. – bward Feb 18 '21 at 10:00
  • @bward OK, now I can see the problem. I'll work on an answer. – john Feb 18 '21 at 10:23

1 Answers1

0

So the problem is the structure of your code. You have some file read/writing code which is fine, and you have some encryption code which is more or less fine. But the problem is that you haven't combined these two pieces of code correctly. In fact the way you have written it the two parts of the code are entirely separate. You try to do the encryption first (before you have read anything) and then you do the reading and writing.

You also seem to have made a mistake with the encryption formula, which in your code above is not exactly as you quoted it in the earlier version of this question.

Here's how it should be (in outline)

while character successfully read
    encrypt that character
    write the encrypted character

See how the reading, encryption and writing are combined in a single loop not in completely separate loops as you have it. That in essence is where you were going wrong.

Here's how it might look in actual code (I haven't tested this, so apologies in advance for any mistakes)

std::string key = "01100011 01100001 01110100";
std::fstream pFile;
std::fstream cFile;
pFile.open("plaintext.txt", std::ios::in);
cFile.open("ciphertext.txt", std::ios::binary | std::ios::out);
if (pFile.is_open())
{
    int i = 0;
    char pch;
    while (pFile.get(pch)) // read a plain text character
    {
        // Use the key to encrypt the plain text character.
        char cch = pch ^ key[i % key.size()];
        // write the encrypted character
        cFile.put(cch);
        // update the character index
        ++i;
    }
}
else
{
    std::cout<<"Error!"<<std::endl;
}

Notice how the reading, writing and encryption all happen together one character at a time.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thank you so much! It is working, but is there a way that I could encrypt it into binary? – bward Feb 19 '21 at 18:57
  • @bward Very much depends what you mean by binary. To me the above is encrypting to binary. What are you expecting to be different? – john Feb 19 '21 at 18:59
  • Oh okay, I was just expecting to see it in 0 and 1's. – bward Feb 19 '21 at 19:33
  • So you want to see eight 0 or 1 digits output for each encrypted byte? That's certainly possible although you'd have to do the work to make it happen. It's just another piece of code you have to add to the loop, read char, encrypt char, convert encrypted char to string of binary digits, write binary digit string. – john Feb 19 '21 at 19:45
  • Oh okay, thank you for all of your help. I really do appreciate it. – bward Feb 19 '21 at 21:02