openssl encrypt text single line of output
Asked Answered
C

2

5

I am encrypting a json string in openssl. When it prints out I get mutli line out put, I need it in a single line, for storage and usability in CLI.

Run command: echo '{"foo":"this is fun","bar":"this is also fun","numbers":123456}' | openssl enc -aes-256-cbc -a Pass: pass

Get: U2FsdGVkX1+u90MyWMyFtrrlzFSLcuNk00Ax5XLJhzSMpuUNxwdZQgdkxSmT3KEP LMGz5I2imE9RP0BvMAPnUq0we98bprtxAnZMHLSntfl2tRSqoyMDvW6P3+Vkr2jZ

To decrypt I run: echo "U2FsdGVkX1+u90MyWMyFtrrlzFSLcuNk00Ax5XLJhzSMpuUNxwdZQgdkxSmT3KEP LMGz5I2imE9RP0BvMAPnUq0we98bprtxAnZMHLSntfl2tRSqoyMDvW6P3+Vkr2jZ" | openssl enc -aes-256-cbc -d -a

But this command will not work with the multi lines, if I out put to a file and the cat the file in, it works, but I need to store is as a string, in a database, with out the multi lines and to be able to run the decrypt command in CLI.

Does anyone know how this can be done or some open way to encrypt in CLI with aes and to be able to get a single line output?

Compass answered 14/10, 2014 at 16:37 Comment(0)
A
9

You can simply use the -A switch in OpenSSL together with the -a switch to produce a single line. You can do this for both encryption and decryption.

Awfully answered 14/10, 2014 at 20:6 Comment(1)
Thanks for that, I must have over looked it when looking for the option.Compass
M
0

I just want to say thank you @maarten-bodewes! This helped me solve an issue I've been trying to debug for a little while now.

Specifically, in a Github Action I wanted to encrypt a string using openssl and then use the output of that encrypted string in subsequent commands. Unfortunately after the Action would run I kept running into "Bad Decrypt" and couldn't figure out why. Then I figured out openssl has an upper bound character limit on the encrypted string and will split it onto multiple lines in the output, meaning I was only grabbing half of the encrypted string which is why decrypt was failing. The -A argument was added and boom, fixed!

If others have this issue see: https://wiki.openssl.org/index.php/Command_Line_Utilities#Base64_Encoding_Strings

Mccartney answered 15/11, 2022 at 5:59 Comment(1)
As this answer does not differ from the other one in merit, it would be best if you added that as comment, not a separate answer.Gowk

© 2022 - 2024 — McMap. All rights reserved.