sevming / crypt
v1.0.2
2018-12-07 08:13 UTC
Requires
- php: >=7.0.0
- sevming/support: ^1.0
This package is auto-updated.
Last update: 2024-12-15 16:27:09 UTC
README
AES
PHP、iOS、Android 三端加解密
<?php // 默认 AES-128-CBC $aes = Crypt::aes([ 'key' => Str::random(16), ]); $encrypt = $aes->encrypt('test'); $decrypt = $aes->decrypt($encrypt); // AES-256-CBC $aes = Crypt::aes([ 'key' => Str::random(32), 'cipher' => 'AES-256-CBC', ]); $encrypt = $aes->encrypt('test'); $decrypt = $aes->decrypt($encrypt);
加解密(支持MAC校验, 防止时序攻击)
<?php // 默认 AES-128-CBC $aes = Crypt::aes([ 'key' => Str::random(16), ]); $encrypt = $aes->encryptWithMac('test'); $decrypt = $aes->decryptWithMac($encrypt); // AES-256-CBC $aes = Crypt::aes([ 'key' => Str::random(32), 'cipher' => 'AES-256-CBC', ]); $encrypt = $aes->encryptWithMac('test'); $decrypt = $aes->decryptWithMac($encrypt);
RSA
加解密
<?php // 加解密 $rsa = Crypt::rsa([ 'publicKey' => __DIR__ . '/public.pem', 'privateKey' => __DIR__ . '/private.pem', ]); $encrypt = $rsa->encrypt('test'); $decrypt = $rsa->decrypt($encrypt);
生成签名及校验(拼接KEY)
$rsa = Crypt::rsa([ 'key' => Str::random(16), ]); $data = [ 'type' => 1, 'value' => 'test' ]; // 生成签名 $data['sign'] = $rsa->generateSign($data); // 校验签名 $verifyResult = $rsa->verifySign($data);
生成签名及校验(RSA签名)
$rsa = Crypt::rsa([ 'publicKey' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1hgDgNvvAymqO7LLT8HY0yMdYP3y8Ms8SSSVFAzP2U5QNN+9r/L5J/Omxs5yXbY10NmjCkn6JfkOQTeNs+G+roNymsxf/S12yt0PmBblbjUGay1bWBH/Z44bZSnVRcPQYJ9tzKjI8LH6Hc3hKOE9Uk3JZ3xw0o1zbF9OhsuOCimV543FxovLsTZymbAvYOaeacpCT/lI6DRYa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vo+JWvidS66tlxZ1aHcLKRqTo9MPGGGFFapQaEuKC8t18/4/HyJtuHDr/NXQV7JB7ERREXTn8sPpJ95rza1K+HB64X6DC/MmalA7KUAyks6u6XIbwpHV2SnbOGCgbdzBX1R8LTW2XdTNNCe1u/lIUt7990XYAVdh4ZPylf3QKBgBQSdOqk34QNlKBiZ3x4GO7WWG7EhZMiZVs73s0kSEufT10aVVAGo053niNPPcSPdgDWp8twYJcN+tkVyELR2o70mRlupBfiEI91o+F+tA1xEy8LUsAKT/ds0FAPDV2BvpoPS9lOYeYhw3uXCTIJDVzTiG6es25OwuV0TLjZKRelAoGAGD6T50UE9un5c9pt6XRFospKqBB7aeZN0pFotVWPpGgiuQzkwZGV/XyLmqcruFShAc+PpNZn3BuV0Pc3ZfdpseEDxKJGKFI340mNHCOS+gaN2QsM6ftkLnrNvSm9dvJJHepOiFsE1bWOjG6vIx/4NZZma4mGhuA+K3tPyVwjpL0CgYEAhNB3iXIc04HqWwKuSac4aexiSJ1ozFU48FWiW0F/8Y+a0qkvLChhD1kNJ5wSr84KGkJGQPJbiclcJiAw9hD2LU6biJIUH/1luTzfFyMFDRLWc9WpeBKdtIfAsxX996VQl4x6cTGkgejWXRA4LBw0T2/coID3GqO4JHb5DAEaOQ4=', ]); $data = [ 'type' => 1, 'value' => 'test' ]; // 生成签名 $sign = $rsa->generateSignWithOpenssl($data); // 校验签名 $verifyResult = $rsa->verifySignWithOpenssl($data, $sign);