最近和java對接接口看文檔就是rsa 一開始怎麼也調不通;覺得就是rsa 加密一把 他們只給一個key 一個字符串php
MIIBVAIBADxxxxxxxEAAkEApWBS7aJbksmufLQZ889vhw5Uy93xGOqe3/YS5/trqgDMA9iZKNxttyBnU7ffj0c3bas7jUklZRnGlO7Tb+Aa8QIDAQABAkBeA7GBSKB4hVE9sYFjepSzmbEZKLYsbQ0e0xySZvsg7GMJFjFAbcFaqD7W4bFbG34QKvrEELbmRyxxxxx0yApG/yJPJlCXzN5Iaavcszx1al7mKPK8PECIQDIhtVhmgYEIM3W/TxxxxxxxxxxxxxJZ38VSU42phAEDBeq59IKkc15hyBQSgXnuM0x2yfszBAiBjS4D155L7ExxxxVhU/F/lMD7oOGoAQIgEM5LXv1q8Suf1oIDetZB4T7jMQ4vgRpQfR0g8YEFR/Y=
我就簡單的調用java
$pu_key = openssl_pkey_get_private($interface_key); 會報錯 // openssl_pkey_get_private(): key param is not a valid private key var_dump($pu_key) openssl_private_encrypt($sign, $resSign, $pu_key);
其實正規生成的pem 文件格式確實不和前面同樣;最後讓java給一個java的demo 最後看到SHA1WithRSA 就網上搜了一下php的實現post
<?php //生成 sha1WithRSA 簽名 function genSign($toSign, $privateKey){ //這裏他是拼接成和pem文件同樣的格式 $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . wordwrap($privateKey, 64, "\n", true) . "\n-----END RSA PRIVATE KEY-----"; $key = openssl_get_privatekey($privateKey); openssl_sign($toSign, $signature, $key); openssl_free_key($key); $sign = base64_encode($signature); return $sign; } //校驗 sha1WithRSA 簽名 function verifySign($data, $sign, $pubKey){ $sign = base64_decode($sign); $pubKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($pubKey, 64, "\n", true) . "\n-----END PUBLIC KEY-----"; $key = openssl_pkey_get_public($pubKey); $result = openssl_verify($data, $sign, $key, OPENSSL_ALGO_SHA1) === 1; return $result; } //測試 $privateKey = "xx...."; $pubKey = "xxx...."; //生成簽名 $sign = genSign("test", $privateKey); //校驗簽名 $rs = verifySign("test", $sign, $pubKey); var_dump($rs);
來源測試