You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Sm2DecryptCommand extends Command
{
/**
* The name and signature of the console command.
*
* 用法php artisan sm2:decrypt "密文hex" "私钥hex" [--mode=c1c3c2] [--asn1]
*/
protected $signature = 'sm2:decrypt';
/**
* The console command description.
*/
protected $description = '使用 SM2 私钥解密(硬编码密文与私钥),输出明文';
public function handle(): int
{
// 硬编码密文与私钥(与加密命令匹配)
$cipher = 'e27c3780e7069bda7082a23a489d77587ce309583ed99253f66e1d9833ed1a1d0b5ce86dc6714e9974cf258589139d7b1855e8c9fa2f2c1175ee123a95a23e9bbbaf04ad750b5c304a875aab17851fac60453940aac9da33cc4249a52ee46be446cf5083b5';
$privateKey = 'MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgrEdb4ONfRO2N0cXZsDY0PcGpfzQKfrg6DB0r+1dt4EmgCgYIKoEcz1UBgi2hRANCAARxd7Iuetxpc/2pgiGY1aLz6RvYyz696c9B8s4FghM0K5T4G+ywxIQ4pNFRe2sbp3wJydCIX647Xw1OCRl/vMHt';
$mode = 1; // C1C3C2
try {
$sm2 = new \Rtgm\sm\RtSm2('hex', true);
$plain = $sm2->doDecrypt($cipher, $privateKey, true, $mode);
if ($plain === '') {
$this->error('解密失败C3 校验不通过或格式不匹配');
return self::FAILURE;
}
$this->line($plain);
return self::SUCCESS;
} catch (\Throwable $e) {
$this->error('SM2 解密异常:' . $e->getMessage());
return self::FAILURE;
}
}
}