/* * Copyright 2013, Leanplum, Inc. All rights reserved. * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package com.leanplum.internal; import android.content.SharedPreferences; import android.util.Pair; import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import java.util.Arrays; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; /** * AES Encryption as detailed at * http://nelenkov.blogspot.com/2012/04/using-password-based-encryption-on.html * * @author Aakash Patel */ public class AESCrypt { private static enum EncryptionType { /** * Encryption based on a token received from the server. Used in SDK versions prior to 1.2.20. *
* Corresponds to ciphertexts of the format "[12, -33, 52]", corresponding to Arrays.toString() * of an encrypted byte[]. *
* Legacy values may decrypt to ciphertexts. We ignore what appear to be double-encrypted * ciphertexts that are stored in a legacy format. */ LEGACY_TOKEN(0), /** * Encryption based on the app ID. Used in SDK versions since 1.2.20. *
* Corresponds to ciphertexts of the format "01[12, -33, 52]". The format adds a version * identifier ("01") prefix to ciphertexts, allowing us to change the format in the future. *
* With the exception of LEGACY_TOKEN ciphertexts, which must continue to be supported, we will
* use the first two characters to determine the encryption protocol.
*/
APP_ID_KEY(1);
public final int id;
public final String prefix;
public final String prefixWithBracket;
EncryptionType(int id) {
this.id = id;
prefix = String.format("%02d", id);
prefixWithBracket = prefix + "[";
}
private static EncryptionType forId(int id) {
if (id == 1) {
return APP_ID_KEY;
}
return null;
}
public static Pair
* Intended for short-term use, since the encryption token can change.
*/
public AESCrypt(String appId, String token) {
this.appId = appId;
this.token = token;
}
private String appIdKeyPassword() {
return APP_ID_KEY_PREFIX + appId + APP_ID_KEY_SUFFIX;
}
/**
* Creates a ciphertext using a password based on current context parameters.
*
* @param plaintext
* @return A cipher text string, or null if encryption fails (unexpected).
*/
public String encrypt(String plaintext) {
if (plaintext == null) {
return null;
}
// Always encrypt using the APP_ID_KEY method.
if (appId == null || appId.isEmpty()) {
Log.e("Encrypt called with null appId.");
return null;
}
String cipherText = encryptInternal(appIdKeyPassword(), plaintext);
if (cipherText == null) {
Log.w("Failed to encrypt.");
return null;
}
if (cipherText.isEmpty() || cipherText.equals(plaintext) || !cipherText.startsWith("[")) {
Log.w("Invalid ciphertext: " + cipherText);
return null;
}
return EncryptionType.APP_ID_KEY.prefix + cipherText;
}
public String decodePreference(SharedPreferences preferences, String key, String defaultValue) {
String cipherText = preferences.getString(key, null);
if (cipherText == null) {
return defaultValue;
}
String decoded = decrypt(cipherText);
if (decoded == null) {
return defaultValue;
}
return decoded;
}
/**
* Decrypts a ciphertext in either legacy or current format, using a password based on context
* parameters.
*
* @param cipherText The value to encrypt; tolerates null.
* @return A cipher text string, or null if the value can't be decrypted.
*/
public String decrypt(String cipherText) {
Pair