This commit is contained in:
net909
2024-07-06 22:45:17 +08:00
parent 31e0686273
commit 16a504c9f0
3 changed files with 25 additions and 11 deletions
+8 -4
View File
@@ -30,6 +30,7 @@ class App extends Plugin
private $coded;
private $opensslPadding;
private $opensslAlgo = 1;
private $dataType;
public function __construct()
@@ -47,6 +48,7 @@ class App extends Plugin
$this->sign = request()->param("sign");
$this->opensslPadding = request()->param("openssl_padding");
$this->opensslAlgo = intval(request()->param("openssl_algo"));
$this->dataType = request()->param("data_type");
}
public function index()
@@ -86,7 +88,7 @@ class App extends Plugin
}
try {
if (openssl_private_encrypt($this->origin, $encrypted, $this->getRsaPrivateKey(), $this->opensslPadding)) {
return msg('ok', 'success', base64_encode($encrypted));
return msg('ok', 'success', $this->dataType == 'hex' ? bin2hex($encrypted) : base64_encode($encrypted));
}
return msg('error', '加密失败');
} catch (\Exception $e) {
@@ -124,7 +126,7 @@ class App extends Plugin
}
if ($string) {
return msg('ok', 'success', base64_encode($string));
return msg('ok', 'success', $this->dataType == 'hex' ? bin2hex($string) : base64_encode($string));
}
return msg('error', '加密失败');
} catch (\Exception $e) {
@@ -143,7 +145,8 @@ class App extends Plugin
return msg('error', '解密失败');
}
try {
if (openssl_private_decrypt(base64_decode($this->coded), $decrypted, $this->getRsaPrivateKey(), $this->opensslPadding)) {
$encrypted = $this->dataType == 'hex' ? hex2bin($this->coded) : base64_decode($this->coded);
if (openssl_private_decrypt($encrypted, $decrypted, $this->getRsaPrivateKey(), $this->opensslPadding)) {
return msg('ok', 'success', $decrypted);
}
return msg('error', '解密失败');
@@ -165,7 +168,8 @@ class App extends Plugin
}
try {
if (openssl_public_decrypt(base64_decode($this->coded), $decrypted, $this->rsaPublicKey, $this->opensslPadding)) {
$encrypted = $this->dataType == 'hex' ? hex2bin($this->coded) : base64_decode($this->coded);
if (openssl_public_decrypt($encrypted, $decrypted, $this->rsaPublicKey, $this->opensslPadding)) {
return msg('ok', 'success', $decrypted);
}
return msg('error', '解密失败');
+16
View File
@@ -74,6 +74,13 @@
<option v-for="v,index in openssl_padding" :value="index+1">{{v}}</option>
</select>
</div>
<div class="form-group">
<label class="form-label">密文数据类型</label>
<select class="form-control" v-model="crypto_from.data_type">
<option value="base64">Base64</option>
<option value="hex">Hex(十六进制)</option>
</select>
</div>
<div class="form-group">
<label class="form-label">加密/解密方式</label>
<select class="form-control" v-model="crypto_from.type">
@@ -113,6 +120,13 @@
<option v-for="v,index in openssl_algo" :value="index+1">{{v}}</option>
</select>
</div>
<div class="form-group">
<label class="form-label">签名数据类型</label>
<select class="form-control" v-model="sign_form.data_type">
<option value="base64">Base64</option>
<option value="hex">Hex(十六进制)</option>
</select>
</div>
<div class="row pt-1 pb-1 mb-3">
<div class="col-6">
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="sign">
@@ -336,12 +350,14 @@ function base64ToPem(base64Key, type) {
},
crypto_from: {
openssl_padding: 1,
data_type: 'base64',
type: 0,
origin: '',
coded: ''
},
sign_form: {
openssl_algo: 1,
data_type: 'base64',
data: "",
sign: "",
},