I have to develop a PHP script to encrypt a given RSA.
The program that decrypts it is in Java, and already works with Android and iOS which sends this data RSA.
Here is my PHP script, which seems to encrypt correctly, but I have a problem with the data type as the Java process that is expected to decrypt a String :
$pwd = "111111";
$pseudo = "Vincent noir ";
$hash = hash("SHA512", $pwd.$pseudo);
for ($i = 0; $i < 7000 - 1; $i++) {
$hash = hash("SHA512", $hash.$pseudo);
}
$priv_key = openssl_pkey_get_private("file:///var/www/pprojet/classes/lib/certs/private_key.pem");
openssl_private_encrypt($hash, &$encrypted, $priv_key, OPENSSL_PKCS1_PADDING);
I try this :
bin2hex($encrypted);
I obtain :
14354fc9f5b151f2c5d0e29494b86182f9d698ab369aa8c5425ea9027108dc761f5a9205abb5d60d1442e85d5c10dab33a89044e2b8f8d59b596a810559192690426d0bb199f673d304376c4ab83d400c3dcf38c7a78e545bd1044410b71a883415b20d9490f0f17ed7c7e2fc15eaccba89424925ee00343cf38311e6db0f37fef94347fbeec15173694ee74d8b942d83e1d611a5642df49595c7c41835ca2509fe61f8af88bc28d5b4a9a4ac15908c1028f1be1029b6cb104151f23aff429b7b5fca1b041939dc61cfa74bd2bed455704743844e77c42fb485cc3530261346f4b9f88db0b00eafbc8a23818e651d696eb0a7aec1a3870cba7e4f0dcf65cbdcf Blockquote
But decryption server level does not work.
Here is the stacktrace :
javax.crypto.BadPaddingException: Data must start with zero
at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
at sun.security.rsa.RSAPadding.unpad(Unknown Source)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at com.wpf.projet.util.CryptData.decryptPwd(CryptData.java:117)
at com.wpf.projet.resources.UserAccountResource.updatePwdByMail(UserAccountResource.java:1721)
at sun.reflect.GeneratedMethodAccessor51.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:598)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:486)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:499)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:233)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1065)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:413)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:999)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
at org.eclipse.jetty.server.Server.handle(Server.java:350)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:454)
at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:900)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:954)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:851)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:606)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:46)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:603)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:538)
at java.lang.Thread.run(Unknown Source)
and the function on server what save on database
//cipher init
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
//charger la clef privée
PrivateKey privKey = (PrivateKey) getPrivateKey();
cipher.init(Cipher.DECRYPT_MODE, privKey);
//System.out.println("password entré : '" + password + "'");
byte[] cipherData= cipher.doFinal(toByte(password));
//System.out.println("password getbyte taille: '" + toByte(password).length + "'");
return cipherData;
Do you have any idea to get the right format in PHP?
Thank you
Why not do this?:
$priv_key = '';
while (!feof($fp)) {
$priv_key.= fread($fp,8192);
}
fclose($fp);
As is you're just reading 8192 bytes and taking it on faith that that really is how big the key is.
Anyway, Java is correct - the first byte should indeed be a zero per the PKCS1 RFC:
b. Concatenate PS, the message M, and other padding to form an encoded message EM of length k octets as
EM = 0x00 || 0x02 || PS || 0x00 || M.
Can you post a sample key and sample plaintext / ciphertext?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.