KeyGen.cs :  » Network-Clients » SharpSSH » Tamir » SharpSsh » jsch » examples » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » SharpSSH 
SharpSSH » Tamir » SharpSsh » jsch » examples » KeyGen.cs
using System;
using Tamir.SharpSsh.jsch;

/* KeyGen.cs
 * ====================================================================
 * The following example was posted with the original JSch java library,
 * and is translated to C# to show the usage of SharpSSH JSch API
 * ====================================================================
 * */
namespace Tamir.SharpSsh.jsch.examples{
  /// <summary>
  /// This progam will demonstrate the RSA/DSA keypair generation. 
  /// You will be asked a passphrase for output_keyfile.
  /// If everything works fine, you will get the DSA or RSA keypair, 
  /// output_keyfile and output_keyfile+".pub".
  /// The private key and public key are in the OpenSSH format.
  /// </summary>
  public class KeyGen
  {
    public static void Main(params string[] arg)
    {
      if(arg.Length<3)
      {
        Console.Error.WriteLine(
          "usage: java KeyGen rsa output_keyfile comment\n"+
          "       java KeyGen dsa  output_keyfile comment");
        Environment.Exit(-1);
      }

      //Get sig type ('rsa' or 'dsa')
      String _type=arg[0];
      int type=0;
      if(_type.Equals("rsa")){type=KeyPair.RSA;}
      else if(_type.Equals("dsa")){type=KeyPair.DSA;}
      else 
      {
        Console.Error.WriteLine(
          "usage: java KeyGen rsa output_keyfile comment\n"+
          "       java KeyGen dsa  output_keyfile comment");
        Environment.Exit(-1);
      }
      //Output file name
      String filename=arg[1];
      //Signature comment
      String comment=arg[2];

      //Create a new JSch instance
      JSch jsch=new JSch();

      //Prompt the user for a passphrase for the private key file
      String passphrase=InputForm.GetUserInput("Enter passphrase (empty for no passphrase)", true);

      try
      {
        //Generate the new key pair
        KeyPair kpair=KeyPair.genKeyPair(jsch, type);
        //Set a passphrase
        kpair.setPassphrase(passphrase);
        //Write the private key to "filename"
        kpair.writePrivateKey(filename);
        //Write the public key to "filename.pub"
        kpair.writePublicKey(filename+".pub", comment);
        //Print the key fingerprint
        Console.WriteLine("Finger print: "+kpair.getFingerPrint());
        //Free resources
        kpair.dispose();
      }
      catch(Exception e)
      {
        Console.WriteLine(e);
      }
      Environment.Exit(0);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.