forked from kjur/jsrsasign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataencrypt
More file actions
executable file
·76 lines (62 loc) · 1.92 KB
/
dataencrypt
File metadata and controls
executable file
·76 lines (62 loc) · 1.92 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
#!/usr/bin/env node
/*
* dataencrypt - data encryptor
*
* 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 encrypts a plain text or data file with
* RSA or RSA-OAEP algorithm.
*
* USAGE
* % dataencrypt data.txt enc.bin rsapub.pem RSA # with RSA alg
* % dataencrypt data.txt enc.bin rsapub.pem RSAOAEP # with RSA-OEAP alg
* % dataencrypt data.txt enc.bin rsapub.pem RSAOAEP256
* # with RSA/ECB/OAEPWithSHA-256AndMGF1Padding
*/
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] <input plain file> <output encrypted file> <PEM RSA public key file> [RSA|RSAOAEP*]')
.description('encrypt data')
.parse(process.argv);
if (program.args.length < 3)
throw "wrong number of arguments";
var keyObj, inStr, encHex;
var algName = "RSA";
var keyStr = "";
var inFileOrStr = 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 {
inStr = rsu.readFile(inFileOrStr);
} catch(ex) {
inStr = inFileOrStr;
}
try {
keyObj = rs.KEYUTIL.getKey(keyStr);
} catch(ex) {};
if (keyObj instanceof rs.RSAKey && keyObj.isPublic) {
encHex = rs.KJUR.crypto.Cipher.encrypt(inStr, keyObj, algName);
rsu.saveFileBinByHex(outFile, encHex);
console.log("data encrypted successfully");
} else {
console.log("data encryption failed");
}