代码之家  ›  专栏  ›  技术社区  ›  Ron Astle Lobo

保护SD卡并通过应用程序访问

  •  2
  • Ron Astle Lobo  · 技术社区  · 7 年前

    我的SD卡上有分类数据。有没有办法只通过应用程序访问SD卡?( 反应本地项目 )?涉及的文件是 MP4 文件夹。

    我发现的大多数解决方案都是关于使用AES加密在SD卡中加密数据。但是我处理的是大文件,因此一次解密整个文件会导致 memory out exception .

    其次,逐流读取整个数据流是可能的,当我测试这个方法时,第一组流(加密的)将被成功解密。下一个流将导致错误 Malformed UTF-8 data .

    第三,我尝试将文件分割成多个部分,对每个部分进行解密,并将每个部分附加到一个期望的文件中。我遇到了这里规定的问题 Issue Description .

    编辑:

    用于加密的包: simple-crypto-js ,

    按流解密文件流的函数:

     decryptfile() {
    RNFetchBlob.fs
      .readStream(
        RNFetchBlob.fs.dirs.DCIMDir + "/encrypted.dat",
        "base64",
        4194303,
        30000      // 30 seconds buffer time before next stream comes
      )
      .then(stream => {
        let data = "";
        stream.open();
        stream.onData(chunk => {
          data += chunk;
          var decipherText = simpleCrypto.decrypt(chunk.toString());// First set of chunk will get successfully decrypted, the next chunk will result to Malformed UTF-8 data error
          this.writeOriginal(decipherText);
        });
        stream.onEnd(() => {
          console.log(data);
        });
      });
     }
    

    附加数据的函数:

    writeOriginal(decipherText) {
    RNFetchBlob.fs
      .appendFile(
        RNFetchBlob.fs.dirs.DCIMDir + "/encrypt.mp4",
        decipherText,
        "base64"
      )
      .then(() => {
    
        console.log("File Written");
      })
      .catch(error => {
        console.log("file writing error", error);
      });
    }
    

    这是一个离线学习应用程序,视频存储在SD卡中,并在应用程序中访问。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Ron Astle Lobo    7 年前

    使用的包是 rn-fetch-blob 和; simple-crypto-js

    逐流加密并在内存中写入:

    RNFetchBlob.fs
            .readStream(res.uri, "base64", 4194304, 4000)
            .then(stream => {
              let data = "";
              stream.open();
              stream.onData(chunk => {
                data += chunk;
                var cipherText = simpleCrypto.encrypt(chunk);
                RNFetchBlob.fs
                  .appendFile(
                    RNFetchBlob.fs.dirs.DCIMDir + "/encryptfile1.dat",
                    cipherText,
                    "base64"
                  )
                  .then(data => {
                    console.log("file written", data); // gives buffer size use it for reading while decrypting
                  })
                  .catch(error => {
                    console.log("file writing error", error);
                  });
              });
              stream.onEnd(() => {
                console.log(data.length);
              });
            });
    

    逐流解密:

    RNFetchBlob.fs
      .readStream(
        RNFetchBlob.fs.dirs.DCIMDir + "/encryptfile1.dat",
        "base64",
        5592464,
        4000
      )
      .then(stream => {
        let data = "";
        stream.open();
        stream.onData(chunk => {
          data += chunk;
          var decipherText = simpleCrypto.decrypt(chunk.toString());
          this.writeOriginal(decipherText);
        });
        stream.onEnd(() => {
          this.setState({ playvideo: !this.state.playvideo });
          console.log("file decrypted");
        });
      });
    

    函数附加解密的base64并生成.mp4文件:

    writeOriginal(decipherText) {
    RNFetchBlob.fs
      .appendFile(
        RNFetchBlob.fs.dirs.DCIMDir + "/encryptfile2.mp4",
        decipherText,
        "base64"
      )
      .then(() => {
        console.log("File Written");
      })
      .catch(error => {
        console.log("file writing error", error);
      });
    }
    
    推荐文章