-
-
Save epsi95/1cea29508a66f716ebaa51919398b0f2 to your computer and use it in GitHub Desktop.
Decrypt Cordova Crypt File Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Blogpost: http://blog.rz.my/2017/11/decrypting-cordova-crypt-file-plugin.html | |
var fs = require("fs"), | |
path = require("path"), | |
crypto = require("crypto"); | |
var config = { | |
key : 'CRYPT_KEY', | |
iv : 'CRYPT_IV' | |
} | |
if (process.argv.length < 4) { | |
console.log("\nUsage:\nnode app.js <options> <folder>\n"); | |
console.log("Options:"); | |
console.log("\t-e\t\t: Encrypt"); | |
console.log("\t-d\t\t: Decrypt"); | |
process.exit(1); | |
} | |
encdec = process.argv[2]; | |
folder = process.argv[3]; | |
if( fs.existsSync( folder ) ) { | |
console.log(folder + " exists"); | |
if( encdec == '-e' ) { | |
EncryptThis(folder); | |
} else if( encdec == '-d' ) { | |
DecryptThis(folder); | |
} else { | |
console.log("Invalid Options"); | |
process.exit(1); | |
} | |
} else { | |
console.log( folder + " doesn't exists"); | |
process.exit(1); | |
} | |
function EncryptThis(folder) { | |
findCryptFiles(folder).filter(function(file) { | |
return isCryptFile(file); | |
}).forEach(function(file) { | |
var content = fs.readFileSync(file, 'utf-8'); | |
fs.writeFileSync(file, Encrypt(content, config.key, config.iv), 'utf-8'); | |
console.log('Encrypt: ' + file); | |
}); | |
} | |
function DecryptThis(folder) { | |
findCryptFiles(folder).filter(function(file) { | |
return isCryptFile(file); | |
}).forEach(function(file) { | |
var content = fs.readFileSync(file, 'utf-8'); | |
content = Buffer.from(content, 'base64').toString('binary'); | |
fs.writeFileSync(file, Decrypt(content, config.key, config.iv), 'utf-8'); | |
console.log('Decrypt: ' + file); | |
}); | |
} | |
function findCryptFiles(dir) { | |
var fileList = []; | |
var list = fs.readdirSync(dir); | |
list.forEach(function(file) { | |
fileList.push(path.join(dir, file)); | |
}); | |
list.filter(function(file) { | |
return fs.statSync(path.join(dir, file)).isDirectory(); | |
}).forEach(function(file) { | |
var subDir = path.join(dir, file) | |
var subFileList = findCryptFiles(subDir); | |
fileList = fileList.concat(subFileList); | |
}); | |
return fileList; | |
} | |
function Decrypt(Input, Key, Iv) { | |
var cipher = crypto.createDecipheriv('aes-256-cbc', Key, Iv); | |
var decrypted = cipher.update(Input, 'binary', 'utf-8'); | |
decrypted + cipher.final('utf8'); | |
return decrypted; | |
} | |
function Encrypt(Input, Key, Iv) { | |
var cipher = crypto.createCipheriv('aes-256-cbc', Key, Iv); | |
var encrypted = cipher.update(Input, 'utf8', 'base64') + cipher.final('base64'); | |
return encrypted; | |
} | |
function isCryptFile(file) { | |
re = /\.(htm|html|js|css)$/; | |
return new RegExp(re).test(file); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment