process.php
4.32 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
146
147
148
149
150
151
152
153
<?php
header("Content-Type: text/html; charset=UTF-8");
$KEY_128 = '0000000000000000';
$IV_128 = '0000000000000000';
echo "[pubKey of Device]<br/>";
$pubKey1 = $_GET['pubKey1'];
echo "pubKey1 = $pubKey1<br/>";
$pubKey2 = $_GET['pubKey2'];
echo "pubKey2 = $pubKey2<br/><br/>";
echo "[Encrypted digitally signed data]<br/>";
$pulse = $_GET['pulse'];
echo "pulse = $pulse<br/>";
//$pubKey1 = $_GET['pubKey1'];
//echo "pubKey1 = $pubKey1<br/>";
//$pubKey2 = $_GET['pubKey2'];
//echo "pubKey2 = $pubKey2<br/>";
$signatureR = $_GET['signatureR'];
echo "signatureR = $signatureR<br/>";
$signatureS = $_GET['signatureS'];
echo "signatureS = $signatureS<br/>";
echo "<br/>[Decrypted digitally signed data]<br/>";
$pulse = hex2bin($pulse);
$pulse = openssl_decrypt($pulse, 'AES-128-CBC', $KEY_128, OPENSSL_NO_PADDING, $IV_128);
//echo "pulse = $pulse<br/>";
$pulse = bin2hex($pulse);
echo "pulse = $pulse<br/>";
$pubKey1 = openssl_decrypt($pubKey1, 'AES-128-CBC', $KEY_128, OPENSSL_NO_PADDING, $IV_128);
$pubKey1 = bin2hex($pubKey1);
$pubKey1 = substr($pubKey1, 1, strlen($pubKey1) / 2);
//echo "pubKey1 = $pubKey1<br/>";
$pubKey2 = openssl_decrypt($pubKey2, 'AES-128-CBC', $KEY_128, OPENSSL_NO_PADDING, $IV_128);
$pubKey2 = bin2hex($pubKey2);
$pubKey2 = substr($pubKey2, 1, strlen($pubKey2) / 2);
//echo "pubKey2 = $pubKey2<br/>";
$signatureR = openssl_decrypt($signatureR, 'AES-128-CBC', $KEY_128, OPENSSL_NO_PADDING, $IV_128);
$signatureR = bin2hex($signatureR);
$signatureR = substr($signatureR, 1, strlen($signatureR) / 2);
echo "signatureR = $signatureR<br/>";
$signatureS = openssl_decrypt($signatureS, 'AES-128-CBC', $KEY_128, OPENSSL_NO_PADDING, $IV_128);
$signatureS = bin2hex($signatureS);
$signatureS = substr($signatureS, 1, strlen($signatureS) / 2);
echo "signatureS = $signatureS<br/>";
echo "<br/>[Digital signature verification]<br/>";
$hashPulse = sha1($hashPulse);
echo "hashPulse = $hashPulse<br/>";
echo "Verify signature with hashed data<br/>";
$result = openssl_verify($hashPulse, $signatureR, $pubKey1, 'ecdsa-with-SHA1');
// if ($result == 1) {
// echo "Valid<br/>";
// } elseif ($result == 0) {
// echo "Invalid<br/>";
// } else {
// echo "error: ".openssl_error_string();
// }
$host = 'localhost';
$user = 'haseo';
$pw = 'haseo';
$dbName = 'M2351';
$mysqli = new mysqli($host, $user, $pw, $dbName);
if($mysqli){
echo "<br/>[MySQL successfully connected!]<br/>";
echo "Convert binary to decimal<br/>";
$pulse = hexdec($pulse[0].$pulse[1]);
echo "pulse = $pulse<br/>";
$query = "INSERT INTO pulseTable(pulse) VALUES ('$pulse')";
mysqli_query($mysqli,$query);
echo "<br/>[Success insert pulse data to DB Table!!]";
}
else{
echo "MySQL could not be connected";
}
mysqli_close($mysqli);
/*
$encryptedBPM = $_GET['pulse'];
echo 'encryptedBPM : '.$encryptedBPM.'<br/>';
$decryptedBPM = Hex2Char($encryptedBPM);
echo 'decryptedBPM : '.$decryptedBPM.'<br/><br/>';
//echo 'decryptedBPM : '.hex2bin($decryptedBPM).'<br/><br/>';
//$decryptedBPM = Hex2String($decryptedBPM);
// echo 'decryptedBPM : '.$decryptedBPM.'<br/><br/>'
$decryptedBPM = openssl_encrypt($decryptedBPM, 'AES-128-CBC', $KEY_128, OPENSSL_NO_PADDING, $IV_128);
echo 'decryptedBPM : '.$decryptedBPM.'<br/><br/>';
$teststr = '414243';
echo 'teststr : '.$teststr.'<br/>';
$teststr = Hex2Char($teststr);
echo 'teststr : '.$teststr.'<br/>';
$teststr = openssl_encrypt($teststr, 'AES-128-CBC', $KEY_128, OPENSSL_NO_PADDING, $IV_128);
echo 'teststr : '.$teststr.'<br/>';
$teststr = openssl_decrypt($teststr, 'AES-128-CBC', $KEY_128, OPENSSL_NO_PADDING, $IV_128);
echo 'teststr : '.$teststr.'<br/>';
$plain = '=000000000000000';
$plain = openssl_encrypt($plain, 'AES-128-CBC', $KEY_128, OPENSSL_NO_PADDING, $IV_128);
echo 'plain : '.bin2hex($plain).'<br/>';
echo 'plain : '.$plain.'<br/>';
$plain = openssl_decrypt($plain, 'AES-128-CBC', $KEY_128, OPENSSL_NO_PADDING, $IV_128);
echo 'plain : '.$plain.'<br/>';
function HexStr2Str($string){
$hex='';
for ($i=0; $i < strlen($string); $i+=2){
$hex .= hex2bin(hexdec(($string[$i].$string[$i+1])));
}
return $hex;
}
function HexStr2DecStr($string){
$hex='';
for ($i=0; $i < strlen($string); $i+=2){
$hex .= hexdec(($string[$i].$string[$i+1]));
}
return $hex;
}
function Hex2Char($hex){
$string='';
for ($i=0; $i < strlen($hex); $i+=2){
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
function HexStr2Hex($str)
{
}
*/
?>