This problem arises in the continuation of the last question How RSA verify the signature in java that was generated in php . This code works for plain text. But now I have a requirement for signing and verifying the text, which also has a public key (except for the confirmation key) in the format.
text1:text2:exported-public-key
Example:
53965C38-E950-231A-8417-074BD95744A4:22-434-565-54544:MIIBCgKCAQEAxWg6ErfkN3xu8rk9WsdzjL5GpjAucMmOAQNeZcgMBxN+VmU43EnvsDLSxUZD1e/cvfP2t2/dzhtV6N2IvT7hveuo/zm3+bUK6AnAfo6pM1Ho0z4WetoYOrHdOVNMMPaytXiVkNlXyeWRF6rl9JOe94mMYWRJzygntiD44+MXsB6agsvQmB1l8thg/8+QHNOBBU1yC4pLQwwO2cb1+oIl0svESkGpzHk8xJUl5jL6dDnhqp8+01KE7AGHwvufrsw9TfVSAPH73lwo3mBMVXE4sfXBzC0/YwZ/8pz13ToYiN88DoqzcfD3+dtrjmpoMpymAA5FBc5c6xhPRcrn24KaiwIDAQAB
PHP code:
$rsa = new Crypt_RSA(); $keysize=2048; $pubformat = "CRYPT_RSA_PUBLIC_FORMAT_PKCS1"; $privformat = "CRYPT_RSA_PRIVATE_FORMAT_PKCS8"; $rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS8); $rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1); $d = $rsa->createKey($keysize); $Kp = $d['publickey']; $Ks = $d['privatekey']; $rsa = new Crypt_RSA(); $rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS8); $rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1); $d = $rsa->createKey($keysize); $Kver = $d['publickey']; $KSign = $d['privatekey']; $plainText = "53965C38-E950-231A-8417-074BD95744A4:22-434-565-54544:".$Kp; // Signing $hash = new Crypt_Hash('sha256'); $rsa = new Crypt_RSA(); $rsa->loadKey($KSign); $rsa->setSignatureMode(CRYPT_RSA_ENCRYPTION_PKCS1); $rsa->setHash('sha256'); $signature = $rsa->sign($plainText); $signedHS = base64_encode($signature); // Verification $signature = base64_decode($signedHS); $rsa->loadKey($Kver); $status = $rsa->verify($plainText, $signature); var_dump($status);
JAVA Code
import static java.nio.charset.StandardCharsets.UTF_8; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.math.BigInteger; import java.security.spec.X509EncodedKeySpec; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; import java.security.Security; import java.security.Signature; import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.RSAPublicKeySpec; Kwc / 10Jl3ie6mvn8hEsC3NAtMJu + Od12gyWYsS0zBDiQ8h2pGZ7p4uWqenc01dRRrq + g968zmoCKPUllPUuR6v9o + wYTX / os4hgaQSBg7DQn4g3BEekcvyk6e6zAMvuhHjeqnrinhCMFgJUhFL8zFNoyaH559C0TNbR6BTKzOoikah8cKhu4UOga0tWDC0I2Ifus / sHOwVaOBkDFIzD6jBxDH / QF8FsrLLTocuIb7Y6lVxFPPtgiUJku6b7wKExV0bPJvm6 / Xhv1GX1FpMrA0Ylzj5IFviuviwgo534EcZQ / Hx3aIf4oPG8jVTQIDAQAB"; import static java.nio.charset.StandardCharsets.UTF_8; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.math.BigInteger; import java.security.spec.X509EncodedKeySpec; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; import java.security.Security; import java.security.Signature; import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.RSAPublicKeySpec; 8417-074BD95744A4: 22-434-565-54544: MIIBCgKCAQEArszIunGg3ievJOpgesYQsp3nPGgrW + 3VwkivkkktOXUBRzb3G3mZzidEjG6LxNe / rrNe0UczmnSHQoSBxJCHyUnCWNfScBD66CFG4hLo5Z1gxrP8D2M2lCa6ap2PWcsKiWqlu38EinMeBjBvB4aYpF7 + FkFy64ObxR4pfVZxnxradkD0HvvMPLMbyeHxeGqYf8orERf9jfuKTdY8V44rxht2D2fg2WhB1 + XL0JulsPvgOaSK3RPnwi + RQAJbihCIh5Zznn0KQCs5pIWoT3XKe1DMpQuEmphSOY9ZUg3AwlOrpRV + 565x6GCSc615 / 6nowmqKzE4T7qT5nbH + ctiEHQIDAQAB"; import static java.nio.charset.StandardCharsets.UTF_8; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.math.BigInteger; import java.security.spec.X509EncodedKeySpec; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; import java.security.Security; import java.security.Signature; import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.RSAPublicKeySpec; Duo1 / PwrxTqC5BZFL / frqsRSVa + vpvGEnj3xe4iImTEasMicQzzaAG9IWIgkRZ272lUZ8PqdtTuqAsRIwir6fEsfVs5uIErEWM18R4JxlFBc3LDIjFOFemEPSVIEBHwWht1c / CrdTtxPRIiugEb1jdofEBUNcWPZgfvApVx5 + 0aS9WTl31AY + RMlvp + 13P / FQgAMnH9rvBdopRIVsZUNlMf8AOE2afhLPfOgx + 41rzCB2wGCrRGELbml466WJ3wYNQ =="; import static java.nio.charset.StandardCharsets.UTF_8; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.math.BigInteger; import java.security.spec.X509EncodedKeySpec; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; import java.security.Security; import java.security.Signature; import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.RSAPublicKeySpec;
It did not give any error, but simply returns false when using the public key in plainText. If you try to delete using the public key, it will work and return true.
PHP is working fine, and the signature is checked in all cases.
I suspect java cannot validate data with base 64 text / public key as text?
UPDATE: I compare the binary bytes of both two times, and the result shows a slight difference.
First case
- PHP → # C: sQ
- JAVA → / # C: sQ
Second case
- PHP →] Q0l O +
- JAVA →] Q0l
If php base64 is not compatible with apache base 64?
source share