private String stripFormData(String data, MediaType type, boolean cipher) { if (data.endsWith("=") && !type.equals(MediaType.TEXT_PLAIN)) { try { data = URLDecoder.decode(data, "UTF-8"); if (cipher) { data = data.replace(" ", "+"); } } catch (UnsupportedEncodingException e) { // Really? } String candidate = data.substring(0, data.length() - 1); if (cipher) { if (data.endsWith("=")) { if (data.length() / 2 != (data.length() + 1) / 2) { try { Hex.decode(candidate); return candidate; } catch (IllegalArgumentException e) { if (Base64.isBase64(data.getBytes())) { return data; } } } } return data; } // User posted data with content type form but meant it to be text/plain data = candidate; } return data; }
public AesBytesEncryptor( String password, CharSequence salt, BytesKeyGenerator ivGenerator, CipherAlgorithm alg) { PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), Hex.decode(salt), 1024, 256); SecretKey secretKey = newSecretKey("PBKDF2WithHmacSHA1", keySpec); this.secretKey = new SecretKeySpec(secretKey.getEncoded(), "AES"); this.alg = alg; this.encryptor = alg.createCipher(); this.decryptor = alg.createCipher(); this.ivGenerator = ivGenerator != null ? ivGenerator : alg.defaultIvGenerator(); }