/
crypt.php
86 lines (76 loc) · 1.34 KB
/
crypt.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
<?php
//$str="password";
$str=
'
<?xml version="1.0" encoding="UTF-8" ?>
<resume>
<name>
Jeff
</name>
<contact>
<tel>
234
</tel>
<address>
<street_number>
255
</street_number>
<street_name>
humber bolverd
</street_name>
</address>
</contact>
'; //208 characters, which is the up range my key can work with. Beyond that, it doesnot work.
//I use 2048 bit key, so it can enctry no more than 2048/8=256 byte data.
/*
<education>
humber
</education>
</resume>
';
*/
$encrypted=public_encrypt($str);
echo $encrypted;
echo "<br>";
echo private_decrypt($encrypted);
function public_encrypt($plaintext){
$fp=fopen("publickey","r");
$pub_key=fread($fp,8192);
fclose($fp);
openssl_get_publickey($pub_key);
openssl_public_encrypt($plaintext,$crypttext, $pub_key );
return(base64_encode($crypttext));
}
function private_decrypt($encryptedext){
$fp=fopen("privatekey","r");
$priv_key=fread($fp,8192);
fclose($fp);
$private_key = openssl_get_privatekey($priv_key);
openssl_private_decrypt(base64_decode($encryptedext), $decrypted, $private_key);
return $decrypted;
}
/*
$str = '<?xml version="1.0" encoding="UTF-8" ?>
<resume>
<name>
Jeff
</name>
<contact>
<tel>
234
</tel>
<address>
<street_number>
255
</street_number>
<street_name>
humber bolverd
</street_name>
</address>
</contact>
<education>
humber
</education>
</resume>';
*/
?>