@Override public void set(final Set<String> value) { final Set<String> encryptedSet = new HashSet<>(value.size()); for (final String s : value) { try { encryptedSet.add(Base64.encodeToString( crypto.encrypt( s.getBytes(Charset.defaultCharset()), entity ), Base64.NO_WRAP )); } catch (KeyChainException | CryptoInitializationException | IOException e) { Log.e(TAG, e.getMessage()); encryptedSet.add(null); } } preferences.edit().putStringSet(key, encryptedSet).apply(); }
@Override public void set(final String value) { try { if (value != null) { super.set(Base64.encodeToString( crypto.encrypt( value.getBytes(Charset.defaultCharset()), entity ), Base64.NO_WRAP )); } else { delete(); } } catch (KeyChainException | CryptoInitializationException | IOException e) { Log.e(TAG, e.getMessage()); } }
@Override public synchronized byte[] getCipherKey() throws KeyChainException { if (!setCipherKey) { PasswordBasedKeyDerivation derivation = new PasswordBasedKeyDerivation(secureRandom, nativeCryptoLibrary); derivation.setPassword(password); derivation.setSalt(password.getBytes()); derivation.setIterations(ITERATION_COUNT); derivation.setKeyLengthInBytes(cryptoConfig.keyLength); try { cipherKey = derivation.generate(); } catch (CryptoInitializationException e) { return null; } } setCipherKey = true; return cipherKey; }
private String encrypt(final String plainText) { if (TextUtils.isEmpty(plainText)) { return plainText; } byte[] cipherText = null; if (!crypto.isAvailable()) { log(Log.WARN, "encrypt: crypto not available"); return null; } try { cipherText = crypto.encrypt(plainText.getBytes(), entity); } catch (KeyChainException | CryptoInitializationException | IOException e) { log(Log.ERROR, "encrypt: " + e); } return cipherText != null ? Base64.encodeToString(cipherText, Base64.DEFAULT) : null; }
private String decrypt(final String encryptedText) { if (TextUtils.isEmpty(encryptedText)) { return encryptedText; } byte[] plainText = null; if (!crypto.isAvailable()) { log(Log.WARN, "decrypt: crypto not available"); return null; } try { plainText = crypto.decrypt(Base64.decode(encryptedText, Base64.DEFAULT), entity); } catch (KeyChainException | CryptoInitializationException | IOException e) { log(Log.ERROR, "decrypt: " + e); } return plainText != null ? new String(plainText) : null; }
private byte[] maybeGenerateKey(String key, int length) throws KeyChainException, JAQException { byte[] data; String salt = mSharedPreferences.getString(CommonCrypto.hashPrefKey(key), ""); if (!TextUtils.isEmpty(salt)) { try { data = BeeCrypto.get().decrypt(salt.getBytes(CharsetsSupport.UTF_8)); data = Base64.decode(data, Base64.NO_WRAP); } catch (Exception e) { Timber.w(e, "get key from preferences failure"); data = generateKeyAndSave(key, length); } } else { data = generateKeyAndSave(key, length); } return data; }
@RequiresPermission(allOf = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}) public File obscureFile(File file,boolean deleteOldFile){ if (enableCrypto) { try { boolean isImage = FileUtils.isFileForImage(file); File mEncryptedFile = new File(makeDirectory()+DEFAULT_PREFIX_FILENAME+file.getName()); OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(mEncryptedFile)); OutputStream outputStream = crypto.getCipherOutputStream(fileStream, mEntityPassword); int read; byte[] buffer = new byte[1024]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); while ((read = bis.read(buffer)) != -1) { outputStream.write(buffer, 0, read); } outputStream.close(); bis.close(); if (deleteOldFile) file.delete(); File pathDir = new File(isImage?makeImagesDirectory():makeFileDirectory()); return FileUtils.moveFile(mEncryptedFile,pathDir); } catch (KeyChainException | CryptoInitializationException | IOException e) { e.printStackTrace(); return null; } } else { return file; } }
@Override public byte[] getMacKey() throws KeyChainException { if (!setMacKey) { macKey = new byte[NativeMac.KEY_LENGTH]; secureRandom.nextBytes(macKey); } setMacKey = true; return macKey; }
@Override public byte[] getCipherKey() throws KeyChainException { if (mCipherKey == null) { try { mCipherKey = maybeGenerateKey(CIPHER_KEY, mCryptoConfig.keyLength); } catch (JAQException e) { Timber.w(e, "generate cipher key failure"); throw new KeyChainException(e.getMessage(), e); } } return mCipherKey; }
@Override public byte[] getMacKey() throws KeyChainException { if (mMacKey == null) { try { mMacKey = maybeGenerateKey(MAC_KEY, 64); } catch (JAQException e) { throw new KeyChainException(e.getMessage(), e); } } return mMacKey; }
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 Bitmap decryptPhoto(String filename) throws IOException, CryptoInitializationException, KeyChainException { FileInputStream fileStream = new FileInputStream(path + filename); InputStream inputStream = crypto.getCipherInputStream(fileStream, entity); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); inputStream.close(); return bitmap; }
/** * 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); }
@RequiresPermission(allOf = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}) public File deObscureFile(File file,boolean deleteOldFile){ if (enableCrypto) { try { if (file.getName().contains(DEFAULT_PREFIX_FILENAME)) { boolean isImage = FileUtils.isFileForImage(file); File mDecryptedFile = new File(makeDirectory() + file.getName().replace(DEFAULT_PREFIX_FILENAME,"")); InputStream inputStream = crypto.getCipherInputStream(new FileInputStream(file), mEntityPassword); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream outputStream = new FileOutputStream(mDecryptedFile); BufferedInputStream bis = new BufferedInputStream(inputStream); int mRead; byte[] mBuffer = new byte[1024]; while ((mRead = bis.read(mBuffer)) != -1) { outputStream.write(mBuffer, 0, mRead); } bis.close(); out.writeTo(outputStream); inputStream.close(); outputStream.close(); out.close(); if (deleteOldFile) file.delete(); File pathDir = new File(isImage?makeImagesDirectory():makeFileDirectory()); return FileUtils.moveFile(mDecryptedFile, pathDir); } return null; } catch (KeyChainException | CryptoInitializationException | IOException e) { e.printStackTrace(); return null; } } return file; }
@Override public byte[] getNewIV() throws KeyChainException { byte[] iv = new byte[cryptoConfig.ivLength]; secureRandom.nextBytes(iv); return iv; }
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 byte[] getNewIV() throws KeyChainException { byte[] iv = new byte[mCryptoConfig.ivLength]; mSecureRandom.nextBytes(iv); return iv; }
public void savePhotoEncrypted(Bitmap imageBitmap, String filename) throws KeyChainException, CryptoInitializationException, IOException { FileOutputStream fileStream = new FileOutputStream(path + filename); OutputStream outputStream = crypto.getCipherOutputStream(fileStream, entity); imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.close(); }
/** * Encrypts a file input stream to the given file * @param encrypted file to be written to. Cannot be a directory * @param unencrypted the original file to be encrypted * @exception java.io.IOException thrown when the operation fails, either because the encrypted * file already exists, or something failed during encryption */ public void encrypt(File encrypted, File unencrypted, String entityName) throws IOException, CryptoInitializationException, KeyChainException;
public void decrypt(File encrypted, File unencrypted, String entityName) throws IOException, CryptoInitializationException, KeyChainException;