OpenSSL encrypt use of -K -Kfile and Pass: switches

Hi All,

Can someone explain the difference between the use of these switches on an OpenSSL encryption?  

I am encrypting a large file, using a random key and then encrypting that key, using the public key from the recipients Certificate.  Here's an example Batch file:

::Generate encryption Key
openssl rand -base64 32 > Key.bin
:: Encrypt Payload Zip File
openssl enc -aes-256-ecb -kfile key.bin -in -out xxx_Payload
::Encrypt Key used above with Public certificate
openssl rsautl -encrypt -inkey certkey.pem -pubin -in
key.bin -out KeyB_Key

I have also tried this using the -Pass file:key.bin  switch.  

In both cases my submission to a partner has been rejected.  In examples they provide they use -K and a hex string on the encryption.

My question is, what's the difference between what I'm doing and what their example is?  Also, if I use a hex string, how do I then encrypt that?  Does it have to be converted back to characters prior to encryption?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

btanExec ConsultantCommented:
rand - generate pseudo-random data and in your case, a 32 bytes random data generated. e.g.
$ openssl rand -base64 12
$ openssl rand -base64 6

Under "openssl enc", different meaning for option -pass, -k, -kfile, and -K, you can catch reference @

Specifically, if you see option "-K", it stated
-K key
the actual key to use: this must be represented as a string comprised only of hex digits. If only the key is specified, the IV must additionally specified using the -iv option. When both a key and a password are specified, the key given with the -K option will be used and the IV generated from the password will be taken. It probably does not make much sense to specify both key and password.
In other word, I am suspecting the aes key string used in your partner case is likely in hex digit which is different from base64. Instead, you may want to try
e.g.  openssl rand -hex 32 (instead of -base64)
....also do make sure with partner if they are also using the option "-aes-256-ecb" as it is different from "-aes-256-cbc" in the type cipher block for those encrypt and decrypt operations

Reference for "openssl rand" @

For info - I see a full use case though using encrypt and decrypt via rand -base64 handy @
Firebladeboy1993Author Commented:
Hi Btan. Thanks for your help (again!) with this.

OK, so I can use a hex key, no problem, but how do I then encrypt that using the public key of the recipient? Do I need to convert it back to text first?  If so, whats the best way to do that using windows command line or batch file?
btanExec ConsultantCommented:
If I suspect it right, you can still use -kfile as you are doing to encrypt the 32 bytes in hex. But the key size is doubled once in hex forme. So your key size is actually 64 and not binary form but character text form. In fact, I personally see it not as secure as bunary form even though longer, you can generate 64 length binary if that is their intent.

Regardless, the remaining should still remain to follow through as it is..i did not test that though.

But maybe good to confirm with your partner about the content prior to whether there is any encoding like base64 in form before encryption etc.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Firebladeboy1993Author Commented:
So you think this would work:

::Generate encryption Key
openssl rand -hex  32 > Key
:: Encrypt Payload Zip File
openssl enc -aes-256-ecb -kfile key -in -out xxx_Payload
::Encrypt Key used above with Public certificate
openssl rsautl -encrypt -inkey certkey.pem -pubin -in
key -out KeyB_Key

btanExec ConsultantCommented:
Actually AES supports  a key size of 128, 192, or 256 bits. Meaning maximum is 32 bytes for 256 bits. So if we used 64 hex character, that will be 512 bits (64*8). So we may need to use 16 instead of 32 for rand since aes-256-ecb requires 256 bits (32 bytes) for key size- as below (pardon me as I did not test)
e.g  openssl rand -hex  16 > key.hex
e.g. openssl enc -aes-256-ecb -kfile key.hex -in -out xxx_Payload
e.g. openssl rsautl -encrypt -inkey certkey.pem -pubin -in key.hex -out KeyB_Key

hopefully this make sense...
Firebladeboy1993Author Commented:
Hmmm....I'm reading the link you sent me and the it specifically says the -kfile switch indicates that a PASSWORD is contained in the file, not the actual key.   A lowercase -k switch also indicates a password but an uppercase -K switch indicates an actual key.  So it would seem I need to pass a hex string with -K but then encrypt that.
btanExec ConsultantCommented:
Yap as mentioned, guess I also "missed" it, as the password can then remains as 32 bytes then ... but will be best you clarify with partner on this. To me the password is more for the pkcs#5 generation of key stream (with salt normally) to be used for encrypt/decrypt. Indeed, keystream should remain as binary - by the way use of rand is not as secure too...extraction extract to make sense and be consistent
-k password, -kfile filename
Both option are used to specify a password or a file containing the password which is used for key derivation. However they are deprecated. You should use the -pass option instead. The equivalents are -pass pass:password and -pass: pass:filename respectively.
-K key
This option allows you to set the key used for encryption or decryption. This is the key directly used by the cipher algorithm. If no key is given OpenSSL will derive it from a password.
-pass arg
This specifies the password source. Possible values for arg are pass:password or pass:filename, where password is your password and filename file containing the password.
Firebladeboy1993Author Commented:
Thanks for your help with this Btan. In the end I managed to solve the problem by generating the key in Hex and using that with the -K switch to encrypt the file.  THEN I converted from hex to Text using a free utility and encrypted that with the public key of the recipient.  And that worked!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
btanExec ConsultantCommented:
thanks for sharing
Firebladeboy1993Author Commented:
While the Expert (Btan) pointed me in the right direction on this, I finally solved the problem myself.  I think a 250 point allocation is appropriate therefore.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.