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