android - java.io.IOException : Unexpected crypto version 0 异常

标签 android encryption cryptography ioexception facebook-conceal

我正在尝试使用 conceal(facebook) 来解密字节数组。

我的代码如下

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

解密时我做错了什么。我从昨天开始就卡住了。请帮忙。 提前致谢。

最佳答案

您试图在向文件写入任何内容之前从文件中读取加密数据。 尝试事先写入数据。 来自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();

关于android - java.io.IOException : Unexpected crypto version 0 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35221531/

相关文章:

android - 无法在自定义适配器中使用 LayoutInflater

javascript - 如何混淆音频标签的 src 属性

ruby - 如何将十六进制字符串转换回二进制 'SecureRandom.random_bytes'?

python - 使用 node.js crypto aes256 加密并使用 python2.7 PyCrypto 解密

Java:从小程序为 AES256 修补客户端安全策略

android - 随机放置对象时避免 Canvas 中的特定范围

javascript - 使用 onchronometerticklistener 的 if 语句

android - Android App XML 中的上标和下标

java.lang.NoClassDefFoundError : sun/security/util/DerValue

c# - .NET 密码学中 SignData() 和 CreateSignature() 的区别