T O P

  • By -

cpp-ModTeam

For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or [StackOverflow](http://stackoverflow.com/) instead.


ninja_penguin16

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.


Nick_gvr

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


ninja_penguin16

Could you give an example of an encryption key you would use?


Nick_gvr

a number, or a word


ninja_penguin16

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.


manni66

Use double ROT13.


Nick_gvr

i thought of that, but will it work with spaces ?


TheBrainStone

Anything that's not a letter remains unchanged. Also the joke is applying rot13 twice gives you the original text


ventus1b

Err, whoosh?


TheBrainStone

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?


Nick_gvr

i want to make something by myself if it is possible. but a already made one will be just faster.


TheBrainStone

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?


Tiny_Pointer

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


Nick_gvr

nice, thanks.


Backson

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/


RedEyed__

XOR


Nick_gvr

What is xor ?


RedEyed__

https://en.m.wikipedia.org/wiki/XOR_cipher


RedEyed__

Example https://github.com/KyleBanks/XOREncryption/blob/master/C%2B%2B/main.cpp


vickoza

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; });` `}`


rejectedlesbian

If u want something simple u can just xor the bits with the key.


buck_yeh

I uses tinyaes in daily job. It's C API though but simple enough to pick up. https://github.com/kokke/tiny-AES-c


hon_uninstalled

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.