kaptk2
kaptk2

Reputation: 341

Decrypt File Made With PHP openssl_encrypt on Command Line

I have a string to encyrpt:

$encryptThis = "Super Secret Text";
echo openssl_encrypt($encryptThis, 'aes-128-cbc', '1234', FALSE, 'F68A9A229A516752');

I then wget get the that php file and end up with what looks like a base64 string of characters.

When I try to decode it on the command line I get "error reading input file". The command I am using to try and decrypt is:

openssl aes-128-cbc -a -iv F68A9A229A516752 -d -in encrypted.txt -out decypted.txt

encrypted.txt is the file name that I saved the file I wget'ed to. What am I missing?

Upvotes: 2

Views: 3486

Answers (3)

kkeller
kkeller

Reputation: 3247

I guess in your encrypted and base64 encoded file, you have long lines, possibly even everything on a single line. Insert a linefeed after every 64 characters and then openssl should be able to decode it.

Openssl (at least the versions I tested) does not properly base64 decode files with lines longer than 64 characters.

Upvotes: 0

kaptk2
kaptk2

Reputation: 341

I finally ended up just making a shell script that is called using php exec(). I never could get PHP built in functions to decode on the command line.

Upvotes: 2

indiv
indiv

Reputation: 17856

The file encrypted.txt is not in the correct format for the command line OpenSSL. It is expecting your data to begin with the string Salted__, followed by a salt. Your file does not have this format, and therefore OpenSSL prints "error reading input file" (from apps/enc.c in the OpenSSL source code).

Note that in a Base64 file, the header is not the plain text Salted__, but is rather U2FsdGVkX1.

So do cat encrypted.txt from the command line and see what it contains.

(I do not know whether PHP's openssl_encrypt is supposed to produce output in this format, but I would assume so if it's just a thin wrapper around OpenSSL's command-line utility.)

Upvotes: 2

Related Questions