• 2022-10-04
宇哥博客 后端开发 PHP RSA加密解密,加密内容过长问题

PHP RSA加密解密,加密内容过长问题

RSA公钥私钥加密解密:

1024位的证书,加密时最大支持117个字节,解密时为128

2048位的证书,加密时最大支持245个字节,解密时为256

4096位的证书,加密时最大支持501个字节,解密时为512

如使用2048位的证书,加密内容长度大于245的时候就会加密失败,这时候可以进行分段加密,解密的时候也进行分段解密。

PHP代码:

<?php
	header("Content-type: text/html; charset=utf-8");

	//RSA加密公钥
	$public_key='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jo/nV70FxhkF+ohcOrPwKI1o+NUIPZ1wjZ6Rh9sL9pXT4+pWRpChgktx7UnK0BLt2oa6ySb4Y/NC928+u9J+POlYlFVggXJD3bP9My6rFnoFx3Z7epeKr/Dl/jrrRHcxlAgM8xwpbFlu2HRCBia43L1Tq7aJegdbh8H6JGiUaA3pq/WV8PaUFqHIu8fds7N2Vo4XpPNVGm/MAu9WaTVl68dQAASxGVeusAXxFsCNHhroSchQc33VszW6yAx9XINOI3n9QEEahK/E4mafcRCxf+Bb+uUnt+Xytb9hP71ltpoGUcymmOmzdsI7Tw+441B9Aog5ezuDtT62fo4lwj3cwIDAQAB';
	$public_key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($public_key, 64, "\n", true) . "\n-----END PUBLIC KEY-----";

	//RSA加密私钥
	$private_key='MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDiOj+dXvQXGGQX6iFw6s/AojWj41Qg9nXCNnpGH2wv2ldPj6lZGkKGCS3HtScrQEu3ahrrJJvhj80L3bz670n486ViUVWCBckPds/0zLqsWegXHdnt6l4qv8OX+OutEdzGUCAzzHClsWW7YdEIGJrjcvVOrtol6B1uHwfokaJRoDemr9ZXw9pQWoci7x92zs3ZWjhek81Uab8wC71ZpNWXrx1AABLEZV66wBfEWwI0eGuhJyFBzfdWzNbrIDH1cg04jef1AQRqEr8TiZp9xELF/4Fv65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GeeP1+siFbXTBwgKalzTtyE7EaKQLHEl4Z9jEGgVQKBgGiFTAa7VCgOouu3kek2Yi4jBSwCgH7uyGgNq8mkeNS/aHS7f4ZugTxH0ecfyzjmkFvwaE+nUgInGQ5h6MbprvKG1nZBQF67dLSeV70LbChXVtkLmMcBEmy5tfqJ3Jl/9qYIq9EcQYX53faI931XKdy/jbF/L0TY51KoGi4X4Xvv';
	$private_key = "-----BEGIN PRIVATE KEY-----\n" . wordwrap($private_key, 64, "\n", true) . "\n-----END PRIVATE KEY-----";

$str1='12346y6njk;4312346y6njk;434534dfgsdfsfwef$%wefr23^#@r23r24534123412346y6njk;434534dfgsdfsfwef$%wefr23^#@r23r26y6njk;434534dfgs12346y6njk;434534dfgsdfsfwef$%wefr23^#@r23r2dfsfwef$%wefr23^#@r23r2dfgsdfsfwef$%wefr23^#@r23r234534dfgsdfsfwef$%wefr23^#@r23r2dfsfwef$%wefr23^#@r23r2dfgsdfsfwef$%wefr23^#@r23r2';

	$str2 = publicEncrypt($public_key,$str1);
	echo '原内容:'.$str1.'<br/>';
	echo '加密后:'.$str2.'<br/>';
	echo '解密后:'.privateDecrypt($private_key,$str2).'<br/>';

	/**
	* 公钥加密
	*/
	function publicEncrypt($public_key,$data){
	    $crypto = '';
		
		if(strlen($data)>245){
			foreach (str_split($data, 245) as $chunk) {
				openssl_public_encrypt($chunk, $encryptData, $public_key);
				$crypto .= $encryptData;
			}
		}else{
			openssl_public_encrypt($data, $crypto, $public_key);
		}
        return base64_encode($crypto);
	}

	/**
	* 私钥解密
	*/
	function privateDecrypt($private_key,$encryptstr) {
        // echo $encryptstr;exit;
        $encryptstr = base64_decode($encryptstr);
        $decrypted = array();
        $dataArray = str_split($encryptstr, 256);
        foreach($dataArray as $subData){
            $subDecrypted = null;
            openssl_private_decrypt($subData, $subDecrypted, $private_key);
            $decrypted[] = $subDecrypted;
        }
        $decrypted = implode('',$decrypted);
        return $decrypted;
    }

RSA加密解密在线工具:https://www.bejson.com/enc/rsa/

ad
本文来自网络,不代表本站立场,转载请注明出处。http://www.ygbks.com/1192.html

发表评论

您的电子邮箱地址不会被公开。

返回顶部