001    /**
002     * Implements the CFML Function decrypt
003     */
004    package railo.runtime.functions.other;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.coder.Coder;
008    import railo.runtime.crypt.CFMXCompat;
009    import railo.runtime.crypt.Cryptor;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.ext.function.Function;
012    import railo.runtime.op.Caster;
013    
014    //FUTURE 2 attr fehlen noch
015    
016    public final class Decrypt implements Function {
017        
018        
019        public synchronized static String call(PageContext pc , String string, String key) throws PageException {
020            return call(pc,string,key,"cfmx_compat","uu");
021            }
022    
023        public synchronized static String call(PageContext pc , String string, String key, String algorithm) throws PageException {
024            return call(pc,string,key,algorithm,"uu");
025            }
026        public synchronized static String call(PageContext pc , String string, String key, String algorithm, String encoding) throws PageException {
027            return invoke(string, key, algorithm, encoding);
028            }
029        
030    
031        protected synchronized static String invoke(String string, String key, String algorithm, String encoding) throws PageException {
032            try {
033                            return new String(invoke(Coder.decode(encoding, string), key, algorithm),"UTF-8");
034                    } 
035            catch (Exception e) {
036                            throw Caster.toPageException(e);
037                    }
038            }
039        protected synchronized static byte[] invoke(byte[] input, String key, String algorithm) throws PageException {
040            try {
041                    if("cfmx_compat".equalsIgnoreCase(algorithm)){
042                            return new CFMXCompat().transformString(key, input);
043                    }
044                    return Cryptor.decrypt(algorithm, key, input);  
045                    } 
046            catch (Exception e) {
047                            throw Caster.toPageException(e);
048                    }
049            }
050    }