2010-09-10 15 views
7

क्या कोई मुझे sshj में निजी/सार्वजनिक कुंजी प्रमाणीकरण का उदाहरण दे सकता है?निजी/सार्वजनिक कुंजी प्रमाणीकरण के एसएसएच उदाहरण

SSHJ की कमांड लाइन बराबर क्या में,

ssh -i /path/to/mykey.private [email protected] 

मैंने कोशिश की (त्रुटि छोड़े गए हैंडलिंग),

final SSHClient ssh = new SSHClient(); 
ssh.loadKnownHosts(); 
ssh.connect("host"); 
ssh.authPublickey("username", "/path/to/mykey.private"); 
final Session session = ssh.startSession(); 
... 

लेकिन मैं देख रहा हूँ लॉग बयान में,

DEBUG net.schmizz.sshj.SSHClient - Attempting to load key from: /path/to/mykey.private 
WARN net.schmizz.sshj.SSHClient - Could not load keys due to: {} 
net.schmizz.sshj.common.SSHException: No provider available forUnknown key file 
    at net.schmizz.sshj.SSHClient.loadKeys(SSHClient.java:482) ~[sshj-0.3.0.jar:na] 
... 
Exception in thread "main" 10:49:55.943 [reader] DEBUG 
net.schmizz.sshj.transport.Reader - Stopping 
net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methods 

धन्यवाद, एवरेट

+0

मैं [यहां] (http://stackoverflow.com/a/15800383/311525) एक ऐसी ही सवाल एक उदाहरण के साथ एक एडब्ल्यूएस से कनेक्ट कर दिए: यहाँ Maven निर्भरता है उदाहरण के साथ एक .pem फ़ाइल प्रदान करते हैं। यह उतना आगे नहीं था जितना कि मैंने आशा की थी। – Scott

उत्तर

0

इस तरह KeyPairWrapper उपयोग करके देखें:

KeyPair kp = ... // read keypair from file 
ssh.authPublickey(user, new KeyPairWrapper(keypair)); 

BouncyCastle प्रदाता के साथ आप (गन्दा कोड के लिए क्षमा याचना)

/** 
* Takes a PEM-encoded PKCS8 key-containing InputStream and returns the KeyPair within. Only the first keypair is considered 
* 
* @return 
* @throws IOException if the stream is not a valid PKCS8 wrapped keypair 
*/ 
public static KeyPair readKeypair(final InputStream is, final char[] password) throws IOException { 
    PasswordFinder passwordFinder = password != null ? new StaticPasswordFinder(password) : null; 

    KeyPair kp = null; 
    try { 
     // read the stream as a PEM encoded 
     try { 

      final PEMReader pem = new PEMReader(new InputStreamReader(is), passwordFinder); 
      try { 
       // Skip over entries in the file which are not KeyPairs 
       do { 
        final Object o = pem.readObject(); 

        if (o == null) 
         break; // at end of file 
        else if (o instanceof KeyPair) 
         kp = (KeyPair) o; 
       } while (kp == null); 
      } 
      finally { 
       pem.close(); 
      } 
     } 
     catch (EncryptionException e) { 
      throw new IOException("Error reading PEM stream: " + e.getMessage(), e); 
     } 
    } 
    finally { 
     is.close(); 
    } 

    // Cast the return to a KeyPair (or, if there is no [valid] return, throw an exception) 
    if (kp != null) 
     return kp; 
    else 
     throw new IOException("Stream " + is + " did not contain a PKCS8 KeyPair"); 
} 
0

आप की जरूरत के लिए एक PKCS8 पीईएम से एक keypair निकालने के लिए कुछ इस तरह उपयोग कर सकते हैं सबसे महत्वपूर्ण प्रकारों के लिए BouncyCastle lib शामिल करें। org.bouncycastle bcprov-jdk16 1.46

संबंधित मुद्दे