LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 05-24-2023, 12:09 PM   #1
banderas20
Member
 
Registered: Aug 2018
Posts: 138

Rep: Reputation: Disabled
Question OpenSSL error : unable to load Private Key


Hi!

I have created a public-private keypair with ssh-keygen and I have both id_rsa and id_rsa.pub.

I'm trying to encrypt/decrypt files with openssl.

If I cypher the file with

Code:
cat plain_file | openssl rsautl -encrypt -pubin -inkey /tmp/id_rsa.pub > /tmp/encrypted.txt
it works.

I try to decypher it with the private key of the remitent user with this command

Code:
cat /tmp/encrypted.txt | openssl rsautl -decrypt -inkey id_rsa
But I get this error:

Code:
unable to load Private Key
139651099592000:error:0909006C:PEM routines:get_name:no start line:../crypto/pem/pem_lib.c:745:Expecting: ANY PRIVATE KEY
After some openssl manipulations, the contents of the private key begin with:

Code:
-----BEGIN RSA PRIVATE KEY-----
and then with
Code:
-----BEGIN OPENSSH PRIVATE KEY-----
By the way, the encryption didn't work at first with the ssh-keygen generated public file. I had to convert it with:

Code:
ssh-keygen -f id_rsa.pub -e -m PKCS8 > id_rsa2.pub
I'm pretty lost and I don't know what's going on.

Can you shed some light on this?

Thanks in advance!

PS: running Ubuntu 20.04

Last edited by banderas20; 05-24-2023 at 12:10 PM.
 
Old 05-24-2023, 12:22 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
What are you trying to accomplish?
 
Old 05-24-2023, 01:00 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by banderas20 View Post
By the way, the encryption didn't work at first with the ssh-keygen generated public file. I had to convert it with:

Code:
ssh-keygen -f id_rsa.pub -e -m PKCS8 > id_rsa2.pub
Did you also convert the private key?
 
Old 05-24-2023, 01:32 PM   #4
banderas20
Member
 
Registered: Aug 2018
Posts: 138

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Did you also convert the private key?
Yes, with

ssh-keygen -f priv_key -e -m PKCS8 > priv_key2

and to my big surprise, priv_key2 seems to have been converted to a Public Key (????), since its contents after the conversion show:

-----BEGIN PUBLIC KEY-----

Quote:
Originally Posted by michaelk View Post
What are you trying to accomplish?
I'm trying to cipher a plain text message with public_key_B and decipher it with private_key_B on the other side.
 
Old 05-24-2023, 03:33 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
It might be easier using gpg.
https://itsfoss.com/gpg-encrypt-files-basic/
 
Old 05-24-2023, 04:50 PM   #6
banderas20
Member
 
Registered: Aug 2018
Posts: 138

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
It might be easier using gpg.
https://itsfoss.com/gpg-encrypt-files-basic/
I'll give it a try, indeed! Thanks.

However, I still don't understand why my approach doesn't work and the reason of the error...
 
Old 05-24-2023, 06:29 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Both keys need to be in PEM format.
ssh-keygen -p -m PEM -f /path/to/id_rsa

This will overwrite the old key.

Using Ubuntu version 22 rsautl is deprecated and instead I used pkeytul.

Last edited by michaelk; 05-24-2023 at 06:31 PM.
 
Old 05-24-2023, 07:48 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
This site https://www.madboa.com/geek/openssl/ has a very good explanation with working examples of manipulations involving OpenSSL tools.
Well worth a read.
 
Old 05-25-2023, 04:34 AM   #9
banderas20
Member
 
Registered: Aug 2018
Posts: 138

Original Poster
Rep: Reputation: Disabled
Hello everyone.

It's working now. Maybe I skipped some step. Here are the steps:

1. Generate keypair for users A and B

ssh-keygen

2. Convert the public keys to PKCS8 format:

ssh-keygen -f A.pub -e -m PKCS8 > A2.pub
ssh-keygen -f B.pub -e -m PKCS8 > B2.pub

3. Move the pub files to "shared folder" /tmp (for the example)

4. Cipher the plain text from A with the pub key of B

cat file | openssl rsautl -encrypt -pubin -inkey /tmp/B2.pub > /tmp/encrypted.txt

5. Decipher the coded file with the priv key of B

cat /tmp/encrypted.txt | openssl rsautl -decrypt -inkey B


And it works.

Thanks everyone for your help!
 
  


Reply

Tags
encryption, ssh, ssl



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] OpenSSL Expecting: ANY PRIVATE KEY Mark_667 Linux - Security 0 02-10-2021 07:46 PM
OpenSSL Error trying to verify a private key bluethundr Linux - Newbie 2 08-13-2020 10:56 AM
How do I convert a putty private key to be used as a private key on a RedHat or Ubuntu Server greavette Linux - General 2 06-26-2019 08:40 PM
[SOLVED] Unable to load Public Key (OpenSSL RSA, Debian Squeeze) gacanepa Linux - Newbie 1 11-29-2012 12:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 12:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration