我在Java中实现WebService,其中服务器需要使用RSA算法将3DES密钥发送到客户端。对称性由服务器生成。服务器和客户端都有自己的RSA密钥对,之前已交换过。
在此代码中,服务器将对称密钥发送给客户端。
@WebMethod
public byte[] getSymmetricKey(){
try{
Cipher cipher = Cipher.getInstance("RSA");
// First, encrypts the symmetric key with the client's public key
cipher.init(Cipher.ENCRYPT_MODE, this.clientKey);
byte[] partialCipher = cipher.doFinal(this.key.getBytes());
// Finally, encrypts the previous result with the server's private key
cipher.init(Cipher.ENCRYPT_MODE, this.privateKey);
byte[] cipherData = cipher.doFinal(partialCipher);
return cipherData;
}catch (Exception ex){
ex.printStackTrace();
}
}
当我使用服务器的私钥运行加密时,出现IllegalBlockSizeException
错误。如果默认情况下激活填充,为什么会出现此异常?我还尝试使用Cipher.getInstance("RSA/ECB/PKCS1Padding")
显式激活填充。
最后,这是异常输出:
SEVERE: javax.crypto.IllegalBlockSizeException: Data must not be longer than 245 bytes
javax.crypto.IllegalBlockSizeException: Data must not be longer than 245 bytes
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:346)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:391)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at server.FileTransfererImpl.getSymmetricKey(FileTransfererImpl.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.xml.internal.ws.api.server.MethodUtil.invoke(Unknown Source)
at com.sun.xml.internal.ws.api.server.InstanceResolver$1.invoke(Unknown Source)
at com.sun.xml.internal.ws.server.InvokerTube$2.invoke(Unknown Source)
at com.sun.xml.internal.ws.server.sei.EndpointMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.server.sei.SEIInvokerTube.processRequest(Unknown Source)
at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Unknown Source)
at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Unknown Source)
at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Unknown Source)
at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Unknown Source)
at com.sun.xml.internal.ws.server.WSEndpointImpl$2.process(Unknown Source)
at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(Unknown Source)
at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(Unknown Source)
at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(Unknown Source)
at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(Unknown Source)
at com.sun.net.httpserver.Filter$Chain.doFilter(Unknown Source)
at sun.net.httpserver.AuthFilter.doFilter(Unknown Source)
at com.sun.net.httpserver.Filter$Chain.doFilter(Unknown Source)
at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(Unknown Source)
at com.sun.net.httpserver.Filter$Chain.doFilter(Unknown Source)
at sun.net.httpserver.ServerImpl$Exchange.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:1)
我今天正在研究soem的东西,并发现了这个问题。由于尚未得到答复,我将此留待此处以供将来参考。
根据PKCS#1,RSAES-PKCS1-V1_5-ENCRYPT
算法可以加密最多k - 11
个字节,其中k
是"大小"键的字节数。这11个字节用于"标题"。
如果您使用的是2048位RSA密钥,则会为您提供k = 256
,并且您可以加密最多256 - 11 = 245
个字节的数据。
检查this.key
的实际尺寸。