1. 程式人生 > >通用RSA加密 - PHP+Java+Javascript加密解密

通用RSA加密 - PHP+Java+Javascript加密解密

php端生成 公鑰私鑰

1、openssl genrsa -out rsa_private_key.pem 1024    私鑰

2、openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem  公鑰

3、openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt     將RSA私鑰轉換成PKCS8格式(java)

 

PHP端加密解密

class
Index { public function index(){ // 私鑰解密 $private_key = "-----BEGIN RSA PRIVATE KEY----- MIICWwIBAAKBgQC4zGR6jo8bcR19eKxJ2IpOxTjwMVya5Egh2ZTf/lXJmVsoOBwO ul/g0+fPI2aQ58mICjnxc0vpYz1M5INmcVIgJdOksN13CpTGpTLunD2xcHrE25Cx 5elNbQCPFICRQcnE1zqn8GPySm4EQO2UiHjoTalwfmhfYlS3cNaAcXrZfwIDAQAB AoGACcmRfMg2Hh70j+OnRFX6pUdex6HlpLJ4e4p5jw96Xa4InO2z1rfaQh8XBOp8 ESqlitX30ajomVjPM1Za+z7/OWuMYV/KehnX9SCKlC9uU9a2zu/oMN+hf2LiqlOy KWmJRzn1UduDKq6pejac8mltZhFduTFnyxOMtkW8/Qf1TvkCQQDqolU686aC5pQU cKsOi23rI+5K4s+T4SAum1vKFvsT9ebLiqru+EauZhHDOUH8plQIuV4tiEFEhzjq XVnwcpolAkEAyaBQ2Wsc/T1uDFGDpRy+nPdtHjQh6NTXvBF5VI3weeQJeCOl6hzQ IV3OWEyfXsXbYq0PzHz077jbM2g0OeiJ0wJAPDYgTRqNlnX5di1bmEXrlTYJRqph vn89NPilW3z+PacS7RTOvLh12vdSg5jD+EPuuLZpFqIqrZ1N28bM98lRCQJAKgs2 EMBXQS9oHUV6MBn4XX7y0BOK5qRLgDoclNcsSB6XIug8Fr9/XxXSi3Me4IJdpwvz eW6tHLftbaDTEyg8ZQJAPy9IuyA8WsHwBtybIxhCppJPGlMojoPyRZ5JWqZskzUB NQuu5zy2yio29hIILEJ6h46qnBdmEjd/C/fZ9oYjJw== -----END RSA PRIVATE KEY-----
"; // 公鑰加密 $public_key = "-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4zGR6jo8bcR19eKxJ2IpOxTjw MVya5Egh2ZTf/lXJmVsoOBwOul/g0+fPI2aQ58mICjnxc0vpYz1M5INmcVIgJdOk sN13CpTGpTLunD2xcHrE25Cx5elNbQCPFICRQcnE1zqn8GPySm4EQO2UiHjoTalw fmhfYlS3cNaAcXrZfwIDAQAB -----END PUBLIC KEY-----
"; // ====Java 私鑰=== // -----BEGIN PRIVATE KEY----- //MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALjMZHqOjxtxHX14 //rEnYik7FOPAxXJrkSCHZlN/+VcmZWyg4HA66X+DT588jZpDnyYgKOfFzS+ljPUzk //g2ZxUiAl06Sw3XcKlMalMu6cPbFwesTbkLHl6U1tAI8UgJFBycTXOqfwY/JKbgRA //7ZSIeOhNqXB+aF9iVLdw1oBxetl/AgMBAAECgYAJyZF8yDYeHvSP46dEVfqlR17H //oeWksnh7inmPD3pdrgic7bPWt9pCHxcE6nwRKqWK1ffRqOiZWM8zVlr7Pv85a4xh //X8p6Gdf1IIqUL25T1rbO7+gw36F/YuKqU7IpaYlHOfVR24Mqrql6NpzyaW1mEV25 //MWfLE4y2Rbz9B/VO+QJBAOqiVTrzpoLmlBRwqw6Lbesj7kriz5PhIC6bW8oW+xP1 //5suKqu74Rq5mEcM5QfymVAi5Xi2IQUSHOOpdWfBymiUCQQDJoFDZaxz9PW4MUYOl //HL6c920eNCHo1Ne8EXlUjfB55Al4I6XqHNAhXc5YTJ9exdtirQ/MfPTvuNszaDQ5 //6InTAkA8NiBNGo2Wdfl2LVuYReuVNglGqmG+fz00+KVbfP49pxLtFM68uHXa91KD //mMP4Q+64tmkWoiqtnU3bxsz3yVEJAkAqCzYQwFdBL2gdRXowGfhdfvLQE4rmpEuA //OhyU1yxIHpci6DwWv39fFdKLcx7ggl2nC/N5bq0ct+1toNMTKDxlAkA/L0i7IDxa //wfAG3JsjGEKmkk8aUyiOg/JFnklapmyTNQE1C67nPLbKKjb2EggsQnqHjqqcF2YS //N38L99n2hiMn //-----END PRIVATE KEY----- //========加密======= // $pi_key = openssl_pkey_get_private($private_key);// 可用返回資源id // $pu_key = openssl_pkey_get_public($public_key); // $data = '{"ip":"192.168.0.1","domain":"www.baidu.com"}'; // openssl_public_encrypt($data, $encrypted, $pu_key); // 公鑰加密 // // 加密後的字元 // var_dump($this->urlsafe_b64encode($encrypted)); // die; //========解密======== // php 加密後的值
//$str = "PXrR2AoSFPdMLis4HWKFbHsIsuAjHZVI8TXfsNvzrEYbafWLvpLm3wa_dqqRJwX2GCmOb2GKo087WCIAUPpZxpJHJ44uQKFEoagUQCyRi3LgCqL7CDPHJHmfEfFgp4hbk04hJ3LVPh4Pz4dOGc4To3OOgQSCIkvytY3fz7Fwv0U";      // js 使用php公鑰加密後的值
     // q5BaGcWoKFSasuAEXeTTy8PfcaHR4xdlurviK4fAoNT3jMiuN/vAJQVxklcFjqql2bn0i6IxG8DiWMAXm8nVFfTOINp811BLYjsHBymFStrTiEiPekL8txK4z5bl6OFi43FLQT96h8XY8zcJL/UFq/NJShVlg/kWHvVixfZF4NI= // java 使用PHP生成公鑰加密後的值 //O91XUcQV8pu_FqnuXO6qkfB-6tEVJVNdlHOnArEcj71JRq_rIHYkpYrOnEiMfKaXrNbyWISiPX14OkZ5mbK92ux58w8Vv8JLkizZ0QUESaJd974l1YcxiddBpKRcgxVB3Vh7RudchzFta64iiOru9fvGU5Uza4KiOLU9PqD8Z-w
$str = 'O91XUcQV8pu/FqnuXO6qkfB+6tEVJVNdlHOnArEcj71JRq/rIHYkpYrOnEiMfKaXrNbyWISiPX14OkZ5mbK92ux58w8Vv8JLkizZ0QUESaJd974l1YcxiddBpKRcgxVB3Vh7RudchzFta64iiOru9fvGU5Uza4KiOLU9PqD8Z+w='; $d = $this->urlsafe_b64decode($str); $pi_key = openssl_pkey_get_private($private_key); // 可用返回資源id @openssl_private_decrypt($d, $decrypted, $pi_key); // 私鑰解密 echo $decrypted; } //url base64解碼 protected function urlsafe_b64decode($string) { $data = str_replace(array('-','_'),array('+','/'),$string); $mod4 = strlen($data) % 4; if ($mod4) { $data .= substr('====', $mod4); } return base64_decode($data); } //url base64編碼 protected function urlsafe_b64encode($string) { $data = base64_encode($string); $data = str_replace(array('+','/','='),array('-','_',''),$data); return $data; } }

 

詳情可以瞭解:

https://my.oschina.net/gKWW0kOYB/blog/1836342

 

附js檔案下載:點選