php aes加密性能测试,php RSA和AES加密算法

这是一个PHP类,实现了RSA算法的公钥加密、私钥解密、私钥加密和公钥解密功能,以及数据签名和签名验证。类中包含了创建密钥对的方法,以及使用UrlSafeBase64编码和解码。这些方法对于数据的安全传输和完整性验证至关重要。
摘要由CSDN通过智能技术生成

{const CHAR_SET = "UTF-8";const BASE_64_FORMAT = "UrlSafeNoPadding";const RSA_ALGORITHM_KEY_TYPE =OPENSSL_KEYTYPE_RSA;const RSA_ALGORITHM_SIGN =OPENSSL_ALGO_SHA256;protected $public_key;protected $private_key;protected $key_len;public function __construct($pub_key, $pri_key = null)

{$this->public_key = $pub_key;$this->private_key = $pri_key;$pub_id = openssl_get_publickey($this->public_key);$this->key_len = openssl_pkey_get_details($pub_id)['bits'];

}/** 创建密钥对*/

public static function createKeys($key_size = 2048)

{$config = array("private_key_bits" => $key_size,

"private_key_type" => self::RSA_ALGORITHM_KEY_TYPE,);$res = openssl_pkey_new($config);

openssl_pkey_export($res, $private_key);$public_key_detail = openssl_pkey_get_details($res);$public_key = $public_key_detail["key"];return["public_key" => $public_key,

"private_key" => $private_key,];

}/** 公钥加密*/

public function publicEncrypt($data)

{$encrypted = '';$part_len = $this->key_len / 8 - 11;$parts = str_split($data, $part_len);foreach ($parts as $part) {$encrypted_temp = '';

openssl_public_encrypt($part, $encrypted_temp, $this->public_key);$encrypted .= $encrypted_temp;

}return $this->url_safe_base64_encode($encrypted);

}/** 私钥解密*/

public function privateDecrypt($encrypted)

{$decrypted = "";$part_len = $this->key_len / 8;$base64_decoded = url_safe_base64_decode($encrypted);$parts = str_split($base64_decoded, $part_len);foreach ($parts as $part) {$decrypted_temp = '';

openssl_private_decrypt($part, $decrypted_temp,$this->private_key);$decrypted .= $decrypted_temp;

}return $decrypted;

}/** 私钥加密*/

public function privateEncrypt($data)

{$encrypted = '';$part_len = $this->key_len / 8 - 11;$parts = str_split($data, $part_len);foreach ($parts as $part) {$encrypted_temp = '';

openssl_private_encrypt($part, $encrypted_temp, $this->private_key);$encrypted .= $encrypted_temp;

}return $this->url_safe_base64_encode($encrypted);

}/** 公钥解密*/

public function publicDecrypt($encrypted)

{$decrypted = "";$part_len = $this->key_len / 8;$base64_decoded = url_safe_base64_decode($encrypted);$parts = str_split($base64_decoded, $part_len);foreach ($parts as $part) {$decrypted_temp = '';

openssl_public_decrypt($part, $decrypted_temp,$this->public_key);$decrypted .= $decrypted_temp;

}return $decrypted;

}/** 数据签名*/

public function sign($data)

{

openssl_sign($data, $sign, $this->private_key, self::RSA_ALGORITHM_SIGN);return $this->url_safe_base64_encode($sign);

}/** 数据签名验证*/

public function verify($data, $sign)

{$pub_id = openssl_get_publickey($this->public_key);

//如果$data没有进行base64,请进行 base64后再验证$res = openssl_verify($data, $this->url_safe_base64_decode($sign), $pub_id, self::RSA_ALGORITHM_SIGN);return $res;

}/** base64_encode处理特殊字符(也有可能不需要替换,直接base64)*/

public function url_safe_base64_encode ($data) {return str_replace(array('+','/', '='),array('-','_', ''), base64_encode($data));

}/** base64_decode处理特殊字符(也有可能不需要,直接base64)*/

public function url_safe_base64_decode ($data) {$base_64 = str_replace(array('-','_'),array('+','/'), $data);return base64_decode($base_64);

}

}?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值