1

I am trying to decrypt the bytearray by using conceal(facebook).

my Code is as below

Log.d("Esource", " intial buffer size = " + buffer.length);
    Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(this), new SystemNativeCryptoLibrary());
    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(buffer);
    InputStream inputStream = null;
    try {
        inputStream = crypto.getCipherInputStream(byteInputStream, new Entity("Password"));
        Log.d("Esource", "applied decryption ");
    } catch (CryptoInitializationException e) {
        Log.d("Esource", "applied decryption e = " + e.getMessage());
        e.printStackTrace();
    } catch (KeyChainException e) {
        Log.d("Esource", "applied decryption e = " + e.getMessage());
        e.printStackTrace();
    } catch (Exception e) {
        Log.d("ESource", "applied decryption e = " + e.getMessage());
        e.printStackTrace();
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    int read;
    byte[] dBuffer = new byte[readLength];
    if (inputStream != null) {
        while ((read = inputStream.read(dBuffer)) != -1) {
            out.write(dBuffer, 0, read);
        }
    } else
        Log.d("Esource", "inputSTream after cipher is null");
    buffer = out.toByteArray();

I am getting below error

ava.io.IOException: Unexpected crypto version 0
at com.facebook.crypto.util.Assertions.checkArgumentForIO(Assertions.java:29)
at com.facebook.crypto.CipherHelper.getCipherInputStream(CipherHelper.java:52)
at com.facebook.crypto.Crypto.getCipherInputStream(Crypto.java:83)
at com.exoplayer.EncryptedDataSource.read(EncryptedDataSource.java:186)
at       com.google.android.exoplayer.extractor.DefaultExtractorInput.peekFully(DefaultExtractorInput.java:135)
at com.google.android.exoplayer.extractor.webm.Sniffer.sniff(Sniffer.java:52)
at com.google.android.exoplayer.extractor.webm.WebmExtractor.sniff(WebmExtractor.java:258)
at com.google.android.exoplayer.extractor.ExtractorSampleSource$ExtractorHolder.selectExtractor(ExtractorSampleSource.java:805)
at com.google.android.exoplayer.extractor.ExtractorSampleSource$ExtractingLoadable.load(ExtractorSampleSource.java:746)
at com.google.android.exoplayer.upstream.Loader$LoadTask.run(Loader.java:209)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.u

What i am doing wrong while decryption.I am stuck since last day . Please help. Thanks in advance.

SimpleCoder
  • 1,665
  • 1
  • 21
  • 34

1 Answers1

1

You are trying to read encrypted data from the file before you have written anything to it. Try writing data beforehand. From the conceal github page:

// Creates a new Crypto object with default implementations of a key chain
KeyChain keyChain = new SharedPrefsBackedKeyChain(context, CryptoConfig.KEY_256);
Crypto crypto = AndroidConceal.get().createDefaultCrypto(keyChain);

// 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,
  Entity.create("entity_id"));

// Write plaintext to it.
outputStream.write(plainText);
outputStream.close();