-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKISA_SEED_CBC_HANDLE.php
116 lines (93 loc) · 2.91 KB
/
KISA_SEED_CBC_HANDLE.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
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
<?php
require_once ('KISA_SEED_CBC.php');
class KISA_SEED_CBC_HANDLE
{
private static $g_bszUser_key = [ "f1", "96", "ff", "18", "28", "c8", "ef", "cc", "ab", "1a", "fd", "8d", "8c", "b9", "a4", "34" ];
private static $g_bszIV = [ "78", "69", "81", "ab", "4a", "4c", "2c", "a8", "a9", "99", "67", "81", "77", "a1", "cc", "bb" ];
private static $SeedBlockSize = 16;
private static $PADDING_VALUE = 0x0F;
public function addPadding($source, $blockSize) {
$paddingResult = $source;
$paddingCount = $blockSize - (count($source) % $blockSize);
if (!$paddingCount) {
for($i=0;$i<self::$SeedBlockSize;$i++) {
$paddingResult[] = self::$PADDING_VALUE;
}
} else {
for($i=0;$i<$paddingCount;$i++) {
$paddingResult[] = 0x0A;
}
}
return $paddingResult;
}
public function removePadding($source, $blockSize) {
$paddingResult = $source;
$byte_length = count($source);
$lb_start = $byte_length - $blockSize - 1;
$is_padding = false;
$padding_start = 0;
for ($i = $lb_start; $i < $byte_length; $i++) {
if ($source[$i] == 10) {
$is_padding = true;
$padding_start = $i;
break;
}
}
for ($i = $padding_start; $i < $byte_length; $i++) {
array_pop($paddingResult);
}
return $paddingResult;
}
public function encrypt($str) {
$planBytes = array_slice(unpack('c*', $str), 0);
$planBytes = $this->addPadding($planBytes, self::$SeedBlockSize);
$keyBytes = self::$g_bszUser_key;
$IVBytes = self::$g_bszIV;
for($i = 0; $i < 16; $i++)
{
$keyBytes[$i] = hexdec($keyBytes[$i]);
$IVBytes[$i] = hexdec($IVBytes[$i]);
}
if (count($planBytes) == 0) {
return $str;
}
$ret = null;
$bszChiperText = null;
$pdwRoundKey = array_pad(array(),32,0);
$bszChiperText = KISA_SEED_CBC::SEED_CBC_Encrypt($keyBytes, $IVBytes, $planBytes, 0, count($planBytes));
$r = count($bszChiperText);
for($i=0;$i< $r;$i++) {
$ret.= ($ret ? "," : "") . sprintf("%02X", $bszChiperText[$i]);
}
return $ret;
}
public function decrypt($str) {
$planBytes = [];
$keyBytes = self::$g_bszUser_key;
$IVBytes = self::$g_bszIV;
for($i = 0; $i < 16; $i++)
{
$keyBytes[$i] = hexdec($keyBytes[$i]);
$IVBytes[$i] = hexdec($IVBytes[$i]);
}
$hex_arr = explode(",", $str);
foreach ($hex_arr as $hex) {
$dec = hexdec($hex);
$planBytes[] = $dec;
}
if (count($planBytes) == 0) {
return $str;
}
$pdwRoundKey = array_pad(array(),32,0);
$bszPlainText = null;
$planBytresMessage = "";
$bszPlainText = KISA_SEED_CBC::SEED_CBC_Decrypt($keyBytes, $IVBytes, $planBytes, 0, count($planBytes));
for($i=0;$i< sizeof($bszPlainText);$i++) {
$planBytresMessage .= sprintf("%02X", hexdec($bszPlainText[$i])).",";
}
$decryptHexStr = substr($planBytresMessage,0,strlen($planBytresMessage)-1);
$decryptHex = explode(",", $decryptHexStr);
$dec_data = call_user_func_array("pack", array_merge(array("c*"), $decryptHex));
return $dec_data ? trim($dec_data) : "";
}
}