1 7 package org.jboss.security.plugins; 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.io.RandomAccessFile ; 12 import java.io.ByteArrayOutputStream ; 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 41 public class FilePassword 42 { 43 private File passwordFile; 44 45 public FilePassword(String file) 46 { 47 passwordFile = new File (file); 48 } 49 50 public char[] toCharArray() 51 throws IOException 52 { 53 RandomAccessFile raf = new RandomAccessFile (passwordFile, "rws"); 54 try 55 { 56 char[] password = decode(raf); 57 return password; 58 } 59 catch(Exception e) 60 { 61 Logger log = Logger.getLogger(FilePassword.class); 62 log.error("Failed to decode password file: "+passwordFile, e); 63 throw new IOException (e.getMessage()); 64 } 65 } 66 67 static char[] decode(RandomAccessFile passwordFile) 68 throws Exception 69 { 70 byte[] salt = new byte[8]; 71 passwordFile.readFully(salt); 72 int count = passwordFile.readInt(); 73 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 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 (decode, "UTF-8").toCharArray(); 88 } 89 static void encode(RandomAccessFile passwordFile, byte[] salt, int count, 90 byte[] secret) 91 throws Exception 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 113 public static void main(String [] args) throws Exception 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 passwordFile = new RandomAccessFile (args[3], "rws"); 130 encode(passwordFile, salt, count, passwordBytes); 131 } 132 } 133 | Popular Tags |