“Invalid privatekey” when using JSch

前端 未结 5 752
攒了一身酷
攒了一身酷 2020-11-27 13:25

I\'m using the following code to work with Git in a Java application. I have a valid key (use it all the time), and this specific code has work for me before with the same

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 13:41

    1. You read a file named .pem and de-base64 all of it and treat the result as PKCS8-unencrypted, apparently successfully. This means the file was NOT PEM-format. PEM format at minimum MUST have the dash-BEGIN and dash-END lines to be valid, which if not removed cause de-base64 to either fail or be wrong. (Some PEM formats also have 822-style headers which must be handled.)

    2. You appear to be using BouncyCastle, but in my versions there is no PKCS8Generator constructor that takes only RSAPrivateKey. The closest thing that works is JcaPKCS8Generator (RSAPrivateKey implements PrivateKey, OutputEncryptor=null) (i.e. a different but related class, and two arguments not one).

    3. PemWriter is buffered, and you didn't flush it before looking at the underlying StringWriter. As a result writer.toString().getBytes() is an empty/zero-length array, which JSch rightly considers invalid.

    With #2 and #3 fixed and using my input, and calling JSch directly instead of via JGit, it works for me.

提交回复
热议问题