Last active
August 25, 2024 10:42
-
-
Save RamadhanAmizudin/969aad4f293125e2acdee2d876a1063a to your computer and use it in GitHub Desktop.
Decrypt Cordova Crypt File Plugin
This file contains 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: https://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); | |
} |
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
Updated the code, thanks!