KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > plugins > FilePassword


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security.plugins;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.RandomAccessFile JavaDoc;
12 import java.io.ByteArrayOutputStream JavaDoc;
13
14 import javax.crypto.spec.PBEParameterSpec;
15 import javax.crypto.spec.PBEKeySpec;
16 import javax.crypto.Cipher;
17 import javax.crypto.SecretKeyFactory;
18 import javax.crypto.SecretKey;
19
20 import org.jboss.logging.Logger;
21
22 /** Read a password in opaque form to a file for use with the FilePassword
23  accessor in conjunction with the JaasSecurityDomain
24  {CLASS}org.jboss.security.plugins.FilePassword:password-file
25  format of the KeyStorePass attribute. The original opaque password file
26  can be created by running:
27    java org.jboss.security.plugins.FilePassword salt count password password-file
28  Running
29    java org.jboss.security.plugins.FilePassword
30  will generate a usage message.
31
32  Note that this is security by obscurity in that the password is not store
33  in plaintext, but it can be recovered by simply using the code from this
34  class.
35
36  @see #main(String[])
37
38  @author Scott.Stark@jboss.org
39  @version $Revison:$
40  */

41 public class FilePassword
42 {
43    private File JavaDoc passwordFile;
44
45    public FilePassword(String JavaDoc file)
46    {
47       passwordFile = new File JavaDoc(file);
48    }
49
50    public char[] toCharArray()
51       throws IOException JavaDoc
52    {
53       RandomAccessFile JavaDoc raf = new RandomAccessFile JavaDoc(passwordFile, "rws");
54       try
55       {
56          char[] password = decode(raf);
57          return password;
58       }
59       catch(Exception JavaDoc e)
60       {
61          Logger log = Logger.getLogger(FilePassword.class);
62          log.error("Failed to decode password file: "+passwordFile, e);
63          throw new IOException JavaDoc(e.getMessage());
64       }
65    }
66
67    static char[] decode(RandomAccessFile JavaDoc passwordFile)
68       throws Exception JavaDoc
69    {
70       byte[] salt = new byte[8];
71       passwordFile.readFully(salt);
72       int count = passwordFile.readInt();
73       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
74       int b;
75       while( (b = passwordFile.read()) >= 0 )
76          baos.write(b);
77       passwordFile.close();
78       byte[] secret = baos.toByteArray();
79
80       PBEParameterSpec cipherSpec = new PBEParameterSpec(salt, count);
81       PBEKeySpec keySpec = new PBEKeySpec("78aac249a60a13d5e882927928043ebb".toCharArray());
82       SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEwithMD5andDES");
83       SecretKey cipherKey = factory.generateSecret(keySpec);
84       Cipher cipher = Cipher.getInstance("PBEwithMD5andDES");
85       cipher.init(Cipher.DECRYPT_MODE, cipherKey, cipherSpec);
86       byte[] decode = cipher.doFinal(secret);
87       return new String JavaDoc(decode, "UTF-8").toCharArray();
88    }
89    static void encode(RandomAccessFile JavaDoc passwordFile, byte[] salt, int count,
90       byte[] secret)
91       throws Exception JavaDoc
92    {
93       PBEParameterSpec cipherSpec = new PBEParameterSpec(salt, count);
94       PBEKeySpec keySpec = new PBEKeySpec("78aac249a60a13d5e882927928043ebb".toCharArray());
95       SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEwithMD5andDES");
96       SecretKey cipherKey = factory.generateSecret(keySpec);
97       Cipher cipher = Cipher.getInstance("PBEwithMD5andDES");
98       cipher.init(Cipher.ENCRYPT_MODE, cipherKey, cipherSpec);
99       byte[] encode = cipher.doFinal(secret);
100       passwordFile.write(salt);
101       passwordFile.writeInt(count);
102       passwordFile.write(encode);
103       passwordFile.close();
104
105    }
106    /** Write a password in opaque form to a file for use with the FilePassword
107     * accessor in conjunction with the JaasSecurityDomain
108     * {CLASS}org.jboss.security.plugins.FilePassword:password-file
109     * format of the KeyStorePass attribute.
110     *
111     * @param args
112     */

113    public static void main(String JavaDoc[] args) throws Exception JavaDoc
114    {
115       if( args.length != 4 )
116       {
117          System.err.println(
118             "Write a password in opaque form to a file for use with the FilePassword accessor"
119            +"Usage: FilePassword salt count password password-file"
120            +" salt : an 8 char sequence for PBEKeySpec"
121            +" count : iteration count for PBEKeySpec"
122            +" password : the clear text password to write"
123            +" password-file : the path to the file to write the password to"
124          );
125       }
126       byte[] salt = args[0].substring(0, 8).getBytes();
127       int count = Integer.parseInt(args[1]);
128       byte[] passwordBytes = args[2].getBytes("UTF-8");
129       RandomAccessFile JavaDoc passwordFile = new RandomAccessFile JavaDoc(args[3], "rws");
130       encode(passwordFile, salt, count, passwordBytes);
131    }
132 }
133
Popular Tags