Skip to content

Instantly share code, notes, and snippets.

@RamadhanAmizudin
Last active June 19, 2023 18:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save RamadhanAmizudin/969aad4f293125e2acdee2d876a1063a to your computer and use it in GitHub Desktop.
Save RamadhanAmizudin/969aad4f293125e2acdee2d876a1063a to your computer and use it in GitHub Desktop.
Decrypt Cordova Crypt File Plugin
// 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);
}
@ostridgep
Copy link

Hi
I have tried too use this, it decrypts the files but misses off the last few bytes of each file
any suggestions?

@bonoadil
Copy link

bonoadil commented May 1, 2018

Hi @ostridgep, missing equal sign in line 80 :

decrypted + cipher.final('utf8');

should be :

decrypted += cipher.final('utf8');

@buffer1900
Copy link

Super Genius Guy.

You are a Profession.

@RamadhanAmizudin
Copy link
Author

Hi @ostridgep, missing equal sign in line 80 :

decrypted + cipher.final('utf8');

should be :

decrypted += cipher.final('utf8');

Updated the code, thanks!

@kero1998a
Copy link

any one have new code
i got an Erorr
Error: error:1C80006B:Provider routines::wrong final block length
at Decipheriv.final (node:internal/crypto/cipher:199:29)
at Decrypt (C:\Users\as\hello\AES256.js:78:25)
at C:\Users\as\hello\AES256.js:52:32
at Array.forEach ()
at DecryptThis (C:\Users\as\hello\AES256.js:49:8)
at Object. (C:\Users\as\hello\AES256.js:26:9)
at Module._compile (node:internal/modules/cjs/loader:1226:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
at Module.load (node:internal/modules/cjs/loader:1089:32)
at Module._load (node:internal/modules/cjs/loader:930:12) {
library: 'Provider routines',
reason: 'wrong final block length',
code: 'ERR_OSSL_WRONG_FINAL_BLOCK_LENGTH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment