|
@@ -0,0 +1,132 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace crypt;
|
|
|
+
|
|
|
+class Rsa {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @uses 获取私钥
|
|
|
+ * @return bool|resource
|
|
|
+ */
|
|
|
+ private static function getPrivateKey()
|
|
|
+ {
|
|
|
+ $abs_path = dirname(__FILE__) . '/file/rsa_private_key.pem';
|
|
|
+ $content = file_get_contents($abs_path);
|
|
|
+ if (empty($content)) return false;
|
|
|
+
|
|
|
+ $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
|
|
|
+ wordwrap($content, 64, "\n", true) .
|
|
|
+ "\n-----END RSA PRIVATE KEY-----";
|
|
|
+
|
|
|
+ return openssl_pkey_get_private($res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @uses 获取公钥
|
|
|
+ * @return bool|resource
|
|
|
+ */
|
|
|
+ private static function getPublicKey()
|
|
|
+ {
|
|
|
+ $abs_path = dirname(__FILE__) . '/file/rsa_public_key.pem';
|
|
|
+ $content = file_get_contents($abs_path);
|
|
|
+ if (empty($content)) return false;
|
|
|
+
|
|
|
+ $res = "-----BEGIN PUBLIC KEY-----\n" .
|
|
|
+ wordwrap($content, 64, "\n", true) .
|
|
|
+ "\n-----END PUBLIC KEY-----";
|
|
|
+
|
|
|
+ return openssl_pkey_get_public($res);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static function getKenLen() {
|
|
|
+ $pub_id = openssl_get_publickey(self::getPublicKey());
|
|
|
+ return openssl_pkey_get_details($pub_id)['bits'];
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function getPK(){
|
|
|
+ return self::getPublicKey();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @uses 私钥加密
|
|
|
+ * @param string $data
|
|
|
+ * @return null|string
|
|
|
+ */
|
|
|
+ public static function privEncrypt($data = '')
|
|
|
+ {
|
|
|
+ if (!is_string($data)) return null;
|
|
|
+
|
|
|
+ $crypto = '';
|
|
|
+ $encrypted = '';
|
|
|
+ $len = self::getKenLen() / 8 - 11;
|
|
|
+
|
|
|
+ foreach (str_split($data, $len) as $chunk) {
|
|
|
+ openssl_private_encrypt($chunk, $encrypted, self::getPrivateKey());
|
|
|
+ $crypto .= $encrypted;
|
|
|
+ }
|
|
|
+
|
|
|
+ return base64_encode($crypto);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @uses 公钥加密
|
|
|
+ * @param string $data
|
|
|
+ * @return null|string
|
|
|
+ */
|
|
|
+ public static function publicEncrypt($data = '')
|
|
|
+ {
|
|
|
+ if (!is_string($data)) return null;
|
|
|
+
|
|
|
+ $crypto = '';
|
|
|
+ $encrypted = '';
|
|
|
+ $len = self::getKenLen() / 8 - 11;
|
|
|
+
|
|
|
+ foreach (str_split($data, $len) as $chunk) {
|
|
|
+ openssl_public_encrypt($chunk, $encrypted, self::getPublicKey());
|
|
|
+ $crypto .= $encrypted;
|
|
|
+ }
|
|
|
+
|
|
|
+ return base64_encode($crypto);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @uses 私钥解密
|
|
|
+ * @param string $encrypted
|
|
|
+ * @return null
|
|
|
+ */
|
|
|
+ public static function privDecrypt($encrypted = '')
|
|
|
+ {
|
|
|
+ if (!is_string($encrypted)) return null;
|
|
|
+
|
|
|
+ $crypto = '';
|
|
|
+ $len = self::getKenLen() / 8;
|
|
|
+
|
|
|
+ foreach (str_split(base64_decode($encrypted), $len) as $chunk) {
|
|
|
+ $decryptData = '';
|
|
|
+ openssl_private_decrypt($chunk, $decryptData, self::getPrivateKey());
|
|
|
+ $crypto .= $decryptData;
|
|
|
+ }
|
|
|
+ return $crypto;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @uses 公钥解密
|
|
|
+ * @param string $encrypted
|
|
|
+ * @return null
|
|
|
+ */
|
|
|
+ public static function publicDecrypt($encrypted = '')
|
|
|
+ {
|
|
|
+ if (!is_string($encrypted)) return null;
|
|
|
+
|
|
|
+ $crypto = '';
|
|
|
+ $decrypted = '';
|
|
|
+ $len = self::getKenLen() / 8;
|
|
|
+
|
|
|
+ foreach (str_split(base64_decode($encrypted), $len) as $chunk) {
|
|
|
+ openssl_public_decrypt($chunk, $decrypted, self::getPublicKey());
|
|
|
+ $crypto .= $decrypted;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $crypto;
|
|
|
+ }
|
|
|
+}
|