1 2 import com.jcraft.jsch.*; 3 import javax.swing.*; 4 5 class ChangePassphrase{ 6 public static void main(String [] arg){ 7 if(arg.length!=1){ 8 System.err.println("usage: java ChangePassphrase private_key"); 9 System.exit(-1); 10 } 11 12 JSch jsch=new JSch(); 13 14 String pkey=arg[0]; 15 16 try{ 17 KeyPair kpair=KeyPair.load(jsch, pkey); 18 19 System.out.println(pkey+" has "+(kpair.isEncrypted()?"been ":"not been ")+"encrypted"); 20 21 String passphrase=""; 22 while(kpair.isEncrypted()){ 23 JTextField passphraseField=(JTextField)new JPasswordField(20); 24 Object [] ob={passphraseField}; 25 int result=JOptionPane.showConfirmDialog(null, ob, 26 "Enter passphrase for "+pkey, 27 JOptionPane.OK_CANCEL_OPTION); 28 if(result!=JOptionPane.OK_OPTION){ 29 System.exit(-1); 30 } 31 passphrase=passphraseField.getText(); 32 if(!kpair.decrypt(passphrase)){ 33 System.out.println("failed to decrypt "+pkey); 34 } 35 else{ 36 System.out.println(pkey+" is decrypted."); 37 } 38 } 39 40 passphrase=""; 41 42 JTextField passphraseField=(JTextField)new JPasswordField(20); 43 Object [] ob={passphraseField}; 44 int result=JOptionPane.showConfirmDialog(null, ob, 45 "Enter new passphrase for "+pkey+ 46 " (empty for no passphrase)", 47 JOptionPane.OK_CANCEL_OPTION); 48 if(result!=JOptionPane.OK_OPTION){ 49 System.exit(-1); 50 } 51 passphrase=passphraseField.getText(); 52 53 kpair.setPassphrase(passphrase); 54 kpair.writePrivateKey(pkey); 55 kpair.dispose(); 56 } 57 catch(Exception e){ 58 System.out.println(e); 59 } 60 System.exit(0); 61 } 62 } 63 | Popular Tags |