in app/src/main/java/com/epam/securestorage/providers/cipher/CipherEncryptionProvider.java [114:150]
public void save(@NonNull String key, @NonNull String value) {
if (key == null || value == null || key.isEmpty() || value.isEmpty()) {
if (callback != null) {
callback.onError(SAVE, new SecureStorageException("Key or Value can't be NULL or empty"));
}
return;
}
try {
key = generateKeyWithPrefix(key);
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(KEY_ALIAS, null);
// Encrypt the text
Cipher inputCipher = Cipher.getInstance(CIPHER_TYPE, CIPHER_PROVIDER);
inputCipher.init(Cipher.ENCRYPT_MODE, privateKeyEntry.getCertificate().getPublicKey());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, inputCipher);
cipherOutputStream.write(value.getBytes(StandardCharsets.UTF_8));
cipherOutputStream.close();
byte[] cryptoText = outputStream.toByteArray();
String encryptedString = (Base64.encodeToString(cryptoText, Base64.DEFAULT));
putPref(key, encryptedString);
outputStream.close();
if (callback != null) {
callback.onComplete(SAVE);
}
} catch (NoSuchAlgorithmException | KeyStoreException | InvalidKeyException | IOException | NoSuchPaddingException | UnrecoverableEntryException | NoSuchProviderException e) {
e.printStackTrace();
if (callback != null) {
callback.onError(SAVE, e);
}
}
}