使用的包是
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);
});
}