java.io.IOException:意外的加密版本0

时间:2016-02-05 10:16:47

标签: android encryption cryptography ioexception facebook-conceal

我试图使用隐藏(facebook)来解密bytearray。

我的代码如下

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();

我收到以下错误

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

我在解密时做错了什么。我从去年开始就陷入了困境。请帮忙。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

您尝试在向文件写入任何内容之前从文件中读取加密数据。 尝试预先写入数据。 来自conceal github页面:

// 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();