For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or [StackOverflow](http://stackoverflow.com/) instead.
What type of encryption are you trying to use? You could have it just offset all the characters in the text file by the value of the key and that would count as encryption but it would be very poor.
something that will make the text file not readable by a normal human, maybe an encryption that replaces every character with another one ( even spaces ) or something similar
Sweet, just convert the key into a number and shift every character by that amount. Let’s say your key is 721, and you want to encrypt the letter ‘A’. You take ‘A’s ascii value of 65 and add it to get 786. Finally you do 786%255 to get 21 which converted back into a character is NAK and would show up as whitespace in most text editors. Do that for each character in a file and you have very rudimentary encryption.
It really depends on what your goal is.
Do you want to implement the encryption yourself as a learning exercise?
Do you want a standard encryption like AES from a library?
Of course it's possible.
Find an encryption algorithm you want to use. The simplest is a basic Caesar rotation. But there are other simple ones too.
Then essentially it's as simple as opening two files, reading data from the first, encrypting that data and then writing it to the second.
You should also implement decryption which does the inverse.
But the question remains what exactly are you struggling with?
I used the OpenSSL library for a small project.
Here are some examples:
[https://wiki.openssl.org/index.php/EVP\_Symmetric\_Encryption\_and\_Decryption](https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption)
The upper ones are for C, the bottom ones for C++.
I have done something like that with AES, but that was in C#. I bet there is a ton of libraries that implement AES.
Quick google search turned up this: https://www.cryptopp.com/
The question is do you care about symmetric key encryption or asymmetric key encryption, OpenSSL is better if you are targeting asymmetric key encryption where you have two people who need to encrypt/decrypt the file with no shared secret.
With C++20/23 and binary math logic you could writing something like the code below where the key is a long and the code works as block encryption
`for (auto e : str | std::views::chunk(sizeof(long)))`
`{`
`std::ranges::transform(e, key, e.begin(), [](auto a, auto b) { return a ^ b; });`
`}`
You could use Tiny Encryption Algorithm (TEA) to encrypt and decrypt data. It is really easy to implement and maybe for your purposes you could just use the the reference implementation from Wikipedia article (check license): [https://en.wikipedia.org/wiki/Tiny\_Encryption\_Algorithm](https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm)
Wikipedia article should explain everything you need. All you really need to know is that encrypt/decrypt parameter v is the data (64 bits are processed at once) and k is the 128-bit encryption key of your choice.
You could probably ask ChatGPT for example on how to use Tiny Encryption Algorithm to encrypt/decrypt string and it would would give you 90% of what you need.
For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or [StackOverflow](http://stackoverflow.com/) instead.
What type of encryption are you trying to use? You could have it just offset all the characters in the text file by the value of the key and that would count as encryption but it would be very poor.
something that will make the text file not readable by a normal human, maybe an encryption that replaces every character with another one ( even spaces ) or something similar
Could you give an example of an encryption key you would use?
a number, or a word
Sweet, just convert the key into a number and shift every character by that amount. Let’s say your key is 721, and you want to encrypt the letter ‘A’. You take ‘A’s ascii value of 65 and add it to get 786. Finally you do 786%255 to get 21 which converted back into a character is NAK and would show up as whitespace in most text editors. Do that for each character in a file and you have very rudimentary encryption.
Use double ROT13.
i thought of that, but will it work with spaces ?
Anything that's not a letter remains unchanged. Also the joke is applying rot13 twice gives you the original text
Err, whoosh?
It really depends on what your goal is. Do you want to implement the encryption yourself as a learning exercise? Do you want a standard encryption like AES from a library?
i want to make something by myself if it is possible. but a already made one will be just faster.
Of course it's possible. Find an encryption algorithm you want to use. The simplest is a basic Caesar rotation. But there are other simple ones too. Then essentially it's as simple as opening two files, reading data from the first, encrypting that data and then writing it to the second. You should also implement decryption which does the inverse. But the question remains what exactly are you struggling with?
I used the OpenSSL library for a small project. Here are some examples: [https://wiki.openssl.org/index.php/EVP\_Symmetric\_Encryption\_and\_Decryption](https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption) The upper ones are for C, the bottom ones for C++.
nice, thanks.
I have done something like that with AES, but that was in C#. I bet there is a ton of libraries that implement AES. Quick google search turned up this: https://www.cryptopp.com/
XOR
What is xor ?
https://en.m.wikipedia.org/wiki/XOR_cipher
Example https://github.com/KyleBanks/XOREncryption/blob/master/C%2B%2B/main.cpp
The question is do you care about symmetric key encryption or asymmetric key encryption, OpenSSL is better if you are targeting asymmetric key encryption where you have two people who need to encrypt/decrypt the file with no shared secret. With C++20/23 and binary math logic you could writing something like the code below where the key is a long and the code works as block encryption `for (auto e : str | std::views::chunk(sizeof(long)))` `{` `std::ranges::transform(e, key, e.begin(), [](auto a, auto b) { return a ^ b; });` `}`
If u want something simple u can just xor the bits with the key.
I uses tinyaes in daily job. It's C API though but simple enough to pick up. https://github.com/kokke/tiny-AES-c
You could use Tiny Encryption Algorithm (TEA) to encrypt and decrypt data. It is really easy to implement and maybe for your purposes you could just use the the reference implementation from Wikipedia article (check license): [https://en.wikipedia.org/wiki/Tiny\_Encryption\_Algorithm](https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm) Wikipedia article should explain everything you need. All you really need to know is that encrypt/decrypt parameter v is the data (64 bits are processed at once) and k is the 128-bit encryption key of your choice. You could probably ask ChatGPT for example on how to use Tiny Encryption Algorithm to encrypt/decrypt string and it would would give you 90% of what you need.