0

我必须开发一个 PHP 脚本来加密给定的 RSA。

解密它的程序是用 Java 编写的,并且已经可以与发送此数据 RSA 的 Android 和 iOS 一起使用。

这是我的 PHP 脚本,它似乎加密正确,但我的数据类型有问题,因为 Java 进程预计会解密 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);

我试试这个:

bin2hex($encrypted);

我得到:

14354fc9f5b151f2c5d0e29494b86182f9d698ab369aa8c5425ea9027108dc761f5a9205abb5d60d1442e85d5c10dab33a89044e2b8f8d59b596a810559192690426d0bb199f673d304376c4ab83d400c3dcf38c7a78e545bd1044410b71a883415b20d9490f0f17ed7c7e2fc15eaccba89424925ee00343cf38311e6db0f37fef94347fbeec15173694ee74d8b942d83e1d611a5642df49595c7c41835ca2509fe61f8af88bc28d5b4a9a4ac15908c1028f1be1029b6cb104151f23aff429b7b5fca1b041939dc61cfa74bd2bed455704743844e77c42fb485cc3530261346f4b9f88db0b00eafbc8a23818e651d696eb0a7aec1a3870cba7e4f0dcf65cbdcf Blockquote

但是解密服务器级别不起作用。

这是堆栈跟踪:

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)

以及服务器上保存在数据库中的功能

//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;

你有什么想法在 PHP 中获得正确的格式吗?

谢谢

4

1 回答 1

0

为什么不这样做?:

$priv_key = '';
while (!feof($fp)) {
    $priv_key.= fread($fp,8192);
}
fclose($fp);

因为你只是在读取 8192 个字节,并相信这确实是密钥的大小。

无论如何,Java 是正确的——根据 PKCS1 RFC,第一个字节确实应该是零:

湾。连接 PS、消息 M 和其他填充以形成长度为 k 个八位字节的编码消息 EM

    EM = 0x00 || 0x02 || PS || 0x00 || M.

您可以发布示例密钥和示例明文/密文吗?

于 2013-10-10T03:08:21.493 回答