001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.util;
018
019import javax.crypto.Cipher;
020import javax.crypto.spec.SecretKeySpec;
021
022/**
023 * Allows passwords to be encrypted and decrypted.
024 * <p>
025 * Warning: This uses AES128 with a fixed encryption key. This is only an obfuscation no cryptographic secure
026 * protection.
027 *
028 * @since 2.0
029 */
030public class DefaultCryptor implements Cryptor {
031    private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
032            'F' };
033
034    private static final byte[] KEY_BYTES = { 0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x43, 0x6F, 0x6D, 0x6D, 0x6F, 0x6E,
035            0x73, 0x56, 0x46, 0x53 };
036
037    private static final int INDEX_NOT_FOUND = -1;
038
039    private static final int BITS_IN_HALF_BYTE = 4;
040
041    private static final char MASK = 0x0f;
042
043    /**
044     * Encrypt the plain text password.
045     * <p>
046     * Warning: This uses AES128 with a fixed encryption key. This is only an obfuscation no cryptographic secure
047     * protection.
048     *
049     * @param plainKey The password.
050     * @return The encrypted password String.
051     * @throws Exception If an error occurs.
052     */
053    @Override
054    public String encrypt(final String plainKey) throws Exception {
055        final byte[] input = plainKey.getBytes();
056        final SecretKeySpec key = new SecretKeySpec(KEY_BYTES, "AES");
057
058        final Cipher cipher = Cipher.getInstance("AES");
059
060        // encryption pass
061        cipher.init(Cipher.ENCRYPT_MODE, key);
062
063        final byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
064        int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
065        ctLength += cipher.doFinal(cipherText, ctLength);
066        return encode(cipherText);
067    }
068
069    /**
070     * Decrypts the password.
071     *
072     * @param encryptedKey the encrypted password.
073     * @return The plain text password.
074     * @throws Exception If an error occurs.
075     */
076    @Override
077    public String decrypt(final String encryptedKey) throws Exception {
078        final SecretKeySpec key = new SecretKeySpec(KEY_BYTES, "AES");
079        final Cipher cipher = Cipher.getInstance("AES");
080        cipher.init(Cipher.DECRYPT_MODE, key);
081        final byte[] decoded = decode(encryptedKey);
082        final byte[] plainText = new byte[cipher.getOutputSize(decoded.length)];
083        int ptLength = cipher.update(decoded, 0, decoded.length, plainText, 0);
084        ptLength += cipher.doFinal(plainText, ptLength);
085        return new String(plainText).substring(0, ptLength);
086    }
087
088    /** Hex-encode bytes. */
089    private String encode(final byte[] bytes) {
090        final StringBuilder builder = new StringBuilder();
091
092        for (final byte b : bytes) {
093            builder.append(HEX_CHARS[(b >> BITS_IN_HALF_BYTE) & MASK]);
094            builder.append(HEX_CHARS[b & MASK]);
095        }
096        return builder.toString();
097    }
098
099    /** Decodes Hey-Bytes. */
100    private byte[] decode(final String str) {
101        final int length = str.length() / 2;
102        final byte[] decoded = new byte[length];
103        final char[] chars = str.toCharArray();
104        int index = 0;
105        for (int i = 0; i < chars.length; ++i) {
106            final int id1 = indexOf(HEX_CHARS, chars[i]);
107            if (id1 == -1) {
108                throw new IllegalArgumentException(
109                        "Character " + chars[i] + " at position " + i + " is not a valid hexidecimal character");
110            }
111            final int id2 = indexOf(HEX_CHARS, chars[++i]);
112            if (id2 == -1) {
113                throw new IllegalArgumentException(
114                        "Character " + chars[i] + " at position " + i + " is not a valid hexidecimal character");
115            }
116            decoded[index++] = (byte) ((id1 << BITS_IN_HALF_BYTE) | id2);
117        }
118        return decoded;
119    }
120
121    private int indexOf(final char[] array, final char valueToFind) {
122        if (array == null) {
123            return INDEX_NOT_FOUND;
124        }
125        for (int i = 0; i < array.length; i++) {
126            if (valueToFind == array[i]) {
127                return i;
128            }
129        }
130        return INDEX_NOT_FOUND;
131    }
132}