-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendMessage.php
88 lines (69 loc) · 1.73 KB
/
sendMessage.php
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
<?php
session_start();
$path = 'phpseclib';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
include_once('Crypt/RSA.php');
header('Content-type: application/json');
class Message {
public $from;
public $to;
public $body;
}
function rsa_encrypt($string, $public_key)
{
//Create an instance of the RSA cypher and load the key into it
$cipher = new Crypt_RSA();
$cipher->loadKey($public_key);
//Set the encryption mode
$cipher->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
//Return the encrypted version
return $cipher->encrypt($string);
}
function check(){
$i;
$array = array();
$str = file_get_contents('users.txt');
$array = json_decode($str);
for($i=0;$i<sizeof($array);$i++){
if($array[$i]->user === $_POST["recipient"]){
return true;
}
}
return false;
}
function findRecipKey(){
$i;
$array = array();
$str = file_get_contents('users.txt');
$array = json_decode($str);
for($i=0;$i<sizeof($array);$i++){
if($array[$i]->user === $_POST["recipient"]){
return $array[$i]->pubKey;
}
}
return "WRONG";
}
$tempText = $_POST["messageVal"];
$recipKey = findRecipKey();
$newText = rsa_encrypt($tempText, $recipKey);
$a = base64_encode($newText);
$array = array();
$mess = new Message();
$mess->from = $_SESSION["login"];
$mess->to = $_POST["recipient"];
$mess->body = $a;
$filename = 'messages.txt';
if (!file_exists($filename) || ($bar = file_get_contents($filename))=== '') {
array_push($array,$mess);
}
else{
$array = json_decode($bar);
array_push($array,$mess);
}
$str = json_encode($array);
if(check()){
file_put_contents($filename, $str);
}
//header('Location: viewPosts.php');
exit;
?>