1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17packageorg.apache.jetspeed.security.util;
1819import java.security.NoSuchAlgorithmException;
20import java.security.spec.InvalidKeySpecException;
2122import javax.crypto.Cipher;
23import javax.crypto.SecretKey;
24import javax.crypto.SecretKeyFactory;
25import javax.crypto.spec.PBEKeySpec;
26import javax.crypto.spec.PBEParameterSpec;
2728import org.apache.commons.codec.binary.Base64;
29import org.apache.jetspeed.security.SecurityException;
3031/***32 * <p>33 * PBEPasswordTool encodes and decodes user passwords using Password Based encryptionl34 * </p>35 * 36 * @author <a href="mailto:ate@douma.nu">Ate Douma</a>37 * @version $Id$38 */39publicclassPBEPasswordTool40 {
41// PKCS #5 (PBE) algoritm42privatestaticfinal String CIPHER_ALGORITM = "PBEwithMD5andDES";
43// PKCS #5 iteration count is advised to be at least 100044privatestaticfinalint PKCS_5_ITERATIONCOUNT = 1111;
45// pseudo random base salt which will be overlayed with userName.getBytes()46privatestaticfinal byte[] PKCS_5_BASE_SALT = {(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32, (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03};
4748// PBE cipher49private SecretKey pbeKey;
5051publicPBEPasswordTool(String pbePassword) throws InvalidKeySpecException, NoSuchAlgorithmException
52 {
53 pbeKey = SecretKeyFactory.getInstance(CIPHER_ALGORITM).generateSecret(new PBEKeySpec(pbePassword.toCharArray()));
54 }
5556/* (non-Javadoc)57 * @see org.apache.jetspeed.security.spi.CredentialPasswordEncoder#encode(java.lang.String, java.lang.String)58 * @see org.apache.jetspeed.security.PasswordEncodingService#encode(java.lang.String, java.lang.String)59 */60public String encode(String userName, String clearTextPassword) throws SecurityException
61 {
62try63 {
64// prevent dictionary attacks as well as copying of encoded passwords by using the userName as salt65 PBEParameterSpec cipherSpec = new PBEParameterSpec(createSalt(userName.getBytes("UTF-8")), PKCS_5_ITERATIONCOUNT);
6667 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITM);
68 cipher.init(Cipher.ENCRYPT_MODE,pbeKey,cipherSpec);
6970returnnew String(Base64.encodeBase64(cipher.doFinal(clearTextPassword.getBytes("UTF-8"))), "UTF-8");
71 }
72catch (Exception e)
73 {
74thrownew SecurityException(SecurityException.UNEXPECTED.create("PBEPasswordTool","encode",e.getMessage()), e);
75 }
76 }
7778/* (non-Javadoc)79 * @see org.apache.jetspeed.security.PasswordEncodingService#decode(java.lang.String, java.lang.String)80 */81public String decode(String userName, String encodedPassword) throws SecurityException
82 {
83try84 {
85// prevent dictionary attacks as well as copying of encoded passwords by using the userName as salt86 PBEParameterSpec cipherSpec = new PBEParameterSpec(createSalt(userName.getBytes("UTF-8")), PKCS_5_ITERATIONCOUNT);
8788 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITM);
89 cipher.init(Cipher.DECRYPT_MODE,pbeKey,cipherSpec);
9091returnnew String(cipher.doFinal(Base64.decodeBase64(encodedPassword.getBytes("UTF-8"))), "UTF-8");
92 }
93catch (Exception e)
94 {
95thrownew SecurityException(SecurityException.UNEXPECTED.create("PBEPasswordTool","decode",e.getMessage()), e);
96 }
97 }
9899/*100 * Create a PCKS #5 salt using the BASE_PCKS_5_SALT overlayed with the provided secret parameter101 */102private byte[] createSalt(byte[] secret)
103 {
104 byte[] salt = new byte[PKCS_5_BASE_SALT.length];
105int i = 0;
106for (;i < salt.length && i < secret.length; i++)
107 {
108 salt[i] = secret[i];
109 }
110for (; i < salt.length; i++)
111 {
112 salt[i] = PKCS_5_BASE_SALT[i];
113 }
114return salt;
115 }
116117publicstaticvoid main(String args[]) throws Exception
118 {
119if (args.length != 4 || (!args[0].equals("encode") && !args[0].equals("decode")))
120 {
121 System.err.println("Encode/Decode a user password using Password Based Encryption");
122 System.err.println("Usage: PBEPasswordTool <encode|decode> <encoding-password> <username> <password>");
123 System.err.println(" encode|decode : specify if to encode or decode the provided password");
124 System.err.println(" encoding-password: the password to be used for encoding and decoding");
125 System.err.println(" username : the name of the user to which the provided password belongs");
126 System.err.println(" password : the cleartext password to encode, or the encoded password to decode\n");
127 }
128elseif (args[0].toLowerCase().equals("encode"))
129 {
130 System.out.println("Encoded password: "+newPBEPasswordTool(args[1]).encode(args[2],args[3]));
131 }
132else133 {
134 System.out.println("Decoded password: "+newPBEPasswordTool(args[1]).decode(args[2],args[3]));
135 }
136 }
137 }