JavaDoc


001    /**
002     * Copyright (c) 2003 Daffodil Software Ltd all rights reserved,
003     * Modifications Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved.
004     * This program is free software; you can redistribute it and/or modify
005     * it under the terms of version 2 of the GNU General Public License as
006     * published by the Free Software Foundation.
007     * There are special exceptions to the terms and conditions of the GPL
008     * as it is applied to this software. See the GNU General Public License for more details.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018     */
019    
020    package org.dbreplicator.repconsole;
021    
022    import java.io.BufferedReader;
023    import java.io.InputStreamReader;
024    import javax.crypto.SecretKey;
025    import javax.crypto.KeyGenerator;
026    import java.security.*;
027    import org.dbreplicator.replication.PathHandler;
028    import java.io.File;
029    import java.io.FileOutputStream;
030    import java.io.*;
031    /* bjt - remove sun proprietary stuff */
032    /* 
033    import sun.misc.BASE64Decoder;
034    import sun.misc.BASE64Encoder;
035    */
036    
037    public class EncryptDecryptPassword {
038       private DesEncrypter encrypter;
039       private String publisherpassword="",pubYes="";
040       private String subscriberpassword="",subyes="";
041       private SecretKey key;
042    
043    
044       public EncryptDecryptPassword() {
045       getPasswordFromEndUser();
046       initialiseDesEncryptor();
047       encryptPassword();
048      }
049    
050    
051      private void initialiseDesEncryptor() {
052        try {
053         key = getSecretKey();
054         String key1 = new String(key.getEncoded() );
055    //     writePublisherPassword("key",key1);
056    //     writeSubscriberPassword("key",key1);
057         encrypter = new DesEncrypter(key);
058       }
059       catch (Exception ex) {
060       }
061    
062      }
063    
064      private void  getPasswordFromEndUser() {
065        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
066        try {
067        System.out.print("Press Y to encrypt password for publication datasource else N :");
068        pubYes=(input.readLine());
069        pubYes =pubYes.trim();
070        if(pubYes.equalsIgnoreCase("y")){
071    
072          System.out.print("Enter Publisher DataSource Password  : ");
073          publisherpassword = (input.readLine());
074          publisherpassword =publisherpassword.trim();
075          if (publisherpassword.equalsIgnoreCase("\n") ||
076          publisherpassword.equalsIgnoreCase("") ) {
077          System.out.println("         Invalid Password     ");
078          getPasswordFromEndUser();
079          }
080    
081        }else  if(pubYes.equalsIgnoreCase("n")){
082    
083          System.out.print("Enter Subscriber DataSource Password : ");
084          subscriberpassword = (input.readLine());
085          subscriberpassword =subscriberpassword.trim();
086          if(subscriberpassword.equalsIgnoreCase("\n") ||
087          subscriberpassword.equalsIgnoreCase("")) {
088          System.out.println("        Invalid Password     ");
089          getPasswordFromEndUser();
090          }
091        }else {
092          System.out.println("       Invalid Option     ");
093          System.out.print("       Continue(Y/N)  :");
094          String  tes=(input.readLine());
095          if(tes.equalsIgnoreCase("y"))
096          getPasswordFromEndUser();
097          else
098          System.exit(00);
099        }
100        }
101        catch (Exception e) {
102    //      System.err.println(e.getMessage());
103    //      e.printStackTrace();
104        }
105      }
106    
107    
108    
109    
110    
111      private void encryptPassword() {
112       if(pubYes.equalsIgnoreCase("y")){
113        publisherpassword = encrypter.encrypt(publisherpassword);
114        writePublisherPassword("PASSWORD",publisherpassword);
115      }else if(pubYes.equalsIgnoreCase("n")){
116        subscriberpassword = encrypter.encrypt(subscriberpassword);
117        writeSubscriberPassword("PASSWORD",subscriberpassword);
118        }
119      }
120    
121    
122      private void writePublisherPassword(String  key,String valueTowrite){
123        try {
124          File f = new File("." + File.separator + "pubconfig.ini");
125          FileOutputStream fos = new FileOutputStream(f,true);
126          OutputStreamWriter os = new OutputStreamWriter(fos);
127          os.write("\r\n");
128          os.write(key+"="+valueTowrite);
129          os.write("\r\n");
130          os.flush();
131        }
132        catch (Exception ex) {
133    
134    //      ex.printStackTrace() ;
135        }
136      }
137    
138    
139      private void writeSubscriberPassword(String  key,String valueTowrite){
140         try {
141           File f = new File("." + File.separator + "Subconfig.ini");
142           FileOutputStream fos = new FileOutputStream(f,true);
143           OutputStreamWriter os = new OutputStreamWriter(fos);
144           os.write("\r\n");
145           os.write(key+"="+valueTowrite);
146           os.write("\r\n");
147           os.flush();
148         }
149         catch (Exception ex) {
150    //       ex.printStackTrace() ;
151         }
152       }
153    
154      private static void EcodePassword(String password){
155        byte[] b =password.getBytes();
156        byte five = (byte)5;
157    //System.out.println("five  byte : "+five);
158        for (int i = 0; i < b.length; i++) {
159    //System.out.println(" b[i] ="+b[i]);
160          b[i] = (byte)(b[i]);
161        }
162    
163    //System.out.println(" Encrypt password : "+new String(b));
164    
165       for (int i = 0; i < b.length; i++) {
166         b[i] = (byte)(b[i]/five*five);
167       }
168    //System.out.println(" Decrypt password : "+new String(b));
169      }
170    
171    
172      private SecretKey getSecretKey(){
173        try {
174          String path ="/org.dbreplicator/repconsole/secretKey.obj";
175          java.net.URL url1 = getClass().getResource(path);
176    //       System.out.println(" url1 : "+url1);
177           ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(url1.openStream()));
178          SecretKey sk = (SecretKey) ois.readObject();
179          return sk;
180          }
181          catch (IOException ex) {
182    //        ex.printStackTrace();
183          }
184          catch(ClassNotFoundException ex){
185    //        ex.printStackTrace();
186    
187          }
188          return null;
189    
190        }
191    
192    
193    
194      public static void main(String[] args) {
195    //    EcodePassword("hrhk12");
196        EncryptDecryptPassword en = new EncryptDecryptPassword();
197      }
198    
199    
200    }





























































Powered by Drupal - Theme by Danger4k