Php openssl_public_decrypt返回false,但无法获取错误消息
我试图将一个字符串作为某种数字签名,在Android设备上用RSA私钥编码。我想我在服务器端的公钥有问题Php openssl_public_decrypt返回false,但无法获取错误消息,php,openssl,Php,Openssl,我试图将一个字符串作为某种数字签名,在Android设备上用RSA私钥编码。我想我在服务器端的公钥有问题 if (openssl_public_decrypt($token, $decrypted, $pubKey) == FALSE) { while ($msg = openssl_error_string()) { echo "ERROR: " . $msg; } return; } 函数openssl_public_decrypt返回f
if (openssl_public_decrypt($token, $decrypted, $pubKey) == FALSE)
{
while ($msg = openssl_error_string())
{
echo "ERROR: " . $msg;
}
return;
}
函数openssl_public_decrypt返回false,但openssl_error_string不返回任何错误。我怎样才能知道这里出了什么问题
更新:
这是我在Android中创建编码值的方式:
public static String Sign(byte[] text, RSAPrivateKey privateKey) throws Exception
{
// Encode the original data with RSA private key
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
c.init(Cipher.ENCRYPT_MODE, privateKey);
encodedBytes = c.doFinal(text);
} catch (Exception e) {
Log.e("RSAHelper", "RSA encryption error");
throw e;
}
return Base64.encodeToString(encodedBytes, Base64.DEFAULT);
}
我正在尝试将一个字符串作为某种数字签名在Android设备上用RSA私钥编码
如果是数字签名,则填充方案不同。RSA加密使用类型2填充,而RSA签名使用类型1填充
既然您认为这是一个数字签名,那么您可能应该寻找openssl\u public\u verify函数或类似函数。我没有真正了解为什么openssl\u error\u字符串什么也不返回,但我可以用不接受的公钥解决我的问题 由于密钥对是由Android创建的,因此PHP无法直接使用。需要解决的问题有: 缺少页眉和页脚 换行错误
在我的问题中添加了Android端的编码
// remove existing line breaks
$pubKey = str_replace ("\r\n" , "" , $pubKey);
$pubKey = str_replace ("\r" , "" , $pubKey);
$pubKey = str_replace ("\n" , "" , $pubKey);
// insert line breaks as expexted by PHP function
$pubKey = wordwrap($pubKey, 65, "\n", true);
// add header and footer
$pubKeyWithHeader = "-----BEGIN PUBLIC KEY----- \r\n" . $pubKey
. " \r\n-----END PUBLIC KEY-----";
// decode the key
openssl_pkey_get_public($pubKeyWithHeader);
if ($x == FALSE)
{
echo "error";
return;
}
// decrypt the message with the public key
if (openssl_public_decrypt($token, $decrypted, $pubKeyWithHeader) == FALSE)
{
echo "error";
return;
}
echo $decrypted;