-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeycrypt.php
More file actions
137 lines (99 loc) · 3.94 KB
/
Copy pathkeycrypt.php
File metadata and controls
137 lines (99 loc) · 3.94 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
<?php
class keycrypt
{
const mainlength = KEYLENGTH; // 1024 , 2048 , 4096
public static function short_public_encrypt(string $input, string $filename):?string
{
$pub = file_get_contents($filename);
if (!openssl_public_encrypt($input, $encrypted, $pub, OPENSSL_PKCS1_OAEP_PADDING)){
throw new \Exception(openssl_error_string());
}
return base64_encode($encrypted);
}
public static function short_private_decrypt(string $input, string $filename):?string
{
$priv = file_get_contents($filename);
$encrypted = base64_decode($input);
if (!openssl_private_decrypt($encrypted, $decrypted, $priv, OPENSSL_PKCS1_OAEP_PADDING)){
throw new \Exception(openssl_error_string());
}
return $decrypted;
}
public static function short_private_encrypt(string $input, string $filename):?string
{
$pub = file_get_contents($filename);
if (!openssl_private_encrypt($input, $encrypted, $pub)) {
throw new \Exception(openssl_error_string());
}
return base64_encode($encrypted);
}
public static function short_public_decrypt(string $input, string $filename):?string
{
$priv = file_get_contents($filename);
$encrypted = base64_decode($input);
if (!openssl_public_decrypt($encrypted, $decrypted, $priv)){
throw new \Exception(openssl_error_string());
}
return $decrypted;
}
public static function encrypt(string $input, string $filename,bool $isPub = true):?string
{
$length = self::mainlength / 8;
$length = $length - 42;
$pub = file_get_contents($filename);
$plain = str_split($input, $length); // 470 für 4096
$return = '';
foreach ($plain AS $part) {
if($isPub) {
if (!openssl_public_encrypt($part, $encrypted, $pub, OPENSSL_PKCS1_OAEP_PADDING)){
throw new \Exception(openssl_error_string());
}
} else {
if (!openssl_private_encrypt($part, $encrypted, $pub)){
throw new \Exception(openssl_error_string());
}
}
$return .= $encrypted;
}
return base64_encode($return);
}
public static function decrypt(string $input, string $filename, bool $isPub = false):?string
{
$length = self::mainlength / 8;
$priv = file_get_contents($filename);
$encrypted = base64_decode($input);
$plain = str_split($encrypted, $length); // 512
$return = '';
foreach ($plain AS $part) {
if($isPub){
if (!openssl_public_decrypt($part, $decrypted, $priv)){
throw new \Exception(openssl_error_string());
}
} else {
if (!openssl_private_decrypt($part, $decrypted, $priv, OPENSSL_PKCS1_OAEP_PADDING)){
throw new \Exception(openssl_error_string());
}
}
$return .= $decrypted;
}
return $return;
}
public static function createkeys($privfile,$pubfile){
$config = [
"private_key_bits" => self::mainlength, // 4096
"private_key_type" => OPENSSL_KEYTYPE_RSA,
];
$private_key = openssl_pkey_new($config);
if (!openssl_pkey_export($private_key , $privKey)){
throw new \Exception(openssl_error_string());
}
file_put_contents($privfile, $privKey);
if (!file_exists(dirname($pubfile))){
if (!mkdir($concurrentDirectory = dirname($pubfile), 0777, true) && !is_dir($concurrentDirectory)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
}
}
$public_key_pem = openssl_pkey_get_details($private_key)['key'];
file_put_contents($pubfile, $public_key_pem);
}
}