[cryptopp-users] Re: How to encrypt string and write to txt
The thing is operator<< is used for formatted text output, not binary. https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt https://en.cppreference.com/w/cpp/named_req/FormattedOutputFunction There are read/write functions to deal with binary files. https://en.cppreference.com/w/cpp/io/basic_ostream/write Since you get binary data after encryption, you must open file in binary mode and use write() to put binary data to file. FileSink with binary=true does exactly what you need. If you still want to play with text, look at HexEncoder & HexDecoder filters https://cryptopp.com/wiki/HexDecoder среда, 30 ноября 2022 г. в 03:19:24 UTC+7, boomer1...@gmail.com: > Ok nevermind i figured it out. For anyone in the future having the same > problem, here is a stackoverflow post solving my situation: > https://stackoverflow.com/questions/30633594/c-convert-hexadecimal-string-with-to-original-binary-string > > .Thanks a lot for the answers, the format thing you said helped a lot. > > On Tuesday, November 29, 2022 at 8:35:16 PM UTC+2 Nikos Karavidas wrote: > >> Ok so i got this figured out with this: >> >> stringstream ss; >> >> for (int i = 0; i < ciphertext.size(); ++i) >> { >> ss << hex << (0xFF & static_cast(ciphertext[i])) << " "; >> } >> >> string mystr = ss.str(); >> >> cout << "mystr: " << mystr << endl; >> >> >> and it works fine, all characters are printed in hex and i can write them >> in a txt file, no problem. BUT when i read them from the txt file, i dont >> know how to convert them back from hex, so i can do the decryption. >> On Tuesday, November 29, 2022 at 8:23:19 PM UTC+2 Nikos Karavidas wrote: >> >>> I 've got the following function: >>> void encrypt(string ivInString, string keyInString, const string >>> _in, const string _out) >>> { >>> SecByteBlock iv(reinterpret_cast([0]), >>> ivInString.size()); >>> SecByteBlock key(reinterpret_cast([0]), >>> keyInString.size()); >>> >>> >>> CFB_Mode::Encryption e; >>> e.SetKeyWithIV(key.data(), key.size(), iv.data()); >>> >>> ifstream in{ filename_in, ios::binary }; >>> ofstream out{ filename_out, ios::binary }; >>> >>> FileSource f{ in, /*pumpAll=*/true, new StreamTransformationFilter{ >>> e, new FileSink{out} } }; >>> } >>> >>> and a similar for decrypting, and they work fine, but this is to encrypt >>> any sort of file, which is my only way of encrypting the txt, but its not >>> what i need. What i need is to encrypt a string. The 1st answer in this >>> link: >>> https://stackoverflow.com/questions/12306956/example-of-aes-using-crypto >>> , prints all the ciphered text in hex in the line that says "cout << "0x" >>> << hex << (0xFF & static_cast(ciphertext[i])) << " ";". But im not >>> sure how that hex part even works. Do you know how i can write that into a >>> txt file? >>> >>> >>> >>> On Tuesday, November 29, 2022 at 7:43:08 PM UTC+2 schmid...@gmail.com >>> wrote: >>> Could you try to write a minimal working example in a separate project and share the code if you still have any issues? Try to write that encryption/decryption routines yourself line by line instead of copy any examples. The idea is simple: you read data source, you put data through the filter (which does the encryption or decryption) and put the result to the sink. So you should setup a source: read file in buffer and use StringSource or VectorSource or use FileSource. Then you should prepare an encryption filter (StreamTransformationFilter) with appropriate options (you could learn it from example code). Then you put the data into the Sink. https://www.cryptopp.com/wiki/FileSource Remember to use same key & iv for decryption. Also binary/text mode is important when reading files. Also remember to use same mode (CBC, ECB, etc.) for both encryption/decryption algorithm. Use authenticated encryption to catch message checksum errors. Try to experiment with pipelining, e.g. calculating messag digest and printing it in hex. When you get the strong feeling what works and what isn't you could easily use any encryption/decryption algorithm in Cryptopp. Just take some time to read wiki and experiment with library primitives. And good luck with that! https://www.cryptopp.com/wiki/Pipelining вторник, 29 ноября 2022 г. в 23:01:33 UTC+7, boomer1...@gmail.com: > Not sure what you mean by format, i just use something like > "myFileStream << cipher", and then read it back again. Do you mean that > the > txt file should be utf-8 or something like that? What would be the right > way to write the ciphered string to the txt file and read it afterwards? > Thanks a lot for answering btw. > > On Tuesday, November 29, 2022 at 4:15:37 PM UTC+2 creyesc...@gmail.com > wrote: > >> I think I have an idea of what is
[cryptopp-users] Re: How to encrypt string and write to txt
Ok nevermind i figured it out. For anyone in the future having the same problem, here is a stackoverflow post solving my situation: https://stackoverflow.com/questions/30633594/c-convert-hexadecimal-string-with-to-original-binary-string .Thanks a lot for the answers, the format thing you said helped a lot. On Tuesday, November 29, 2022 at 8:35:16 PM UTC+2 Nikos Karavidas wrote: > Ok so i got this figured out with this: > > stringstream ss; > > for (int i = 0; i < ciphertext.size(); ++i) > { > ss << hex << (0xFF & static_cast(ciphertext[i])) << " "; > } > > string mystr = ss.str(); > > cout << "mystr: " << mystr << endl; > > > and it works fine, all characters are printed in hex and i can write them > in a txt file, no problem. BUT when i read them from the txt file, i dont > know how to convert them back from hex, so i can do the decryption. > On Tuesday, November 29, 2022 at 8:23:19 PM UTC+2 Nikos Karavidas wrote: > >> I 've got the following function: >> void encrypt(string ivInString, string keyInString, const string >> _in, const string _out) >> { >> SecByteBlock iv(reinterpret_cast([0]), >> ivInString.size()); >> SecByteBlock key(reinterpret_cast([0]), >> keyInString.size()); >> >> >> CFB_Mode::Encryption e; >> e.SetKeyWithIV(key.data(), key.size(), iv.data()); >> >> ifstream in{ filename_in, ios::binary }; >> ofstream out{ filename_out, ios::binary }; >> >> FileSource f{ in, /*pumpAll=*/true, new StreamTransformationFilter{ >> e, new FileSink{out} } }; >> } >> >> and a similar for decrypting, and they work fine, but this is to encrypt >> any sort of file, which is my only way of encrypting the txt, but its not >> what i need. What i need is to encrypt a string. The 1st answer in this >> link: >> https://stackoverflow.com/questions/12306956/example-of-aes-using-crypto >> , prints all the ciphered text in hex in the line that says "cout << "0x" >> << hex << (0xFF & static_cast(ciphertext[i])) << " ";". But im not >> sure how that hex part even works. Do you know how i can write that into a >> txt file? >> >> >> >> On Tuesday, November 29, 2022 at 7:43:08 PM UTC+2 schmid...@gmail.com >> wrote: >> >>> Could you try to write a minimal working example in a separate project >>> and share the code if you still have any issues? Try to write that >>> encryption/decryption routines yourself line by line instead of copy >>> any examples. >>> >>> The idea is simple: you read data source, you put data through the >>> filter (which does the encryption or decryption) and put the result to the >>> sink. So you should setup a source: read file in buffer and use >>> StringSource or VectorSource or use FileSource. Then you should prepare an >>> encryption filter (StreamTransformationFilter) with appropriate options >>> (you could learn it from example code). Then you put the data into the Sink. >>> https://www.cryptopp.com/wiki/FileSource >>> >>> Remember to use same key & iv for decryption. Also binary/text mode is >>> important when reading files. Also remember to use same mode (CBC, ECB, >>> etc.) for both encryption/decryption algorithm. Use authenticated >>> encryption to catch message checksum errors. >>> >>> Try to experiment with pipelining, e.g. calculating messag digest and >>> printing it in hex. When you get the strong feeling what works and what >>> isn't you could easily use any encryption/decryption algorithm in Cryptopp. >>> Just take some time to read wiki and experiment with library primitives. >>> And good luck with that! >>> https://www.cryptopp.com/wiki/Pipelining >>> >>> вторник, 29 ноября 2022 г. в 23:01:33 UTC+7, boomer1...@gmail.com: >>> Not sure what you mean by format, i just use something like "myFileStream << cipher", and then read it back again. Do you mean that the txt file should be utf-8 or something like that? What would be the right way to write the ciphered string to the txt file and read it afterwards? Thanks a lot for answering btw. On Tuesday, November 29, 2022 at 4:15:37 PM UTC+2 creyesc...@gmail.com wrote: > I think I have an idea of what is happening to you, what happens > that you must decrypt in the same format that you encrypted, since that > happened to me at some point, you must also see the format in how you > return the txt file, or the text of txt if it is the same format that is > needed to decrypt it. Greetings. > > El lunes, 28 de noviembre de 2022 a las 14:30:04 UTC-5, > boomer1...@gmail.com escribió: > >> Ok so based on the example from this link ( >> https://www.cryptopp.com/wiki/Advanced_Encryption_Standard),i >> have tried encrypting a "plain" string into a "cipher" string and >> writing >> that "cipher" string to a .txt file in one program, and in another >> program >> afterwards reading that same .txt file into a string and
[cryptopp-users] Re: How to encrypt string and write to txt
Ok so i got this figured out with this: stringstream ss; for (int i = 0; i < ciphertext.size(); ++i) { ss << hex << (0xFF & static_cast(ciphertext[i])) << " "; } string mystr = ss.str(); cout << "mystr: " << mystr << endl; and it works fine, all characters are printed in hex and i can write them in a txt file, no problem. BUT when i read them from the txt file, i dont know how to convert them back from hex, so i can do the decryption. On Tuesday, November 29, 2022 at 8:23:19 PM UTC+2 Nikos Karavidas wrote: > I 've got the following function: > void encrypt(string ivInString, string keyInString, const string > _in, const string _out) > { > SecByteBlock iv(reinterpret_cast([0]), > ivInString.size()); > SecByteBlock key(reinterpret_cast([0]), > keyInString.size()); > > > CFB_Mode::Encryption e; > e.SetKeyWithIV(key.data(), key.size(), iv.data()); > > ifstream in{ filename_in, ios::binary }; > ofstream out{ filename_out, ios::binary }; > > FileSource f{ in, /*pumpAll=*/true, new StreamTransformationFilter{ e, > new FileSink{out} } }; > } > > and a similar for decrypting, and they work fine, but this is to encrypt > any sort of file, which is my only way of encrypting the txt, but its not > what i need. What i need is to encrypt a string. The 1st answer in this > link: > https://stackoverflow.com/questions/12306956/example-of-aes-using-crypto > , prints all the ciphered text in hex in the line that says "cout << "0x" > << hex << (0xFF & static_cast(ciphertext[i])) << " ";". But im not > sure how that hex part even works. Do you know how i can write that into a > txt file? > > > > On Tuesday, November 29, 2022 at 7:43:08 PM UTC+2 schmid...@gmail.com > wrote: > >> Could you try to write a minimal working example in a separate project >> and share the code if you still have any issues? Try to write that >> encryption/decryption routines yourself line by line instead of copy >> any examples. >> >> The idea is simple: you read data source, you put data through the filter >> (which does the encryption or decryption) and put the result to the sink. >> So you should setup a source: read file in buffer and use StringSource or >> VectorSource or use FileSource. Then you should prepare an encryption >> filter (StreamTransformationFilter) with appropriate options (you could >> learn it from example code). Then you put the data into the Sink. >> https://www.cryptopp.com/wiki/FileSource >> >> Remember to use same key & iv for decryption. Also binary/text mode is >> important when reading files. Also remember to use same mode (CBC, ECB, >> etc.) for both encryption/decryption algorithm. Use authenticated >> encryption to catch message checksum errors. >> >> Try to experiment with pipelining, e.g. calculating messag digest and >> printing it in hex. When you get the strong feeling what works and what >> isn't you could easily use any encryption/decryption algorithm in Cryptopp. >> Just take some time to read wiki and experiment with library primitives. >> And good luck with that! >> https://www.cryptopp.com/wiki/Pipelining >> >> вторник, 29 ноября 2022 г. в 23:01:33 UTC+7, boomer1...@gmail.com: >> >>> Not sure what you mean by format, i just use something like >>> "myFileStream << cipher", and then read it back again. Do you mean that the >>> txt file should be utf-8 or something like that? What would be the right >>> way to write the ciphered string to the txt file and read it afterwards? >>> Thanks a lot for answering btw. >>> >>> On Tuesday, November 29, 2022 at 4:15:37 PM UTC+2 creyesc...@gmail.com >>> wrote: >>> I think I have an idea of what is happening to you, what happens that you must decrypt in the same format that you encrypted, since that happened to me at some point, you must also see the format in how you return the txt file, or the text of txt if it is the same format that is needed to decrypt it. Greetings. El lunes, 28 de noviembre de 2022 a las 14:30:04 UTC-5, boomer1...@gmail.com escribió: > Ok so based on the example from this link ( > https://www.cryptopp.com/wiki/Advanced_Encryption_Standard),i > have tried encrypting a "plain" string into a "cipher" string and writing > that "cipher" string to a .txt file in one program, and in another > program > afterwards reading that same .txt file into a string and decrypting it. > But > when i cout that "recovered" string, it is just random characters and > not > my original "plain" string. I think the problem starts when i attempt to > get the "cipher" string in the .txt file, but not sure. Any help is > greatly > appreciated. Thanks in advance. -- You received this message because you are subscribed to the Google Groups "Crypto++ Users" group. To unsubscribe from this group and stop receiving emails from it, send
[cryptopp-users] Re: How to encrypt string and write to txt
I 've got the following function: void encrypt(string ivInString, string keyInString, const string _in, const string _out) { SecByteBlock iv(reinterpret_cast([0]), ivInString.size()); SecByteBlock key(reinterpret_cast([0]), keyInString.size()); CFB_Mode::Encryption e; e.SetKeyWithIV(key.data(), key.size(), iv.data()); ifstream in{ filename_in, ios::binary }; ofstream out{ filename_out, ios::binary }; FileSource f{ in, /*pumpAll=*/true, new StreamTransformationFilter{ e, new FileSink{out} } }; } and a similar for decrypting, and they work fine, but this is to encrypt any sort of file, which is my only way of encrypting the txt, but its not what i need. What i need is to encrypt a string. The 1st answer in this link: https://stackoverflow.com/questions/12306956/example-of-aes-using-crypto , prints all the ciphered text in hex in the line that says "cout << "0x" << hex << (0xFF & static_cast(ciphertext[i])) << " ";". But im not sure how that hex part even works. Do you know how i can write that into a txt file? On Tuesday, November 29, 2022 at 7:43:08 PM UTC+2 schmid...@gmail.com wrote: > Could you try to write a minimal working example in a separate project and > share the code if you still have any issues? Try to write that > encryption/decryption routines yourself line by line instead of copy > any examples. > > The idea is simple: you read data source, you put data through the filter > (which does the encryption or decryption) and put the result to the sink. > So you should setup a source: read file in buffer and use StringSource or > VectorSource or use FileSource. Then you should prepare an encryption > filter (StreamTransformationFilter) with appropriate options (you could > learn it from example code). Then you put the data into the Sink. > https://www.cryptopp.com/wiki/FileSource > > Remember to use same key & iv for decryption. Also binary/text mode is > important when reading files. Also remember to use same mode (CBC, ECB, > etc.) for both encryption/decryption algorithm. Use authenticated > encryption to catch message checksum errors. > > Try to experiment with pipelining, e.g. calculating messag digest and > printing it in hex. When you get the strong feeling what works and what > isn't you could easily use any encryption/decryption algorithm in Cryptopp. > Just take some time to read wiki and experiment with library primitives. > And good luck with that! > https://www.cryptopp.com/wiki/Pipelining > > вторник, 29 ноября 2022 г. в 23:01:33 UTC+7, boomer1...@gmail.com: > >> Not sure what you mean by format, i just use something like "myFileStream >> << cipher", and then read it back again. Do you mean that the txt file >> should be utf-8 or something like that? What would be the right way to >> write the ciphered string to the txt file and read it afterwards? Thanks a >> lot for answering btw. >> >> On Tuesday, November 29, 2022 at 4:15:37 PM UTC+2 creyesc...@gmail.com >> wrote: >> >>> I think I have an idea of what is happening to you, what happens that >>> you must decrypt in the same format that you encrypted, since that happened >>> to me at some point, you must also see the format in how you return the txt >>> file, or the text of txt if it is the same format that is needed to decrypt >>> it. Greetings. >>> >>> El lunes, 28 de noviembre de 2022 a las 14:30:04 UTC-5, >>> boomer1...@gmail.com escribió: >>> Ok so based on the example from this link ( https://www.cryptopp.com/wiki/Advanced_Encryption_Standard),i have tried encrypting a "plain" string into a "cipher" string and writing that "cipher" string to a .txt file in one program, and in another program afterwards reading that same .txt file into a string and decrypting it. But when i cout that "recovered" string, it is just random characters and not my original "plain" string. I think the problem starts when i attempt to get the "cipher" string in the .txt file, but not sure. Any help is greatly appreciated. Thanks in advance. >>> >>> -- You received this message because you are subscribed to the Google Groups "Crypto++ Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to cryptopp-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/cryptopp-users/7932b579-e858-4783-9834-f4a70f0069b0n%40googlegroups.com.
[cryptopp-users] Re: How to encrypt string and write to txt
Could you try to write a minimal working example in a separate project and share the code if you still have any issues? Try to write that encryption/decryption routines yourself line by line instead of copy any examples. The idea is simple: you read data source, you put data through the filter (which does the encryption or decryption) and put the result to the sink. So you should setup a source: read file in buffer and use StringSource or VectorSource or use FileSource. Then you should prepare an encryption filter (StreamTransformationFilter) with appropriate options (you could learn it from example code). Then you put the data into the Sink. https://www.cryptopp.com/wiki/FileSource Remember to use same key & iv for decryption. Also binary/text mode is important when reading files. Also remember to use same mode (CBC, ECB, etc.) for both encryption/decryption algorithm. Use authenticated encryption to catch message checksum errors. Try to experiment with pipelining, e.g. calculating messag digest and printing it in hex. When you get the strong feeling what works and what isn't you could easily use any encryption/decryption algorithm in Cryptopp. Just take some time to read wiki and experiment with library primitives. And good luck with that! https://www.cryptopp.com/wiki/Pipelining вторник, 29 ноября 2022 г. в 23:01:33 UTC+7, boomer1...@gmail.com: > Not sure what you mean by format, i just use something like "myFileStream > << cipher", and then read it back again. Do you mean that the txt file > should be utf-8 or something like that? What would be the right way to > write the ciphered string to the txt file and read it afterwards? Thanks a > lot for answering btw. > > On Tuesday, November 29, 2022 at 4:15:37 PM UTC+2 creyesc...@gmail.com > wrote: > >> I think I have an idea of what is happening to you, what happens that >> you must decrypt in the same format that you encrypted, since that happened >> to me at some point, you must also see the format in how you return the txt >> file, or the text of txt if it is the same format that is needed to decrypt >> it. Greetings. >> >> El lunes, 28 de noviembre de 2022 a las 14:30:04 UTC-5, >> boomer1...@gmail.com escribió: >> >>> Ok so based on the example from this link ( >>> https://www.cryptopp.com/wiki/Advanced_Encryption_Standard),i have >>> tried encrypting a "plain" string into a "cipher" string and writing that >>> "cipher" string to a .txt file in one program, and in another program >>> afterwards reading that same .txt file into a string and decrypting it. But >>> when i cout that "recovered" string, it is just random characters and not >>> my original "plain" string. I think the problem starts when i attempt to >>> get the "cipher" string in the .txt file, but not sure. Any help is greatly >>> appreciated. Thanks in advance. >> >> -- You received this message because you are subscribed to the Google Groups "Crypto++ Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to cryptopp-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/cryptopp-users/9137d9f2-9dc2-4504-80e1-a824f5e1b0e3n%40googlegroups.com.
[cryptopp-users] Re: How to encrypt string and write to txt
Not sure what you mean by format, i just use something like "myFileStream << cipher", and then read it back again. Do you mean that the txt file should be utf-8 or something like that? What would be the right way to write the ciphered string to the txt file and read it afterwards? Thanks a lot for answering btw. On Tuesday, November 29, 2022 at 4:15:37 PM UTC+2 creyesc...@gmail.com wrote: > I think I have an idea of what is happening to you, what happens that > you must decrypt in the same format that you encrypted, since that happened > to me at some point, you must also see the format in how you return the txt > file, or the text of txt if it is the same format that is needed to decrypt > it. Greetings. > > El lunes, 28 de noviembre de 2022 a las 14:30:04 UTC-5, > boomer1...@gmail.com escribió: > >> Ok so based on the example from this link ( >> https://www.cryptopp.com/wiki/Advanced_Encryption_Standard),i have >> tried encrypting a "plain" string into a "cipher" string and writing that >> "cipher" string to a .txt file in one program, and in another program >> afterwards reading that same .txt file into a string and decrypting it. But >> when i cout that "recovered" string, it is just random characters and not >> my original "plain" string. I think the problem starts when i attempt to >> get the "cipher" string in the .txt file, but not sure. Any help is greatly >> appreciated. Thanks in advance. > > -- You received this message because you are subscribed to the Google Groups "Crypto++ Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to cryptopp-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/cryptopp-users/fe607b7a-88e4-4249-8fd2-a645a1f0fbf4n%40googlegroups.com.
[cryptopp-users] Re: How to encrypt string and write to txt
I think I have an idea of what is happening to you, what happens that you must decrypt in the same format that you encrypted, since that happened to me at some point, you must also see the format in how you return the txt file, or the text of txt if it is the same format that is needed to decrypt it. Greetings. El lunes, 28 de noviembre de 2022 a las 14:30:04 UTC-5, boomer1...@gmail.com escribió: > Ok so based on the example from this link ( > https://www.cryptopp.com/wiki/Advanced_Encryption_Standard),i have > tried encrypting a "plain" string into a "cipher" string and writing that > "cipher" string to a .txt file in one program, and in another program > afterwards reading that same .txt file into a string and decrypting it. But > when i cout that "recovered" string, it is just random characters and not > my original "plain" string. I think the problem starts when i attempt to > get the "cipher" string in the .txt file, but not sure. Any help is greatly > appreciated. Thanks in advance. -- You received this message because you are subscribed to the Google Groups "Crypto++ Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to cryptopp-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/cryptopp-users/1793b647-9fe0-47e1-93ad-b114297a6b37n%40googlegroups.com.