欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于RSA公私**换行处理的问题

程序员文章站 2024-03-14 14:59:46
...

在RSA加密处理时,经常遇到RSA**为一行,但是在Linux下,需要换行处理,否则,无法进行加密处理。换行,只能按照指定的规则换行,否则无法加密,下面介绍下标准处理方:

 

  public function TrasferRSA()
  {
        if (IS_POST) {
            $rsa_type    = I('post.rsa_type');
            $rsa_content = trim(I('post.rsa_content'));
            var_dump($rsa_content);
            if ($rsa_type == 1) {
                $start_key       = str_replace('-----BEGIN RSA PRIVATE KEY-----', '', $rsa_content);
                $start_key       = trim(str_replace('-----END RSA PRIVATE KEY-----', '', $start_key));
                //wordwrap 按照指定的长度,对字符串进行换行
                $private_content = wordwrap($start_key, 64, "\n", true);
                $key             = <<<EOF
        -----BEGIN RSA PRIVATE KEY-----
        {$private_content}
        -----END RSA PRIVATE KEY-----
EOF;
                var_dump($key);//输出私钥
            } elseif ($rsa_type == 2) {
                $start_key      = str_replace('-----BEGIN PUBLIC KEY-----', '', $rsa_content);
                $start_key      = trim(str_replace('-----END PUBLIC KEY-----', '', $start_key));
                $public_content = wordwrap($start_key, 64, "\n", true);
                $key            = <<<EOF
-----BEGIN PUBLIC KEY-----
{$public_content}
-----END PUBLIC KEY-----
EOF;
                var_dump($key);//输出公钥
            }
        } else {
            $this->display();
        }
    }