01: /*
02: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05: package com.sun.portal.rewriter.util.crypto;
06:
07: import com.iplanet.services.util.Base64;
08: import com.sun.portal.rewriter.util.ConfigManager;
09:
10: import java.net.URLDecoder;
11:
12: public class CryptoHelper {
13: public static final String OBSCURE_URI_STR = "/.OBSCURE.";
14: public final static String PROPERTY_SECRET_KEY = "CRYPTO_SECRET_KEY";
15: private final static String SECRET_KEY;
16:
17: private final static String CRYPTO_BEGIN_SUFFIX;
18: private final static String CRYPTO_END_SUFFIX;
19:
20: static {
21: SECRET_KEY = ConfigManager.getProperty(PROPERTY_SECRET_KEY,
22: "DefaultSecretKeyPassword");
23: CRYPTO_BEGIN_SUFFIX = RandomString.create(SECRET_KEY);
24: CRYPTO_END_SUFFIX = RandomString.create(CRYPTO_BEGIN_SUFFIX);
25: }//static block
26:
27: public static String encode(String aString) {
28: //if encrypted once send the same
29: if (aString == null || aString.length() == 0
30: || aString.startsWith(CRYPTO_BEGIN_SUFFIX)) {
31: return aString + OBSCURE_URI_STR;
32: }
33:
34: return CRYPTO_BEGIN_SUFFIX + Base64.encode(aString.getBytes())
35: + CRYPTO_END_SUFFIX;
36: }//encode()
37:
38: public static String decode(String aString) {
39: //if not encrypted send the same
40: if (aString == null || aString.length() == 0) {
41: return aString;
42: }
43: if (aString.endsWith(OBSCURE_URI_STR)) {
44: aString = aString.substring(0, aString
45: .indexOf(OBSCURE_URI_STR));
46:
47: }
48:
49: StringBuffer sb = new StringBuffer(aString.length());
50:
51: int startIndex = 0;
52: int endIndex = -1;
53: while ((endIndex = aString.indexOf(CRYPTO_BEGIN_SUFFIX,
54: startIndex)) != -1) {
55: sb.append(aString.substring(startIndex, endIndex));
56: startIndex = endIndex;
57: endIndex = aString.indexOf(CRYPTO_END_SUFFIX, startIndex);
58: if (endIndex != -1) {
59: String encodedPart = aString.substring(startIndex
60: + CRYPTO_BEGIN_SUFFIX.length(), endIndex);
61: //if some encoded part is part of query string and jsapplications escape this
62: //in some case = in Base64 is converted to %3D after URLEncoded or unescape() of JScript
63: //hence we need to revert it. This was observed during SAP integration
64: //Bug No:4981732
65: encodedPart = URLDecoder.decode(encodedPart);
66: sb.append(new String(Base64.decode(encodedPart)));
67: startIndex = endIndex + CRYPTO_END_SUFFIX.length();
68: } else {
69: sb.append(aString.substring(startIndex, startIndex
70: + CRYPTO_BEGIN_SUFFIX.length()));
71: startIndex = startIndex + CRYPTO_BEGIN_SUFFIX.length();
72: }
73: }//while loop
74: sb.append(aString.substring(startIndex));
75: return sb.toString();
76: }//decode()
77:
78: }//CryptoHelper
|