Skip to content

Commit 0c71078

Browse files
authored
Merge pull request #112 from TurboRx/fix/legacy-crypto-decipher-fallbacks
Fix legacy cipher decryption in crypto tools and handlers
2 parents 2795640 + edc17cc commit 0c71078

3 files changed

Lines changed: 48 additions & 12 deletions

File tree

‎src/data-access/mysql-json-db-crypto.js‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,22 @@ function encrypt(text, algorithm, password) {
3333
*/
3434
function decrypt(text, algorithm, password) {
3535
if (text.indexOf(":") === -1) {
36-
let decipher = Crypto.createDecipher(algorithm, password);
37-
let data = decipher.update(text, 'hex', 'utf8');
38-
data += decipher.final('utf8');
39-
return data;
36+
try {
37+
if (typeof Crypto.createDecipher === 'function') {
38+
let decipher = Crypto.createDecipher(algorithm, password);
39+
let data = decipher.update(text, 'hex', 'utf8');
40+
data += decipher.final('utf8');
41+
return data;
42+
}
43+
const hash = Crypto.createHash('md5');
44+
hash.update(password);
45+
let decipher = Crypto.createDecipheriv(algorithm, hash.digest(), Buffer.alloc(0));
46+
let data = decipher.update(text, 'hex', 'utf8');
47+
data += decipher.final('utf8');
48+
return data;
49+
} catch (e) {
50+
return "";
51+
}
4052
} else {
4153
const parts = text.split(":");
4254
const iv = Buffer.from(parts[0], 'hex');

‎src/server/handlers/tools.js‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,22 @@ exports.setup = function (App) {
409409
*/
410410
function decrypt(text, algorithm, password) {
411411
if (text.indexOf(":") === -1) {
412-
let decipher = Crypto.createDecipher(algorithm, password);
413-
let data = decipher.update(text, 'hex', 'utf8');
414-
data += decipher.final('utf8');
415-
return data;
412+
try {
413+
if (typeof Crypto.createDecipher === 'function') {
414+
let decipher = Crypto.createDecipher(algorithm, password);
415+
let data = decipher.update(text, 'hex', 'utf8');
416+
data += decipher.final('utf8');
417+
return data;
418+
}
419+
const hash = Crypto.createHash('md5');
420+
hash.update(password);
421+
let decipher = Crypto.createDecipheriv(algorithm, hash.digest(), Buffer.alloc(0));
422+
let data = decipher.update(text, 'hex', 'utf8');
423+
data += decipher.final('utf8');
424+
return data;
425+
} catch (e) {
426+
return "";
427+
}
416428
} else {
417429
const parts = text.split(":");
418430
const iv = Buffer.from(parts[0], 'hex');

‎src/tools/crypto-json.js‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,22 @@ function encrypt(text, algorithm, password) {
3434
*/
3535
function decrypt(text, algorithm, password) {
3636
if (text.indexOf(":") === -1) {
37-
let decipher = Crypto.createDecipher(algorithm, password);
38-
let data = decipher.update(text, 'hex', 'utf8');
39-
data += decipher.final('utf8');
40-
return data;
37+
try {
38+
if (typeof Crypto.createDecipher === 'function') {
39+
let decipher = Crypto.createDecipher(algorithm, password);
40+
let data = decipher.update(text, 'hex', 'utf8');
41+
data += decipher.final('utf8');
42+
return data;
43+
}
44+
const hash = Crypto.createHash('md5');
45+
hash.update(password);
46+
let decipher = Crypto.createDecipheriv(algorithm, hash.digest(), Buffer.alloc(0));
47+
let data = decipher.update(text, 'hex', 'utf8');
48+
data += decipher.final('utf8');
49+
return data;
50+
} catch (e) {
51+
return "";
52+
}
4153
} else {
4254
const parts = text.split(":");
4355
const iv = Buffer.from(parts[0], 'hex');

0 commit comments

Comments
 (0)