32

UPDATED

When I am trying to generate a public key it still gives me an error "error in libcrypto"

Load key "breakthis": error in libcrypto

Private key is given below

I am trying to solve a Hack the box machine

I was able to get a private key then I converted that private key into a hash for JOHNTHERIPPER and cracked it successfully

But now when I try to login in through ssh I am getting the following error I have searched a lot but not able to resolve these error


ssh -i id_rsa [email protected]

load pubkey "id_rsa": invalid format 

Load key "id_rsa": error in libcrypto

[email protected]'s password: 

10
  • 3
    I am still getting an error "error in libcrypto" Commented Apr 2, 2020 at 11:13
  • Update your question with how you are attempting to connect after generating the public key from the private key. Commented Apr 2, 2020 at 12:46
  • 1
    As mentioned by Alex, a using a public key in ~.ssh/.config seems to cause cause the error in libcrypto for me as long as that key is not loaded by a running SSH agent. It happened for me when my keeagent crashed. However, restarting the agent seems to allow such setup as mentioned in here
    – kf06925
    Commented Jan 6, 2023 at 14:42
  • 1
    @NasirRiley Your statement is wrong. I am simply correcting the wrong information. End of the conversation.
    – Drew
    Commented Jan 28, 2023 at 5:40
  • 3
    Just to clear up confusion for anyone stumbling across this comment chain, the private key is indeed what you need to connect each time. As proof, try ssh -i ~/.ssh/id_rsa.pub (or equivalent) and see what happens. You need ssh -i ~/.ssh/id_rsa. You can use the file command on the key files to see what kind of key they are. Indeed, the server needs your public key in its known_hosts for you to connect, but that is a one-time operation and is not a routine part of establishing SSH connections to a server.
    – eeowaa
    Commented Jun 13, 2023 at 19:17

8 Answers 8

40

In my case I had copied the keyfile using the clipboard, which had created a similar file but without terminating newline. That was enough to trip up libcrypto. Opening the file in vim and :wq closing it, was enough to add the newline, and it fixed my issue.

More background

Here is what you want for this file:

  • LF line endings (aka Unix). Not CRLF (aka Windows)
  • Terminating newline (meaning: the last character of the entire file must be a newline).

To ensure you have both, assuming your keyfile is ~/.ssh/id_rsa, you can do this:

dos2unix ~/.ssh/id_rsa
vim --clean ~/.ssh/id_rsa

Once in vim you type :wq then hit the return key.

7
  • 9
    haha this is such a stupid issue, surely it is a bug? why would a newline at the end of the file be a hard requirement Commented Feb 19, 2023 at 19:38
  • Mine was a different problem... but replacing every "\n" with actually "enter" in nano editor has solved my problem. :-)
    – Manan Shah
    Commented Jun 1, 2023 at 6:25
  • I had a similar issue caused by copying to the clipboard - the issue was my pasted file then had Windows CRLF line endings. The solution was to convert to Unix LF line endings (which can be done in Sublime Text)
    – pjpscriv
    Commented Oct 20, 2023 at 4:46
  • 3
    No success with vim, but I used dos2unix .ssh/id_keyfound and it worked
    – Kantium
    Commented Nov 29, 2023 at 21:15
  • 1
    The only thing that helped me was to copy the key as file instead of using the clipboard
    – Joerg S
    Commented Jan 22 at 14:19
28

ERROR: load pubkey "id_rsa": invalid format

It happens when public key is used in ~/.ssh/config instead of using private key.

3
  • 2
    sick. this was it
    – gatsbyz
    Commented Jun 20 at 2:22
  • I had the same issue (using a pubkey instead of a privkey in my~/.ssh/config). But strangely, I haven't touched my .ssh/config in a while and it was working fine until today. Maybe a bug that got introduced in a later update of something...
    – Hippo
    Commented Jul 25 at 13:58
  • if you aren't loading the private key with ssh-add then the pub key is useless. There are alternatives to ssh-add that make the process smooth btw
    – develCuy
    Commented Aug 13 at 21:05
12

I fixed the Load key "id_rsa": error in libcrypto error by adding an LF control character (new line) at the end of the last line -----END OPENSSH PRIVATE KEY----- of the .pem file.

Before (doesn't work):

enter image description here

After (works):

enter image description here

Note the additional LF control character in the last non-empty line. One can add an LF control character by simply hitting the ENTER key.

Commands used to SSH into my remote computer:

chmod 400 key.pem
ssh -i "key.pem" [email protected]

Note: the error message can be either:

  • Load key "id_rsa": error in libcrypto (I saw it on my Ubuntu computer).
  • Load key "key.pem": invalid format (I saw it on my Windows computer) .

To show End of Line control characters in Notepad++:

enter image description here

1
  • 1
    This is the answer if you are coming here from GitLab CI. As a File variable it will truncate the trailing newline. Adding a simple extra newline in the variable solves the issue.
    – Jeff Mixon
    Commented Jan 19 at 21:07
4

for future readers. I was using a ssh key file inside Gitlab CI YAML file and I kept getting error in libcrypto message. I did 2 things that FIXED the problem:

  1. Just changing the variable type from FILE variable to ENV_VAR
  2. Using ssh-keygen -t ed25519 when generating the key. Looks like gitlab does not support the default rsa keys.

Then everything works fine.

Also use this as the base before_script:

  before_script:
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client wget gnupg -y )'
    - wget -qO- https://get.docker.com/gpg | apt-key add -
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - touch ~/.ssh/config
    - touch ~/.ssh/known_hosts
    - chmod -R 400 ~/.ssh
    - ssh-keyscan <ip> >> ~/.ssh/known_hosts
    - '[[ -f /.dockerinit ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
1
  • this helped me so very much - thanks !!!!!!
    – Datum Geek
    Commented Mar 11 at 7:08
2

I got this error, when I was using 2 github accounts with 2 ssh keys, but get rid of this error when i changed my config file like below,

myDir$ ssh -T [email protected]
Load key "/home/taxi/.ssh/mhmd_personal.pub": error in libcrypto
[email protected]: Permission denied (publickey).

From this

Host *
  AddKeysToAgent yes
  IdentityFile ~/.ssh/mhmd_personal.pub
  # IdentityFile ~/.ssh/id_ed25519

to

Host *
  AddKeysToAgent yes
  IdentityFile ~/.ssh/mhmd_personal
  # IdentityFile ~/.ssh/id_ed25519

removed the .pub extension from config file.

3
  • This does not fix the error.
    – nurettin
    Commented Dec 24, 2022 at 8:20
  • some other git error may show similar error message, I am here to help Commented Dec 26, 2022 at 6:11
  • Thank you @Md.SultanulArefin This was a life saver!
    – zsubzwary
    Commented Dec 8, 2023 at 6:35
0

I just have fixed this problem. This problem was caused because of the format of the text.

Replace following the ssh-key format, Its looks like this:

-----BEGIN OPENSSH PRIVATE KEY-----
ENCODED KEY DATA HERE
-----END OPENSSH PRIVATE KEY-----
2
  • 2
    For the next time; there's no need to do this manually. Command ssh-copy-id -i /pathto/privatekey user@remotehost will copy the corresponding public key in the correct format to the target :-) Commented Mar 21, 2023 at 4:45
  • @Peregrino69 all that command does is edit the authorized_keys file in the remote. It does not copy the actual private key to the remote.
    – crypdick
    Commented May 2 at 19:35
0

what fixed it for me was the answer to this question

I opened the key in puttygen then exported a new ppk using the conversion --> Export OpenSSH Key

0

So in my case the private key file was simply missing the read permissions for the owner. After adding the read permission (chmod u+r id_ecdsa), everything worked fine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .