-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrutetime.js
76 lines (63 loc) · 2.26 KB
/
brutetime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const crypto = require('crypto');
const argv = require('minimist')(process.argv.slice(2));
const KEY_SIZE_BITS = 28;
const KEY_MARKER = 'KEY:';
const main = () => {
if (argv._[0] === 'encrypt') {
if (!argv._[1]) throw Error('Expecting a secret to encrypt');
const secret = argv._[1];
const encryptedText = encrypt(secret, argv.size || KEY_SIZE_BITS);
console.log('# ENCRYPTING');
console.log('* Encrypted Text: ' + encryptedText);
console.log(`* Key size: ${argv.size || KEY_SIZE_BITS}bits`);
console.log('');
console.log('');
} else if (argv._[0] === 'decrypt') {
if (!argv._[1]) throw Error('Expecting a secret to decrypt');
const encryptedText = argv._[1];
const KEY_MAX = Math.pow(2, argv.size || KEY_SIZE_BITS);
console.log('# DECRYPTING');
const start = Date.now();
for (let i = 0; i < KEY_MAX; i++) {
if (i > 0 && i % 10000 === 0) {
// Update progress now and then
console.log(`${i} / ${KEY_MAX} (${Math.floor((i / KEY_MAX) * 10000) / 100}%)`);
if (i % 100000 === 0) {
const estimatedSec = (KEY_MAX / i) * ((Date.now() - start) / 1000);
if (estimatedSec && !isNaN(estimatedSec)) {
console.log(`Estimated to take a maximum of ${Math.floor(estimatedSec)} sec (${Math.floor(estimatedSec / (60 * 60))}hrs)`);
}
}
}
const decryptedText = decrypt('' + i, encryptedText);
if (decryptedText) {
console.log('DECRYPTED: ' + decryptedText);
break;
}
}
console.log(`It took ${(Date.now() - start) / 1000} sec`);
} else {
throw Error(`
Usage: brutetime [options] encrypt|decrypt
\t--size - Size in bits of the random key (roughly; 24bit <15m, 26bit <1h, 30bit <6h, 32bit <1w)
`)
}
};
const encrypt = (text, keySize = KEY_MAX) => {
let key = '' + Math.floor(Math.random() * Math.pow(2, keySize));
let cipher = crypto.createCipher('aes-256-cbc', key);
let crypted = cipher.update(KEY_MARKER + text, 'utf-8', 'hex');
crypted += cipher.final('hex');
return crypted;
};
const decrypt = (key, data) => {
try {
let decipher = crypto.createDecipher('aes-256-cbc', key);
let decrypted = decipher.update(data, 'hex', 'utf-8');
decrypted += decipher.final('utf-8');
return decrypted.indexOf(KEY_MARKER) === 0 ? decrypted.split(KEY_MARKER)[1] : null;
} catch (e) {
return null;
}
};
main();