Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save autotrof/2ba7a59e21933f3bafb35f968d726db2 to your computer and use it in GitHub Desktop.
Save autotrof/2ba7a59e21933f3bafb35f968d726db2 to your computer and use it in GitHub Desktop.
// App.js root project
using rn-fetch-blob,node-forge, and buffer
import React from 'react';
import { StyleSheet, Text, View, PermissionsAndroid } from 'react-native';
import RNFS from 'react-native-fs';
import RNFetchBlob from 'rn-fetch-blob'
import forge from 'node-forge';
import {Buffer} from 'buffer';
const fs = RNFetchBlob.fs;
export default class App extends React.Component {
async componentWillMount(){
try {
const granted = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.CAMERA,
])
}catch (e){
console.warn(e)
}
}
// componentDidMount(){
// }
render(){
var key = forge.random.getBytesSync(16);
var iv = forge.random.getBytesSync(16);
var bufferSize = 1024*64;
var cipher = forge.cipher.createCipher('AES-CBC', key);
cipher.start({iv: iv});
var decipher = forge.cipher.createDecipher('AES-CBC', key);
decipher.start({iv: iv});
fs.ls(fs.dirs.DownloadDir)
.then(res=>{
fs.readStream('/sdcard/Download/d1.pdf','ascii',bufferSize)
.then(stream=>{
var c = [];
stream.open();
stream.onData(chunk=>{
c = c.concat(chunk)
})
stream.onEnd(()=>{
console.log('c',c)
// fs.writeFile('/sdcard/Download/2.copy.copy.png', c, 'ascii').then(()=>{console.log('copycopy')}) //this is working guys
cipher.update(forge.util.createBuffer(c,'utf8'));
cipher.finish();
var encrypted = cipher.output;
// console.log('encrypted : ',encrypted)
// console.log('encrypted array : ',Array.from(Buffer.from(encrypted.getBytes())))
fs.writeFile('/sdcard/Download/d1.pdf.enc',Array.from(Buffer.from(encrypted.getBytes())),'ascii')
.then(()=>{
console.log("FILE ENCRYPTED")
fs.readFile('/sdcard/Download/d1.pdf.enc','ascii')
.then(data=>{
console.log('data: ',data)
decipher.update(forge.util.createBuffer(data,'utf8'));
var result = decipher.finish();
var array_decrypted = Array.from(decipher.output.getBytes());
fs.writeFile('/sdcard/Download/d1.copy.dec.pdf', c, 'ascii')
.then(()=>{console.log('File Already decrypted')})
})
})
console.log("OKE")
})
})
})
return (
<View style={styles.container}>
<Text>Open up App.tsx to start working on your app!</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment