forked from kjur/jsrsasign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatadecrypt
More file actions
executable file
·79 lines (65 loc) · 2.01 KB
/
datadecrypt
File metadata and controls
executable file
·79 lines (65 loc) · 2.01 KB
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
#!/usr/bin/env node
/*
* datadecrypt - data decryptor
*
* Copyright (c) 2016 Kenji Urushima (kenji.urushima@gmail.com)
*
* This software is licensed under the terms of the MIT License.
* https://kjur.github.io/jsrsasign/license
*
* The above copyright and license notice shall be
* included in all copies or substantial portions of the Software.
*
* Please use '-h' option for this script usage.
* ---------------------------------------------------------
* DESCRIPTION
* This script decrypts a encrypted binary data file with
* RSA or RSA-OAEP algorithm.
*
* USAGE
* % dataencrypt enc.bin plain.txt rsaprv.pem RSA # with RSA alg
* % dataencrypt enc.bin plain.txt rsaprv.pem RSAOAEP # with RSAOAEP alg
* % dataencrypt enc.bin - rsaprv.pem RSA # output to stdout by '-'
*/
var program = require('commander');
var rs = require('jsrsasign');
var rsu = require('jsrsasign-util');
var path = require('path');
program
.version('1.0.0 (2016-Nov-05)')
.usage('[options] <encrypted data file> <output plain file or "-"> <PEM RSA private key file> [RSA|RSAOEAP*>]')
.description('encrypt data')
.parse(process.argv);
if (program.args.length < 3)
throw "wrong number of arguments";
var keyObj, inHex, encHex;
var algName = "RSA";
var keyStr = "";
var inFileOrHex = program.args[0];
var outFile = program.args[1];
var keyFileOrStr = program.args[2];
if (program.args.length > 3) algName = program.args[3];
try {
keyStr = rsu.readFile(keyFileOrStr);
} catch(ex) {
keyStr = keyFileOrStr;
}
try {
inHex = rs.rstrtohex(rsu.readFile(inFileOrHex));
} catch(ex) {
inHex = inFileOrHex;
}
try {
keyObj = rs.KEYUTIL.getKey(keyStr);
} catch(ex) {};
if (keyObj instanceof rs.RSAKey && keyObj.isPrivate) {
var plainStr = rs.KJUR.crypto.Cipher.decrypt(inHex, keyObj, algName);
if (outFile === "-") {
process.stdout.write(plainStr);
} else {
rsu.saveFile(outFile, plainStr);
console.log("data decrypted and write to file successfully");
}
} else {
console.log("data decryption failed");
}