技术文章–日常技术分享–RSA算法

作者 : 欧ber 本文共6501个字,预计阅读时间需要17分钟 发布时间: 2022-07-4 共436人阅读

RSA算法—加密!

 

  function Rsacheck($data){
   //rsa私钥
    $PRIVATEkey ='-----BEGIN PRIVATE KEY-----

MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCqX/LIdQ8+IhuT

/dCU2/2XWOzh4DbfhdHCtfPlExxPjBf15sluzimt/ECIrWo8jgt44pokz1rXiYzi

fSPX9DbmlNJcG5q3TQ112aBLWFbJswIchSXvGfkHdtSXP63Wd4Gg5uCZWiyOVfno

5eS/2JAnQTV4onm/vCbn2fpkLnYKGAMLhPx+0camvxY/EwFMGl9MJPv4la0Iswom

p6yuVJIDT7lsbMEK6a5rpRKrDK1blowoKzXkxeXwx5Zx7DaGjoTzpROjqKsgDPap

lExv/dP0rhvRzIQhkOjYYR3YIL2349BD0Bs+rb9L5JSKKIWbEazAi8aN7xDFbCtd

788soSkDAgMBAAECggEAU5pXiVQf0a5s9/bhhlpZGw5Q1xnt80s4PiKI9syk5Mfq

7gh8qbnSrASPKkJmUialrIENgKibfA6MKoubrhmxYv5+JyKIjg/qXf0XckF7/Oex

TotrrOOC1s3RzhI6XHYyDuGcLsQX/xRjWIyO7jD3s7uE4Jhhw9sxAC5vdv9xfsuy

c0CF1IfNhnFht/dz2uIQP2+gH8h7OoTZqjID0SGYGKmpA6EZ2gTFcAEGiKbvqHza

2DuwYwgMFqAKSLTFfmsh3k3S0pcz5H4pLhhAcNE9la1YqUUX/8eRDeoqtsmT1+PU

MDFfoFrQUVljYIEF6nuCDep8S5p7+YZ9Mcvqys6V+QKBgQDaEsCFvzhMtDn0Vope

4XmH3mu/CIjxM2JOxXI8FHVZRPmIHFjS20g2tEkxOMRyYYUYwIpZCRE+h4ApN6w8

NA93T4lN2z0Y3NDe0MN8BUMwdCuqpRc3nUp2f8qCLMDHRjO9Y3VVijTjh5pikrFp

qCJcn2D1yBHP/SMYEYWEJOcSfwKBgQDIAYYuQJHTb81jXUOsrMOq3K9tIovGPtq1

ew0IU3VivaNbFkCTpJrC5YB+koCWfqUztmqntWN69nTdmWaTkTpDyNcnDFzG6FFr

+paiilGS0UruYzcfqRyTdzPSZ+RO22e2cIOH/WLj5qpV7s0WLHIXRc4M/jO9G8lu

jgY5wBBffQKBgCWnXz64ycvUwr0BxFqlrApdyVTu8FZVfQAE77iX/7suQbfSzTN7

UXjAoHKzTphBZm3jsP+2pRea5fI3/rFWe+UqApdC58XHOvl+KoLblcJkDLYmU1/y

9MyJPDpF5/OAl6Z36eafOoqe+3VRxvhrY6OCkYdeyFSVbN3XCKrMhGmZAoGAGqCB

UaUNBP4sdN8UxGdXwWaSAFCKFUGG8oRITuR1i0i998F1pXJTnEDbo34zmlWP/tt5

RYyTiupG0r26d0jtXdreMfemXmDNfjelWExb4dT1SdlxnEhyYnuLLabhMVJqc1J1

YGRuhvLNmJUrPpl6Mvj7HqW1E6cgYRSNZaDZTS0CgYEAgPcpDNjNFbpfLYY6rAoi

oLE0FEHdYlDCElNKZgUCttfsvBpcS4YZ/T9orUpfR9b5NrEXUcch1813omLxPLIt

wG3niMNre41X491CVBgaIVnISw7Md4ApU3zz2VWQufGzDCj3T+EOWifUKICfkLLy

W8DbMbkbhJ5Jk+78t8vjGx4=

-----END PRIVATE KEY-----';  //测试私钥

   $data='ceshi';

     $res = openssl_get_privatekey($PRIVATEkey);//必须通过OpenSSL的方法进行读取rsa的私钥否则如果直接使用私钥进行加密的话会出现乱码

     $res =openssl_private_encrypt($data,$decrypt,$res);//rsa使用私钥进行加密,第一个是需要加密的参数,第二个是空参数,第三个是私钥

     

      $sign = base64_encode($decrypt);//经过以上的加密后还是需要最后一步进行base64_encode,的加密,encode是加密,decode是解密

      var_dump($sign);die;

   }


RSA算法—解密—!

//rsa公钥解密---妥善保存好公钥

   function check2(){

       $publickey='

-----BEGIN PUBLIC KEY-----

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAql/yyHUPPiIbk/3QlNv9

l1js4eA234XRwrXz5RMcT4wX9ebJbs4prfxAiK1qPI4LeOKaJM9a14mM4n0j1/Q2

5pTSXBuat00NddmgS1hWybMCHIUl7xn5B3bUlz+t1neBoObgmVosjlX56OXkv9iQ

J0E1eKJ5v7wm59n6ZC52ChgDC4T8ftHGpr8WPxMBTBpfTCT7+JWtCLMKJqesrlSS

A0+5bGzBCumua6USqwytW5aMKCs15MXl8MeWcew2ho6E86UTo6irIAz2qZRMb/3T

9K4b0cyEIZDo2GEd2CC9t+PQQ9AbPq2/S+SUiiiFmxGswIvGje8QxWwrXe/PLKEp

AwIDAQAB

-----END PUBLIC KEY-----';

      $data1='H8wz7AswZEV+jkAaQhTMu4+vsPQ432XL6QVB/UKiXShXUKR0HFTuA6L9KmA7PEtli1eqi4Q41BZ/iCl+aUxc9NIBve2mh3CC2gTj5LODSRV1BOY0F1BRkmTGZS5VKfhuce7KzkaTlgtHtH1j/nCqruMoAgWnFn0jh/767kGH4kq8uD2TrBqwx6JPfh2blQy2op3MpV5RIsb9MJnj/C/honjZ5u6WXOzntZhMVPkRCKIyh3H2hUrsXYFATkjcLmuJpoimfrUqldVuqSAdQ5me8c4w5e/mOHAFg3SHVhp/NgNQzdk75154GYrdOQxpY5pCeaFxw53myG1moiwTb3pe1g==';//测试数据

      $data=base64_decode($data1);//拿到已经加密的密文,首先进行bade64_decode的解密这个是首要的,先解密。才能继续下一步

      $res=openssl_public_decrypt($data,$decrypt,$publickey);//已经经过base64——decode的加密的密文还需要进行一次rsa公钥解密,跟加密的时候参数位置一样,

      var_dump($decrypt);die;

   }

RSA算法—加签—!

//rsa进行私钥加签

   //以下使用rsa进行签名调用open_sgin的方法进行签名,记住这个是签名,不是加密,签名的密文跟加密的密文是不一样的,谨记!

   function check3(){

         $PRIVATEkey ='-----BEGIN PRIVATE KEY-----

MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCqX/LIdQ8+IhuT

/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VTu8FZVfQAE77iX/7suQbfSzTN7

UXjAoHKzTphBZm3jsP+2pRea5fI3/rFWe+UqApdC58XHOvl+KoLblcJkDLYmU1/y

9MyJPDpF5/OAl6Z36eafOoqe+3VRxvhrY6OCkYdeyFSVbN3XCKrMhGmZAoGAGqCB

UaUNBP4sdN8UxGdXwWaSAFCKFUGG8oRITuR1i0i998F1pXJTnEDbo34zmlWP/tt5

RYyTiupG0r26d0jtXdreMfemXmDNfjelWExb4dT1SdlxnEhyYnuLLabhMVJqc1J1

YGRuhvLNmJUrPpl6Mvj7HqW1E6cgYRSNZaDZTS0CgYEAgPcpDNjNFbpfLYY6rAoi

oLE0FEHdYlDCElNKZgUCttfsvBpcS4YZ/T9orUpfR9b5NrEXUcch1813omLxPLIt

wG3niMNre41X491CVBgaIVnISw7Md4ApU3zz2VWQufGzDCj3T+EOWifUKICfkLLy

W8DbMbkbhJ5Jk+78t8vjGx4=

-----END PRIVATE KEY-----';  

   $data='ceshi';//常规的业务流程就是参数拼接,最后加上唯一的MD5,最后赋值给$data。然后进行加签,这里只是测试而已

     $res = openssl_get_privatekey($PRIVATEkey);//必须通过OpenSSL的方法进行读取rsa的私钥否则如果直接使用私钥进行加签的话会出现乱码

      openssl_sign($data, $sign, $res,OPENSSL_ALGO_MD5);////rsa使用私钥进行加签,第一个是需要加密的参数,第二个是空参数,第三个是私钥,这里出现了第四个参数,OPENSSL_ALGO_MD5,这个是MD5实现的就是MD5withRSA签名的算法,看情况需求吧,有的需要有的不需要!

      $sign = base64_encode($sign);//经过以上的加签后还是需要最后一步进行base64_encode,加密,encode是加密,decode是解密

      var_dump($sign);die;

   }

RSA算法—验签—!

function check4(){

   $publickey='-----BEGIN PUBLIC KEY-----

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAql/yyHUPPiIbk/3QlNv9

l1js4eA234XRwrXz5RMcT4wX9ebJbs4prfxAiK1qPI4LeOKaJM9a14mM4n0j1/Q2

5pTSXBuat00NddmgS1hWybMCHIUl7xn5B3bUlz+t1neBoObgmVosjlX56OXkv9iQ

J0E1eKJ5v7wm59n6ZC52ChgDC4T8ftHGpr8WPxMBTBpfTCT7+JWtCLMKJqesrlSS

A0+5bGzBCumua6USqwytW5aMKCs15MXl8MeWcew2ho6E86UTo6irIAz2qZRMb/3T

9K4b0cyEIZDo2GEd2CC9t+PQQ9AbPq2/S+SUiiiFmxGswIvGje8QxWwrXe/PLKEp

AwIDAQAB

-----END PUBLIC KEY-----';

   $data='ceshi';

   $sign='BLHa1JaXonj3paLNBmFpm6rAKy46hBJxVp/JOm0U0Ipy6webw2ev5KYgWtU/vANeIwGcT+YJW9yPXYwd+LpdRscbegBRq0UDUH4Id14hojgkJYnOACl2SrbF470zn5pv6XBLYCUs3EXWMknV7exHOuyVfJSoyds3KC0ITEWoSsHSnjviW2sxMMY94tqLy9oh75QEk6uRksRB0GkTRHtYU3fW2So8hDNgL43FnCVKF6MCCkOvoGqbUpxWNCODYz0s3FyVjIA6zkdVRfnEpFEnR8zOTtjaAb+G7MHnfDLf6i8wgMqQGlosU0+9z0g89a6hjI/eujq9B0uu69wwX3Fbtg==';//测试数据

     $res = openssl_get_publickey($publickey);

   $result = (bool)openssl_verify($data, base64_decode($sign), $res,OPENSSL_ALGO_MD5);//$data是签名参数,$sign,是需要验签的签名,先对签名进行base64_decode的解密,然后在对签名密文进行MD5withRSA进行验签,验签是调用openssl内置方法验签,返回bool值

  }

 会员可下载实例文档进行参考!!!

 

Ober---源码,极致的服务
Ober-bbs » 技术文章–日常技术分享–RSA算法

发表评论

提供最优质的资源集合

立即查看 了解详情