public ConcealCrypto create(){ if (this.context == null){ throw new RuntimeException("Context cannot be null"); } mEntityPassword = Entity.create(CipherUtils.obscureEncodeSixFourString(mEntityPasswordRaw.getBytes())); makeKeyChain = new SharedPrefsBackedKeyChain(this.context.get(),(mKeyChain==null)?CryptoConfig.KEY_256:mKeyChain); if (mKeyChain == null) { crypto = AndroidConceal.get().createDefaultCrypto(makeKeyChain); } else if (mKeyChain == CryptoConfig.KEY_128) { crypto = AndroidConceal.get().createCrypto128Bits(makeKeyChain); } else { crypto = AndroidConceal.get().createCrypto256Bits(makeKeyChain); } return new ConcealCrypto(this); }
@Override public String encrypt(String data) { long startTime = System.currentTimeMillis(); ESLog.v("%s=>encrypt(%s)", getClass().getSimpleName(), data); try { Entity entity = Entity.create(getEntityName()); byte[] encrypted = crypto.encrypt(data.getBytes(SSCharset.CHARSET), entity); ESLog.v("Encryption millis time: %d", System.currentTimeMillis() - startTime); return encoding.toBase64(encrypted); } catch (Exception e) { ESLog.wtf("Something went wrong %s", e.getCause()); throw new UnexpectedEncryptionException(e); } }
@Override public String decrypt(String data) { long startTime = System.currentTimeMillis(); ESLog.v("%s=>decrypt(%s)", getClass().getSimpleName(), data); try { Entity entity = Entity.create(getEntityName()); byte[] decodedData = encoding.fromBase64ToBytes(data); byte[] decrypted = crypto.decrypt(decodedData, entity); ESLog.v("Decryption millis time: %d", System.currentTimeMillis() - startTime); return new String(decrypted); } catch (Exception e) { ESLog.wtf("Something went wrong %s", e.getCause()); throw new UnexpectedDecryptionException(e); } }
public <T> T get(@NonNull String key, Type type) { T value = null; try { String hashKey = CommonCrypto.hashPrefKey(key); String old = mPreference.getString(hashKey, null); if (old != null) { Entity entity = Entity.create(key); byte[] data = mCrypto.decrypt(Base64.decode(old, Base64.NO_WRAP), entity); value = mGson.fromJson(new String(data, CharsetsSupport.UTF_8), type); } } catch (Exception e) { Timber.w(e); return null; } return value; }
@Override public EncryptionResult encrypt(@NonNull String service, @NonNull String username, @NonNull String password) throws CryptoFailedException { if (!crypto.isAvailable()) { throw new CryptoFailedException("Crypto is missing"); } Entity usernameEntity = createUsernameEntity(service); Entity passwordEntity = createPasswordEntity(service); try { byte[] encryptedUsername = crypto.encrypt(username.getBytes(Charset.forName("UTF-8")), usernameEntity); byte[] encryptedPassword = crypto.encrypt(password.getBytes(Charset.forName("UTF-8")), passwordEntity); return new EncryptionResult(encryptedUsername, encryptedPassword, this); } catch (Exception e) { throw new CryptoFailedException("Encryption failed for service " + service, e); } }
@Override public DecryptionResult decrypt(@NonNull String service, @NonNull byte[] username, @NonNull byte[] password) throws CryptoFailedException { if (!crypto.isAvailable()) { throw new CryptoFailedException("Crypto is missing"); } Entity usernameEntity = createUsernameEntity(service); Entity passwordEntity = createPasswordEntity(service); try { byte[] decryptedUsername = crypto.decrypt(username, usernameEntity); byte[] decryptedPassword = crypto.decrypt(password, passwordEntity); return new DecryptionResult( new String(decryptedUsername, Charset.forName("UTF-8")), new String(decryptedPassword, Charset.forName("UTF-8"))); } catch (Exception e) { throw new CryptoFailedException("Decryption failed for service " + service, e); } }
public ConcealStringSetPreference( final Crypto crypto, final SharedPreferences preferences, final String key, final Set<String> defaultValue) { super(preferences, key, defaultValue); this.crypto = crypto; this.entity = Entity.create(key); }
public ConcealStringPreference( final Crypto crypto, final SharedPreferences preferences, final String key, final String defaultValue) { super(preferences, key, defaultValue); this.crypto = crypto; this.entity = Entity.create(key); }
public SharedPreferences build() { if(!isInit) { Log.w(TAG, "You need call 'SecurePreferences.init()' in onCreate() from your application class."); } KeyChain keyChain = new SharedPrefsBackedKeyChain(context, CryptoConfig.KEY_256); Entity entity = Entity.create( TextUtils.isEmpty(password) ? getClass().getPackage().getName() : password ); return new SecurePreferences( context, keyChain, entity, sharedPrefFilename ); }
private SecurePreferences(Context context, final KeyChain keyChain, final Entity entity, final String sharedPrefFilename) { this.entity = entity; this.sharedPreferences = getSharedPreferenceFile(context, sharedPrefFilename); this.crypto = AndroidConceal.get().createCrypto256Bits(keyChain); }
public String decrypt(String key, String value, Type type) throws CryptoException { try { Entity entity = Entity.create(key); byte[] data = mFbCrypto.decrypt(Base64.decode(value, Base64.NO_WRAP), entity); return mGson.fromJson(new String(data, CharsetsSupport.UTF_8), type); } catch (Exception e) { throw new CryptoException(e); } }
@Override public String decrypt(String key, String value) throws Exception { Entity entity = Entity.create(key); byte[] decodedBytes = Base64.decode(value, Base64.NO_WRAP); byte[] bytes = crypto.decrypt(decodedBytes, entity); return new String(bytes); }
private void put(String key, String hashKey, String value) throws KeyChainException, CryptoInitializationException, IOException { Entity entity = Entity.create(key); // original key byte[] data = mCrypto.encrypt(value.getBytes(CharsetsSupport.UTF_8), entity); mPreference.edit().putString(hashKey, Base64.encodeToString(data, Base64.NO_WRAP)).apply(); }
public CryptoManager(Context context, String path, String password) { this.path = path; this.crypto = new Crypto( new SharedPrefsBackedKeyChain(context), new SystemNativeCryptoLibrary()); entity = new Entity(password); checkPathExists(); }
@Test public void testConceal() throws Exception { Entity entity = Entity.create("key"); String value = "value"; byte[] encryptedValue = crypto.encrypt(value.getBytes(), entity); assertThat(encryptedValue).isNotNull(); String decryptedValue = new String(crypto.decrypt(encryptedValue, entity)); assertThat(decryptedValue).isEqualTo("value"); }
public static void encryptingContent(Context context, File file, byte[] plainTextBytes) throws Exception { // Creates a new Crypto object with default implementations of // a key chain as well as native library. Crypto crypto = new Crypto( new SharedPrefsBackedKeyChain(context), new SystemNativeCryptoLibrary()); // Check for whether the crypto functionality is available // This might fail if android does not load libaries correctly. if (!crypto.isAvailable()) { return; } OutputStream fileStream = new BufferedOutputStream( new FileOutputStream(file)); // Creates an output stream which encrypts the data as // it is written to it and writes it out to the file. OutputStream outputStream = crypto.getCipherOutputStream( fileStream, new Entity("TEST1")); // Write plaintext to it. outputStream.write(plainTextBytes); outputStream.close(); }
public static void decryptingContent(Context context, File file, String newPath) throws Exception { // Get the file to which ciphertext has been written. FileInputStream fileStream = new FileInputStream(file); Crypto crypto = new Crypto( new SharedPrefsBackedKeyChain(context), new SystemNativeCryptoLibrary()); // Creates an input stream which decrypts the data as // it is read from it. InputStream inputStream = crypto.getCipherInputStream( fileStream, new Entity("TEST1")); // Read into a byte array. int read; byte[] buffer = new byte[1024]; // You must read the entire stream to completion. // The verification is done at the end of the stream. // Thus not reading till the end of the stream will cause // a security bug. FileOutputStream fs = new FileOutputStream(newPath); while ((read = inputStream.read(buffer)) != -1) { fs.write(buffer, 0, read); } inputStream.close(); }
/** * Encrypts the unencrypted file. Can throw a lot of errors * * @param encrypted * @param unencrypted * @param entityName * @throws IOException * @throws CryptoInitializationException * @throws KeyChainException */ @Override public void encrypt(File encrypted, File unencrypted, String entityName) throws IOException, CryptoInitializationException, KeyChainException { doFileChecks(unencrypted, encrypted); FileInputStream from = new FileInputStream(unencrypted); // Stream to read from source OutputStream to = crypto.getCipherOutputStream(new FileOutputStream(encrypted), new Entity(entityName)); // Stream to write to destination copyStreams(from, to); }
/** * Decrypts the encrypted file. Can also throw a lot of errors * * @param encrypted * @param unencrypted * @param entityName * @throws IOException * @throws CryptoInitializationException * @throws KeyChainException */ @Override public void decrypt(File encrypted, File unencrypted, String entityName) throws IOException, CryptoInitializationException, KeyChainException { doFileChecks(encrypted, unencrypted); InputStream from = crypto.getCipherInputStream(new FileInputStream(encrypted), new Entity(entityName)); FileOutputStream to = new FileOutputStream(unencrypted); copyStreams(from, to); }
public void setEntityPassword(Entity mEntityPassword) { if (mEntityPassword!=null)this.mEntityPassword = mEntityPassword; }
public void setEntityPassword(String password) { if (password!=null)this.mEntityPassword = Entity.create(CipherUtils.obscureEncodeSixFourString(password.getBytes())); }
public static String encrypt(Crypto crypto, String alias, String plainText) throws IOException, KeyChainException, CryptoInitializationException { final byte[] bytes = crypto.encrypt(plainText.getBytes(ENCODING), Entity.create(alias)); return Base64.encodeToString(bytes, BASE64_FLAG); }
public static String decrypt(Crypto crypto, String alias, String encryptedText) throws IOException, KeyChainException, CryptoInitializationException { final byte[] bytes = crypto.decrypt(Base64.decode(encryptedText, BASE64_FLAG), Entity.create(alias)); return new String(bytes, ENCODING); }
@Override public String encrypt(String key, String value) throws Exception { Entity entity = Entity.create(key); byte[] bytes = crypto.encrypt(value.getBytes(), entity); return Base64.encodeToString(bytes, Base64.NO_WRAP); }
private Entity createUsernameEntity(String service) { String prefix = getEntityPrefix(service); return Entity.create(prefix + "user"); }
private Entity createPasswordEntity(String service) { String prefix = getEntityPrefix(service); return Entity.create(prefix + "pass"); }
@Override public String encrypt(String key, String plainText) throws Exception { Entity entity = Entity.create(key); byte[] bytes = crypto.encrypt(plainText.getBytes(), entity); return Base64.encodeToString(bytes, Base64.NO_WRAP); }
@Override public String decrypt(String key, String cipherText) throws Exception { Entity entity = Entity.create(key); byte[] decodedBytes = Base64.decode(cipherText, Base64.NO_WRAP); byte[] bytes = crypto.decrypt(decodedBytes, entity); return new String(bytes); }