001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library 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 GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/**
020 * Implements the CFML Function decrypt
021 */
022package lucee.runtime.functions.other;
023
024import lucee.runtime.PageContext;
025import lucee.runtime.coder.Coder;
026import lucee.runtime.crypt.CFMXCompat;
027import lucee.runtime.crypt.Cryptor;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.ext.function.Function;
030import lucee.runtime.op.Caster;
031
032
033public final class Decrypt implements Function {
034
035
036    public synchronized static String call( PageContext pc, String input, String key ) throws PageException {
037
038        return invoke(input, key, CFMXCompat.ALGORITHM_NAME, Cryptor.DEFAULT_ENCODING, null, 0);
039    }
040
041
042    public synchronized static String call( PageContext pc, String input, String key, String algorithm ) throws PageException {
043
044        return invoke(input, key, algorithm, Cryptor.DEFAULT_ENCODING, null, 0);
045    }
046
047
048    public synchronized static String call( PageContext pc, String input, String key, String algorithm, String encoding ) throws PageException {
049
050        return invoke( input, key, algorithm, encoding, null, 0 );
051    }
052
053
054    public synchronized static String call( PageContext pc, String input, String key, String algorithm, String encoding, Object ivOrSalt ) throws PageException {
055
056        return invoke(input, key, algorithm, encoding, ivOrSalt, 0);
057    }
058
059
060    /**
061     * call with all optional args
062     */
063    public synchronized static String call( PageContext pc, String input, String key, String algorithm, String encoding, Object ivOrSalt, double iterations ) throws PageException {
064
065        return invoke( input, key, algorithm, encoding, ivOrSalt, Caster.toInteger( iterations ) );
066    }
067
068
069    public synchronized static String invoke( String input, String key, String algorithm, String encoding, Object ivOrSalt, int iterations ) throws PageException {
070
071        try {
072
073            if ( CFMXCompat.isCfmxCompat( algorithm ) )
074                return new String( invoke( Coder.decode( encoding, input ), key, algorithm, null, 0 ), Cryptor.DEFAULT_CHARSET );
075
076            byte[] baIVS = null;
077            if ( ivOrSalt instanceof String )
078                baIVS = ((String)ivOrSalt).getBytes( Cryptor.DEFAULT_CHARSET );
079            else if ( ivOrSalt != null )
080                baIVS = Caster.toBinary( ivOrSalt );
081
082            return Cryptor.decrypt( input, key, algorithm, baIVS, iterations, encoding, Cryptor.DEFAULT_CHARSET  );
083        }
084        catch (Throwable t ) {
085            throw Caster.toPageException( t );
086        }
087    }
088
089
090    public synchronized static byte[] invoke( byte[] input, String key, String algorithm, byte[] ivOrSalt, int iterations ) throws PageException {
091
092        if ( CFMXCompat.isCfmxCompat( algorithm ) )
093            return new CFMXCompat().transformString( key, input );
094
095        return Cryptor.decrypt( input, key, algorithm, ivOrSalt, iterations );
096    }
097}