-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
145 lines (137 loc) · 4.34 KB
/
Copy pathclient.js
File metadata and controls
145 lines (137 loc) · 4.34 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// IMPORT MODEULES
//
const colors = require("colors");
const fs = require("fs");
const crypto = require("crypto");
const path = require("path");
const WebSocket = require("ws");
const prompt = require("prompt");
const { message } = require("prompt");
const { exit } = require("process");
const ws = new WebSocket("ws://localhost:6969");
//
// COLORS OBJECT
//
colors.setTheme({
info: "bgGreen",
help: "bgCyan",
warn: "yellow",
success: "bgBlue",
error: "blue",
bold: "bold",
});
//
// UTENTI AUTORIZZATI
//
const alessio = { username: "alex", password: "secret" };
const marco = { username: "mark", password: "secret" };
//
// CIFRATURA SIMMETRICA (INTEGRA UTENTE E MESSAGGIO IN UN UNICO BLOCCO)
//
const algorithm = "aes-256-cbc";
//
//const initVector = crypto.randomBytes(16);
const initVector = Buffer.from([
8, 6, 7, 5, 3, 0, 9, 6, 8, 6, 7, 5, 3, 0, 9, 6,
]);
//console.log(initVector);
//
//const Securitykey = crypto.randomBytes(32);
const Securitykey = Buffer.from([
8, 6, 7, 5, 3, 0, 9, 6, 8, 6, 7, 5, 3, 0, 9, 6, 8, 6, 7, 5, 3, 0, 9, 6, 8, 6,
7, 5, 3, 0, 9, 6,
]);
//console.log(Securitykey);
//
// FUNZIONE PER CIFRARE A CHAVE SIMMETRICA
//
const cifra02 = function (message) {
//
const cipher = crypto.createCipheriv(algorithm, Securitykey, initVector);
let encryptedData = cipher.update(message, "utf-8", "hex");
encryptedData += cipher.final("hex"); // coding finale
//console.log("Encrypted message: " + encryptedData);
return encryptedData;
};
//
// FUNZIONE PER CIFRARE A CHAVE SIMMETRICA
// GARANTISCE SICUREZZA DEL MESSAGGIO
//
function pubEncrypt(toEncrypt, keyPath) {
const publicKey = fs.readFileSync(path.join(__dirname, "public.pem"), "utf8");
const buffer = Buffer.from(toEncrypt, "utf8");
const encrypted = crypto.publicEncrypt(publicKey, buffer);
return encrypted.toString("base64");
}
//
// FUNZIONE PER CIFRARE A CHAVE SIMMETRICA
// GARANTISCE SICUREZZA DEL MESSAGGIO
//
function priEncrypt(toEncrypt, keyPath) {
const privateKey = fs.readFileSync(
path.join(__dirname, "private.pem"),
"utf8"
);
const buffer = Buffer.from(toEncrypt, "utf8");
const encrypted = crypto.privateEncrypt(privateKey, buffer);
return encrypted.toString("base64");
}
// CALLBACK OPTION (WELCOME TO CALLBACK HELL!)
//
const takeCreds = function () {
prompt.get(["username", "password"], function (err, result) {
//
const u = result.username;
const p = result.password;
if ((u === alessio.username) & (p === alessio.password)) {
console.log("");
console.log("________ WELCOME TO SHELL CHAT ________".help.bold);
console.log(" ".success);
console.log(" ___ *** * * **** ***** __ ".success);
console.log(" _____ * **** **** * ____ ".success);
console.log("_______ *** * * * * * ______".success);
console.log(" ".success);
console.log(" ".help);
console.log("");
userID = alessio.username;
//fs.writeFile("user.txt", userID, function (err) {
//if (err) throw err;
//console.log("Saved!");
//});
input();
} else if ((u === marco.username) & (p === marco.password)) {
console.log("");
console.log("________ WELCOME TO SHELL CHAT ________".help.bold);
console.log(" ".success);
console.log(" ___ *** * * **** ***** __ ".success);
console.log(" _____ * **** **** * ____ ".success);
console.log("_______ *** * * * * * ______".success);
console.log(" ".success);
console.log(" ".help);
console.log("");
userID = marco.username;
//fs.writeFile("user.txt", userID, function (err) {
//if (err) throw err;
//});
input();
} else {
console.log("Wrong Credentials!");
takeCreds();
}
});
};
//
const input = function input_message() {
//console.log(userID);
prompt.get("testo", function (err, result) {
let cleartext = result.testo;
const enc02 = priEncrypt(cleartext, `<private.pem>`);
//fs.readFile("user.txt", "utf8", function (err, data) {
const enc01 = cifra02(enc02 + userID);
ws.send(enc01);
input();
//});
});
};
//
takeCreds();