1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
public function getSign($data, $private_key) { $data = iconv('UTF-8', 'GB2312', $data); $private_key = base64_encode(hex2bin($private_key)); $private_key = chunk_split($private_key, 64, "\n"); $private_key = "-----BEGIN RSA PRIVATE KEY-----\n$private_key-----END RSA PRIVATE KEY-----\n"; openssl_sign($data, $signature, $private_key, OPENSSL_ALGO_SHA1);
return strtoupper(bin2hex($signature)); }
public function verifySign($data, $sign, $pub_key) { $data = iconv('UTF-8', 'GB2312', $data); $sign = hex2bin($sign); $pub_key = base64_encode(hex2bin($pub_key)); $pub_key = chunk_split($pub_key, 64, "\n"); $pub_key = "-----BEGIN PUBLIC KEY-----\n$pub_key-----END PUBLIC KEY-----\n"; $result = openssl_verify($data, $sign, $pub_key) === 1;
return $result; }
public function encryptAes($data, $key, $iv) { $key = hex2bin($key); $iv = hex2bin($iv); $data = iconv('UTF-8', 'GB2312', $data); $result = openssl_encrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
return strtoupper(bin2hex($result)); }
public function decryptAes($data, $key, $iv) { $data = hex2bin($data); $key = hex2bin($key); $iv = hex2bin($iv); $result = openssl_decrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv); $result = iconv('GB2312', 'UTF-8', $result);
return $result; }
public function decryptRsa($data, $key) { $data = hex2bin($data); $key = base64_encode(hex2bin($key)); $key = chunk_split($key, 64, "\n"); $key = "-----BEGIN RSA PRIVATE KEY-----\n$key-----END RSA PRIVATE KEY-----\n"; openssl_private_decrypt($data, $decrypted, $key); $decrypted = iconv('GB2312', 'UTF-8', $decrypted); return $decrypted; }
public function encryptRsa($data, $key) { $data = iconv('UTF-8', 'GB2312', $data); $key = base64_encode(hex2bin($key)); $key = chunk_split($key, 64, "\n"); $key = "-----BEGIN PUBLIC KEY-----\n$key-----END PUBLIC KEY-----\n"; openssl_public_encrypt($data, $crypted, $key); return strtoupper(bin2hex($crypted)); }
|