[英]Secure a SD-CARD and access it through app
我已经在SD-CARD中对数据进行了分类。 有什么方法只能通过app( react-native-project )来访问SD-CARD吗? 涉及的文件是.mp4文件。
我发现的大多数解决方案都是关于使用AES加密对SD-CARD中的数据进行加密。 但是我正在处理大文件,因此立即解密整个文件将抛出memory out exception
。
其次,有可能逐流读取整个数据流,当我测试此方法时,第一组流(已加密)将被成功解密。 下一个流将导致错误Malformed UTF-8 data
。
第三,我尝试将文件分成多个部分,解密每个部分,然后将它们附加在一起以制成预期的文件。 我遇到了此处问题说明中指定的问题 。
编辑:
用于加密的软件包: 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卡中,并可以在应用程序中访问。
使用的软件包是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);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.