Reputation: 841
I was just experimenting with encryption and decryption and made a function to encrypt, decrypt and write both to a file...
void encrypt(const std::string& r){
std::string new_r = "Encrypted:\n";
for(int i = 0; i < r.length(); i++)
{
new_r += ~r[i];
}
wtofe("/home/programming/Desktop/latin.txt", new_r); // Writes it to a file
decrypt(new_r);
}
void decrypt(const std::string& r){
std::string new_r = "Decrypted:\n";
for(int i = 0; i < r.length(); i++)
{
new_r += ~(r[i]);
}
wtofd("/home/programming/Desktop/latin.txt", new_r); //Writes it to a file
}
Writing to the file and encrypting worked. It also decrypted it but take a look at this strange output :
What I wrote as input was Davlog and as you can see it has been added to the end of the decryption. But why? What did I do wrong?
Upvotes: 1
Views: 101
Reputation: 13541
Try with this:
void encrypt(const std::string& r){
std::string new_r;
for(int i = 0; i < r.length(); i++)
{
new_r += ~r[i];
}
wtofe("/home/programming/Desktop/latin.txt", new_r); // Writes it to a file
decrypt(new_r);
}
void decrypt(const std::string& r){
std::string new_r;
for(int i = 0; i < r.length(); i++)
{
new_r += ~(r[i]);
}
wtofd("/home/programming/Desktop/latin.txt", new_r); //Writes it to a file
}
In your original code you were writing "Encrypted:[your encrypted msg]"
instead of just "[your encrypted msg]"
to your file. So the decrypt step would decrypt the "Encrypted:" part as well as the original encrypted message.
Upvotes: 4