使用php语言对opensSL加密解密的方法,把这些方法封装成一个类,方便大家调用。 废话不多说,直接贴代码
第一步是新建类RSAEncryptTest
//公钥是根据私钥来生成的 class RSAEncryptTest { const PRIVATE_KEY = "-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQC/R0jRS2FdORhvkiDsDlMnX+Lqa06WHRxEewNw85pb8d6cvI7e jzOjFok2tadwhr94Ynp/9r/pxpyNo8NhZ2gvOSZA/A1ejwkk1XK5aK/EysuI/JlC Vf8xT/Sqay1G0Dc3Y4kTi/B3SwsxOGBHLbXcfOczdX/uVDrOjXQ+1FZfdQIDAQAB AoGBALh8nqUPGWd6rDDgNlS3NEij2Q7UVMXAagU6yITm0BiiXl26F+pdICbKk6lq 54ZG0OHhydkmOs/VuiV6r/WAcSB8V6gr6iDTlLBzH5bYvJbY1faZvRZ8fadueg+D 5beRi9qkaAMlN4USu8IOioloHWjlk0BTLmnPwRJ0IxbGGzY5AkEA/J91vRAz4sde gFt2DdVNyJHztqJQxnELDW0/1Chc8iGO9OEzCcyyesUK63+y0Gyr7yuQqFHMNnAS s+O0aloTDwJBAMHV42Cs8QKhB8oDlIw9w6cvmR0ofYOVOqnIEgqDCmJDMf5EDfoI ckYRl/2PUAfBPlgLsED5ftxKuIYi+gLAVTsCQEK2baZXEAy0b71f6eYvTVJJUN4L MV+mXK/YCQIjAqCiwhjjG9Jbr0qCnKaiW3t1J1aK37HDoDVEFXxSBsQLM3sCQAi5 ngCOCFM7+GPJgXhGl3YBGH1Ng0jXAMr/rIA4odEGgUuWuKJWmv44cIUDXHDc6/d+ nzJtiJ5V5rRzApn5Gl0CQGUA/AwrbxYY3Nppq2yqZ/P8vgx/ibb04lBTHPoGjECV AGpNs2YXkw/56FucJs0VtCjjKh2e3euCL7kh9jyJu78= -----END RSA PRIVATE KEY----- "; const PUBLIC_KEY = "-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/R0jRS2FdORhvkiDsDlMnX+Lq a06WHRxEewNw85pb8d6cvI7ejzOjFok2tadwhr94Ynp/9r/pxpyNo8NhZ2gvOSZA /A1ejwkk1XK5aK/EysuI/JlCVf8xT/Sqay1G0Dc3Y4kTi/B3SwsxOGBHLbXcfOcz dX/uVDrOjXQ+1FZfdQIDAQAB -----END PUBLIC KEY-----"; private static $private_key; //私钥 private static $public_key; //公钥 /** * 第一种根据 私钥加密 * Enter description here ... * @param unknown_type $str 需要加密的字符串 * @return 返回是base64 加密后的字符串 */ public static function private_encrypt($str){ self::setup_key(); if(openssl_private_encrypt($str, $encrypted, self::$private_key)) return $encrypted; } /** * 根据公钥加密的字符串,利用私钥来解密 * Enter description here ... * @param string $str 需要解密的字符串 * @return 返回解密后的字符串 */ public static function private_decrypt($str){ self::setup_key(); if(openssl_private_decrypt($str, $decrypted, self::$private_key)) return $decrypted; } /** * 公钥解密,根据 私钥加密的字符串,利用公钥来解密 * Enter description here ... * @param string $str 需要解密的字符串 * @return 返回解密后的字符串 */ public static function public_decrypt($str){ self::setup_key(); if(openssl_public_decrypt($str, $decrypted, self::$public_key)) return $decrypted; } /** * 第一种根据公钥加密 * Enter description here ... * @param unknown_type $str 需要加密的字符串 * @return 返回是base64 加密后的字符串 */ public static function public_encrypt($str){ self::setup_key(); if(openssl_public_encrypt($str, $encrypted, self::$public_key)) return $encrypted; } /** * 根据公钥和密钥生成密钥 * Enter description here ... */ private static function setup_key(){ if (!self::$private_key){ // 这里的test就是在生成证书的时候设置的私钥密码 self::$private_key = openssl_pkey_get_private(self::PRIVATE_KEY, "1234"); } if (!self::$public_key) self::$public_key = openssl_pkey_get_public(self::PUBLIC_KEY); } }
第一步是调用类,方便使用
//公钥加密字符串 xxx 第一种公钥加密 $public_encrypt=RSAEncryptTest::public_encrypt("xxx123456789"); echo "public_encrypt:\n"; echo base64_encode($public_encrypt) ; echo "\n";
//私钥解密 $private_decrypt=RSAEncryptTest::private_decrypt($public_encrypt); echo "private_decrypt:\n"; echo $private_decrypt; echo "\n";
//私钥加密字符串 xxx 第二次私钥加密 $public_encrypt=RSAEncryptTest::private_encrypt("xxx1234567810"); echo " public_encrypt:\n"; echo base64_encode($public_encrypt) ; echo "\n";
//公钥解密 $private_decrypt=RSAEncryptTest::public_decrypt($public_encrypt); echo "private_decrypt:\n"; echo $private_decrypt; echo "\n";
上面就是这个类的使用方法,大家有需要可以根据自己的需求做相对应的调整。这个主要用于支付宝接口的开发当中。