00001 using System;
00002 using System.IO;
00003 using System.Windows.Forms;
00004
00005
00006
00007
00008
00009
00010
00011 namespace Tamir.SharpSsh.jsch.examples
00012 {
00021 public class ChangePassphrase
00022 {
00023 public static void Main(String[] arg)
00024 {
00025
00026 Console.WriteLine("Please choose your private key file...");
00027 String pkey = InputForm.GetFileFromUser("Choose your privatekey(ex. ~/.ssh/id_dsa)");
00028 Console.WriteLine("You chose "+pkey+".");
00029
00030
00031 JSch jsch=new JSch();
00032
00033 try
00034 {
00035
00036 KeyPair kpair=KeyPair.load(jsch, pkey);
00037
00038
00039 Console.WriteLine(pkey+" has "+(kpair.isEncrypted()?"been ":"not been ")+"encrypted");
00040
00041 String passphrase = "";
00042
00043 while(kpair.isEncrypted())
00044 {
00045 passphrase = InputForm.GetUserInput("Enter passphrase for "+pkey, true);
00046 if(!kpair.decrypt(passphrase))
00047 {
00048 Console.WriteLine("failed to decrypt "+pkey);
00049 }
00050 else
00051 {
00052 Console.WriteLine(pkey+" is decrypted.");
00053 }
00054 }
00055
00056 passphrase="";
00057
00058 passphrase=InputForm.GetUserInput("Enter new passphrase for "+pkey+
00059 " (empty for no passphrase)", true);
00060
00061
00062 kpair.setPassphrase(passphrase);
00063
00064 kpair.writePrivateKey(pkey);
00065
00066 kpair.dispose();
00067 }
00068 catch(Exception e)
00069 {
00070 Console.WriteLine(e);
00071 }
00072 }
00073 }
00074 }