sshtools / maverick-synergy

Next Generation Java SSH API
https://jadaptive.com
GNU Lesser General Public License v3.0
96 stars 26 forks source link

Unable to generate public key from private key #32

Closed TheAustinSwamy closed 2 years ago

TheAustinSwamy commented 3 years ago

Getting this exception with bouncy castle 1.69. Probably not supported yet.

java.security.spec.InvalidKeySpecException: encoded key spec not recognized: 'buf' must have length 32 with bouncy castle 1.69 version. The work around is to go back to 1.68.

org.bouncycastle bcprov-jdk15on 1.69

Here is a simple test case


import com.sshtools.common.publickey.*; import com.sshtools.common.ssh.components.SshKeyPair;

import java.io.IOException; import java.nio.charset.StandardCharsets;

public class TestCase {

private static final String EXAMPLE_PRIVATE_KEY = "-----BEGIN OPENSSH PRIVATE KEY-----\n" +
        "b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABBg\n" +
        "cV+MWCsqs95/2gU8qPWwAAAAEAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAIAxP\n" +
        "t6ivXsQjygWAMO7TvzmTKdNo/0gLd5XBw6ILSa6QAAAAkHQmYjPlbGWnwlBjeb1T\n" +
        "ATBpThm4mY9iJ5I5sustQVqJF/meE+eygDbuLFrCjXpvargV2WtHbrOVQB8kbUPd\n" +
        "jB8HFjnvEP8dL75q5Hngab2V2uVt8vJ0+d8vdrTwqCasFW/ASvzudrDJtFV+KGxv\n" +
        "7m2LvH+Xgyc8ChxyZfIBB6Jtoy1KkQyA2V4d8ILB4qLFmA==\n" +
        "-----END OPENSSH PRIVATE KEY-----\n";
private static final String EXAMPLE_PRIVATE_KEY_PASSPHRASE = "bbUNtjXPtsCA75!!";

public static String testGeneratePublicKey() throws InvalidPassphraseException, IOException {
    SshKeyPair pair = SshKeyUtils.getPrivateKey(EXAMPLE_PRIVATE_KEY, EXAMPLE_PRIVATE_KEY_PASSPHRASE);
    SshPublicKeyFile kf =
            SshPublicKeyFileFactory.create(
                    pair.getPublicKey(), "Comment Test", SshPrivateKeyFileFactory.OPENSSH_FORMAT);
    return new String(kf.getFormattedKey(), StandardCharsets.UTF_8);
}

public static void main(String[] args) {
    try {
        System.out.println(testGeneratePublicKey());
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

}

ludup commented 3 years ago

This is a known issue with the current open source releases but fixed in our hotfix repository. This will be patched to the open-source version at the next applicable merge.

ludup commented 2 years ago

This issue has been fixed in the latest 3.0.6 update.