Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Encrypt large SealedObject


  • Please log in to reply
2 replies to this topic

#1 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 20 March 2012 - 02:55 AM

Hello,

I'm trying to create a SealedObject that contains a Serializable object and a cipher that initialise with RSA. I want to create a pair of keys for each user, so that's why I'm using RSA. I'm getting an Exception when trying to encrypt this object and I know that the reason is because key's size. My question is how I can create a SealedObject that will contains an Object[] (with two integers and a SecretKey) and encrypt it using user's public key?

Thanks in advance,
toto_7
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 20 March 2012 - 03:36 AM

I'm getting an Exception

Please post the full text of the error message and the code that caused it.
  • 0

#3 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 20 March 2012 - 03:48 AM

Here is the Exception:
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Data must not be longer than 245 bytes
	at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:337)
	at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382)
	at javax.crypto.Cipher.doFinal(Cipher.java:2086)
	at javax.crypto.SealedObject.<init>(SealedObject.java:172)
	at AuctionSecurity.encryptMessage(AuctionSecurity.java:71)
	at AuctionImpl.authUser(AuctionImpl.java:91)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
	at sun.rmi.transport.Transport$1.run(Transport.java:177)
	at sun.rmi.transport.Transport$1.run(Transport.java:174)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
	at java.lang.Thread.run(Thread.java:722)
	at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
	at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
	at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
	at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
	at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
	at $Proxy0.authUser(Unknown Source)
	at ClientSeller.main(ClientSeller.java:61)

Here what Server is trying to do:

Key secretKey = ausec.generateDES();
		int rndNum = new Random().nextInt(90000);
		ausec.sendMessage(username);
		SealedObject reply_obj = ausec.encryptMessage(new UserAuthorisation(userRnd, rndNum, secretKey));

and here is my encryption/decryption class:

public void sendMessage(String to) throws IOException, NoSuchAlgorithmException, 
	InvalidKeySpecException{
		File f = new File("keys//"+to+"_public.key");
		FileInputStream fis = new FileInputStream(f);
		byte[] enpubK = new byte[(int)f.length()];
		fis.read(enpubK);
		fis.close();

		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(enpubK);
		pubkey = keyFactory.generatePublic(publicKeySpec);
	}


public SealedObject encryptMessage(Serializable message) throws InvalidKeyException, NoSuchAlgorithmException, 
	NoSuchPaddingException, IllegalBlockSizeException{
		try{
			Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(Cipher.ENCRYPT_MODE, pubkey);
			Key secretKey = generateDES();
			SealedObject so;
			so = new SealedObject(secretKey, cipher);
			return so;
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

//next decryption

**Please ignore previous version
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download