2013-10-09 70 views
0

我必須開發一個PHP腳本來加密給定的RSA。php openssl rsa到java

解密它的程序是用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); 

我獲得:

14354fc9f5b151f2c5d0e29494b86182f9d698ab369aa8c5425ea9027108dc761f5a9205abb5d60d1442e85d5c 10dab33a89044e2b8f8d59b596a810559192690426d0bb199f673d304376c4ab83d400c3dcf38c7a78e545bd1044410b71a883415b20d9490f0f17ed7c7e2fc15eaccba89424925ee00343cf38311e6db0f37fef94347fbeec15173694ee74d8b942d83e1d611a5642df49595c7c41835ca2509fe61f8af88bc28d5b4a9a4ac15908c1028f1be1029b6cb104151f23aff429b7b5fca1b041939dc61cfa74bd2bed455704743844e77c42fb485cc3530261346f4b9f88db0b00eafbc8a23818e651d696eb0a7aec1a3870cba7e4f0dcf65cbdcf 大段引用

但解密服務器級別不起作用。

這裏是堆棧跟蹤:

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中正確的格式?

謝謝

回答

0

爲什麼不這樣做?:

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

正如你剛剛讀8192個字節,並把它的信念,真正的關鍵是有多大。

不管怎樣,Java是正確的 - 第一個字節確實應該是按照RFC PKCS1零:

灣串連PS,消息M和其他填充以形成長度爲k個八位組的 編碼消息EM作爲

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

你能後的樣品密鑰和明文樣品/密文?