美文网首页
ras 加密

ras 加密

作者: 半瓶阳光o_o | 来源:发表于2018-09-15 09:42 被阅读0次

    <?php

    class SoftdogController extends Controller {

    public function key(Request $request,$name){

            $password = crc32($name);  //aes 数据吗

            $data='66777777766';  //数据码

            $iv = '0000000000000000';

            $method = 'aes-128-cbc';  //加密方式

            $encdata=openssl_encrypt ($data, $method, $password,0, $iv);

            $key['key'] = $encdata;

            //var_dump($encdata);die;

            return return_json(1,'success',1,$key);

    }

    public function rsakey(Request $request){

          //私key  密文 格式不要更改!!

    //       $private_key = '-----BEGIN RSA PRIVATE KEY-----

    // MIICXQIBAAKBgQC4NPleUC4dsnZ6/k7x+SoZlfnxSeVcVvmfQUWtFCFO+ERK/luY

    // g6KxrFvQfK4vgf8zBmGMjAsP4o7ZQW3S6nUkyBwmgowL+k3s2vO9JHpeknzo7jjf

    // pIJjLhqpiyfBiCBPu2/6hKW7cpemUjChx/NhHpMsnsizpgWF7GDeRGLQzwIDAQAB

    // AoGBAJvXTWVqJim/sxcJEZcLen+c75EgOGgvuQ3vtdfg4PEjfvuHR8y3CSyRqjev

    // U9oje9kaO+shC7bLsJjDDvvSmXkuVYQWUjiBsZTqrFh7S5CU5RasvPaPtgRAyVgq

    // A898R5qmBhBNOd3oynPyWgzjTEyzpx7RGR/XpWt6pQVqKshBAkEA2jYwshjfUdU7

    // 6f3Wydj/EUb8jUaKc/NqC3vvNzSu7mCqnlBFkFDo7AEz/6PrVgYz6kcuHmg5CPvK

    // YBESgp8VYQJBANgbRw3vgcMzL5IyN9JEH/mG37bsyUDh17eJEs4xvUwwTNIso1o0

    // RnJJ8hJcjLfHb4j3uULYCDbkCF5awcFUZC8CQQCM57c0GcmC3y2MIESlHIFAV9VX

    // KLjYvNK6TbIftN5MwZELH4x77zMoynIYXnHln6XAX2xRaD2EJor806lfPXihAkA6

    // c3ZOgJ96apfNVSPgDWkQlrhfw7/TesJb9AlQGAf5WX8NweEU/tqnAvUknj+5XYn/

    // Y/GJ8OCX6CmTS7zoS6klAkA+rsg+xthBuAs+Lam+QH8mv6X8ceE2UxcjJG+GWDWC

    // RkBg7F1ajvAu0JD5EHQ+caGl96AOtbhJhbMY8owDAuJD

    // -----END RSA PRIVATE KEY-----';

        //公key

                    $public_key= '-----BEGIN PUBLIC KEY-----

    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4NPleUC4dsnZ6/k7x+SoZlfnx

    SeVcVvmfQUWtFCFO+ERK/luYg6KxrFvQfK4vgf8zBmGMjAsP4o7ZQW3S6nUkyBwm

    gowL+k3s2vO9JHpeknzo7jjfpIJjLhqpiyfBiCBPu2/6hKW7cpemUjChx/NhHpMs

    nsizpgWF7GDeRGLQzwIDAQAB

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

                  $file_path = storage_path('app\rsa_private_key.pem');

                  $private_key = '';

    if(file_exists($file_path)){

    $file_arr = file($file_path);

        }

        foreach($file_arr as $value){

      $private_key .= $value;

    }

                  // echo $foo_str;die;

                    $pi_key =  openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id

                    $pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的

        $data = "aassssasssddd";//原始数据 

    $encrypted = "";    //加密

    $decrypted = "";    //解密

    openssl_private_encrypt($data,$encrypted,$pi_key);//私钥加密 

    $encrypted = base64_encode($encrypted);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的 

        // $encrypted  加密后密文

    //var_dump($encrypted);die;

    openssl_public_decrypt(base64_decode($encrypted),$decrypted,$pu_key);//私钥加密的内容通过公钥可用解密出来 

    //  $decrypted  解密后原代码 aassssasssddd

        var_dump($decrypted);die;

    openssl_public_encrypt($data,$encrypted,$pu_key);//公钥加密 

    $encrypted = base64_encode($encrypted); 

    //var_dump($encrypted);die;

    // $encrypted  公钥加密  加密后密文

    openssl_private_decrypt(base64_decode($encrypted),$decrypted,$pi_key);//私钥解密 

    //  $decrypted  私钥解密  后源代码 aassssasssddd

    }

    }

    相关文章

      网友评论

          本文标题:ras 加密

          本文链接:https://www.haomeiwen.com/subject/isrgdftx.html