1 13 14 package org.ejbca.util; 15 16 import java.io.UnsupportedEncodingException ; 17 import java.security.InvalidAlgorithmParameterException ; 18 import java.security.InvalidKeyException ; 19 import java.security.NoSuchAlgorithmException ; 20 import java.security.NoSuchProviderException ; 21 import java.security.spec.InvalidKeySpecException ; 22 import java.util.regex.Pattern ; 23 24 import javax.crypto.BadPaddingException; 25 import javax.crypto.Cipher; 26 import javax.crypto.IllegalBlockSizeException; 27 import javax.crypto.NoSuchPaddingException; 28 import javax.crypto.SecretKeyFactory; 29 import javax.crypto.spec.IvParameterSpec; 30 import javax.crypto.spec.PBEKeySpec; 31 import javax.crypto.spec.SecretKeySpec; 32 33 import org.apache.commons.lang.StringUtils; 34 import org.apache.log4j.Logger; 35 import org.bouncycastle.crypto.Digest; 36 import org.bouncycastle.crypto.PBEParametersGenerator; 37 import org.bouncycastle.crypto.digests.SHA256Digest; 38 import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator; 39 import org.bouncycastle.crypto.params.KeyParameter; 40 import org.bouncycastle.crypto.params.ParametersWithIV; 41 import org.bouncycastle.util.encoders.Hex; 42 43 48 public class StringTools { 49 private static Logger log = Logger.getLogger(StringTools.class); 50 51 private static final char[] stripChars = { 53 '\"', '\n', '\r', '\\', ';', '&', '!', '\0', '%', '`', '<', '>', '?', 54 '$', '~' 55 }; 56 private static final char[] stripSqlChars = { 58 '\'', '\"', '\n', '\r', '\\', ';', '&', '|', '!', '\0', '%', '`', '<', '>', '?', 59 '$', '~' 60 }; 61 private static final char[] allowedEscapeChars = { 63 ',' 64 }; 65 66 private static final Pattern WS = Pattern.compile("\\s+"); 67 68 75 public static String strip(String str) { 76 if (str == null) { 77 return null; 78 } 79 String ret = str; 80 for (int i = 0; i < stripChars.length; i++) { 81 if (ret.indexOf(stripChars[i]) > -1) { 82 if (stripChars[i] == '\\') { 84 int index = ret.indexOf('\\'); 86 while (index > -1) { 87 if (!isAllowed(ret.charAt(index+1))) { 88 ret = StringUtils.overlay(ret,"/",index,index+1); 89 } 90 index = ret.indexOf('\\',index+1); 91 } 92 } else { 93 ret = ret.replace(stripChars[i], '/'); 94 } 95 } 96 } 97 return ret; 98 } 106 public static boolean hasSqlStripChars(String str) { 107 if (str == null) { 108 return false; 109 } 110 String ret = str; 111 for (int i = 0; i < stripSqlChars.length; i++) { 112 if (ret.indexOf(stripSqlChars[i]) > -1) { 113 if (stripSqlChars[i] == '\\') { 115 int index = ret.indexOf('\\'); 117 while (index > -1) { 118 if (isAllowed(ret.charAt(index+1))) { 119 return true; 120 } 121 index = ret.indexOf('\\',index+1); 122 } 123 } else { 124 return true; 125 } 126 } 127 } 128 return false; 129 } 131 136 private static boolean isAllowed(char ch) { 137 boolean allowed = false; 138 for (int j = 0; j < allowedEscapeChars.length; j++) { 139 if (ch == allowedEscapeChars[j]) { 140 allowed = true; 141 break; 142 } 143 } 144 return allowed; 145 } 146 147 154 public static String stripWhitespace(String str) { 155 if (str == null) { 156 return null; 157 } 158 return WS.matcher(str).replaceAll(""); 159 } 160 161 166 public static byte[] ipStringToOctets(String str) { 167 String [] toks = str.split("[.:]"); 168 if (toks.length == 4) { 169 byte[] ret = new byte[4]; 171 for (int i = 0;i<toks.length;i++) { 172 int t = Integer.parseInt(toks[i]); 173 if (t>255) { 174 log.error("IPv4 address '"+str+"' contains octet > 255."); 175 return null; 176 } 177 ret[i] = (byte)t; 178 } 179 return ret; 180 } 181 if (toks.length == 8) { 182 byte[] ret = new byte[16]; 184 int ind = 0; 185 for (int i = 0;i<toks.length;i++) { 186 int t = Integer.parseInt(toks[i]); 187 if (t>0xFFFF) { 188 log.error("IPv6 address '"+str+"' contains part > 0xFFFF."); 189 return null; 190 } 191 int b1 = t & 0x00FF; 192 ret[ind++] = (byte)b1; 193 int b2 = t & 0xFF00; 194 ret[ind++] = (byte)b2; 195 } 196 } 197 log.error("Not a IPv4 or IPv6 address."); 198 return null; 199 } 200 201 207 public static String putBase64String(String s) { 208 if (StringUtils.isEmpty(s)) { 209 return s; 210 } 211 if (s.startsWith("B64:")) { 212 return s; 214 } 215 String n = null; 216 try { 217 n="B64:"+new String (Base64.encode(s.getBytes("UTF-8"), false)); 219 } catch (UnsupportedEncodingException e) { 220 n=s; 222 } 223 return n; 224 225 } 226 227 233 public static String getBase64String(String s) { 234 if (StringUtils.isEmpty(s)) { 235 return s; 236 } 237 String s1 = null; 238 if (s.startsWith("B64:")) { 239 s1 = s.substring(4); 240 String n = null; 241 try { 242 n = new String (Base64.decode(s1.getBytes("UTF-8")), "UTF-8"); 244 } catch (UnsupportedEncodingException e) { 245 n = s; 246 } catch (ArrayIndexOutOfBoundsException e) { 247 n = s; 249 } 250 return n; 251 } 252 return s; 253 } 254 255 262 public static String obfuscate(String s) 263 { 264 StringBuffer buf = new StringBuffer (); 265 byte[] b = s.getBytes(); 266 267 synchronized(buf) 268 { 269 buf.append("OBF:"); 270 for (int i=0;i<b.length;i++) 271 { 272 byte b1 = b[i]; 273 byte b2 = b[s.length()-(i+1)]; 274 int i1= b1+b2+127; 275 int i2= b1-b2+127; 276 int i0=i1*256+i2; 277 String x=Integer.toString(i0,36); 278 279 switch(x.length()) 280 { 281 case 1:buf.append('0'); break; 282 case 2:buf.append('0'); break; 283 case 3:buf.append('0'); break; 284 default:buf.append(x); break; 285 } 286 } 287 return buf.toString(); 288 } 289 } 290 291 296 public static String deobfuscate(String s) 297 { 298 if (s.startsWith("OBF:")) 299 s=s.substring(4); 300 301 byte[] b=new byte[s.length()/2]; 302 int l=0; 303 for (int i=0;i<s.length();i+=4) 304 { 305 String x=s.substring(i,i+4); 306 int i0 = Integer.parseInt(x,36); 307 int i1=(i0/256); 308 int i2=(i0%256); 309 b[l++]=(byte)((i1+i2-254)/2); 310 } 311 312 return new String (b,0,l); 313 } 314 private static final String q = "OBF:1m0r1kmo1ioe1ia01j8z17y41l0q1abo1abm1abg1abe1kyc17ya1j631i5y1ik01kjy1lxf"; 315 public static String pbeEncryptStringWithSha256Aes192(String in) throws NoSuchAlgorithmException , NoSuchProviderException , NoSuchPaddingException, InvalidKeyException , InvalidAlgorithmParameterException , IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 316 Digest digest = new SHA256Digest(); 317 final char[] p = deobfuscate(q).toCharArray(); 318 String saltStr = "1958473059684739584hfurmaqiekcmq"; 319 byte[] salt = saltStr.getBytes("UTF-8"); 320 int iCount = 100; 321 322 PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(digest); 323 pGen.init( 324 PBEParametersGenerator.PKCS12PasswordToBytes(p), 325 salt, 326 iCount); 327 328 ParametersWithIV params = (ParametersWithIV)pGen.generateDerivedParameters(192, 128); 329 SecretKeySpec encKey = new SecretKeySpec(((KeyParameter)params.getParameters()).getKey(), "AES"); 330 Cipher c; 331 c = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); 332 c.init(Cipher.ENCRYPT_MODE, encKey, new IvParameterSpec(params.getIV())); 333 334 byte[] enc = c.doFinal(in.getBytes("UTF-8")); 335 336 byte[] hex = Hex.encode(enc); 337 return new String (hex); 338 } 339 340 public static String pbeDecryptStringWithSha256Aes192(String in) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException , InvalidKeySpecException , NoSuchAlgorithmException , NoSuchProviderException , NoSuchPaddingException, UnsupportedEncodingException { 341 final char[] p = deobfuscate(q).toCharArray(); 342 String saltStr = "1958473059684739584hfurmaqiekcmq"; 343 byte[] salt = saltStr.getBytes("UTF-8"); 344 int iCount = 100; 345 346 Cipher c = Cipher.getInstance("PBEWithSHA256And192BitAES-CBC-BC", "BC"); 347 PBEKeySpec keySpec = new PBEKeySpec(p, salt, iCount); 348 SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWithSHA256And192BitAES-CBC-BC", "BC"); 349 350 c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec)); 351 352 byte[] dec = c.doFinal(Hex.decode(in.getBytes("UTF-8"))); 353 return new String (dec); 354 } 355 356 } | Popular Tags |