1 64 65 package com.jcorporate.expresso.core.misc; 66 67 72 73 import com.jcorporate.expresso.core.security.CryptoManager; 74 import com.jcorporate.expresso.kernel.exception.ChainedException; 75 76 77 84 public class CookieUtil { 85 86 public CookieUtil() { 87 } 88 89 97 static public String cookieDecode(String data) 98 throws Exception { 99 try { 100 if (data == null || data.length() == 0 || data.equals("NONE")) { 101 return (""); 102 } 103 104 return CryptoManager.getInstance().getStringEncryption().decryptString( 105 CookieBase64.decodeNoPadding(URLUTF8Encoder.decode(data))); 106 107 } catch (com.jcorporate.expresso.kernel.exception.ChainedException ex) { 108 return ""; 109 } catch (IllegalArgumentException ex) { 110 111 return (""); 113 } catch (IllegalStateException ise) { 114 return (""); 115 } 116 } 117 118 119 128 static public String cookieEncode(String data) 129 throws ChainedException { 130 if (data == null || data.length() == 0) { 131 return (""); 132 } 133 try { 134 return URLUTF8Encoder.encode(CookieBase64.encodeNoPadding(CryptoManager.getInstance().getStringEncryption().encryptString( 135 data))); 136 } catch (IllegalArgumentException ex) { 137 return (""); 138 } catch (IllegalStateException ise) { 139 return (""); 140 } 141 } 142 143 144 public static void test() { 145 try { 146 String original = "testPassword"; 147 String encoded = CookieUtil.cookieEncode(original); 148 System.out.println("Encoded:" + encoded); 149 150 String decoded = CookieUtil.cookieDecode(encoded); 151 152 if (original.equals(decoded)) { 153 System.out.println("Decoded correctly"); 154 } else { 155 System.out.println("Decode failed: '" + decoded + "'"); 156 } 157 158 original = "yes"; 159 encoded = CookieUtil.cookieEncode(original); 160 System.out.println("Encoded:" + encoded); 161 decoded = CookieUtil.cookieDecode(encoded); 162 163 if (original.equals(decoded)) { 164 System.out.println("Decoded correctly"); 165 } else { 166 System.out.println("Decode failed: '" + decoded + "'"); 167 } 168 169 System.out.println("Decoded:'" + 170 CookieUtil.cookieDecode("ZkRLGAfiT7e") + "'"); 171 } catch (Exception e) { 172 e.printStackTrace(System.out); 173 } 174 } 175 } 176 | Popular Tags |