Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

AES key transfer using UDP or TCP

843811Feb 1 2006
Hello

I am getting a problem is transmitting an AES key from a machine to another using the UDP transport protocol.

Actually i am sending an encoded byte array which will be used for generating a SecretKeySpec for the encryption/decryption process. Hoever, the problem that i noticed is that the content of teh byte array at the sender/client(the one who generates the byte array actually) is not the same as the one received after.Below is the code samples.

class testserver
{
public static void main(String args[])throws IOException
{
AES e=new AES();

try
{
byte[] buffer=new byte[16];
DatagramSocket aSocket = new DatagramSocket(222);
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
aSocket.receive(request);
byte[] data=request.getData();
System.out.println("Key received is"+data);
}catch(Exception Error){}
}
}
//this is the server class

->>this class is the client one responsible for generating a key ad sending it to the server

class testSendKey
{
public static void main(String args[])
{
AES e=new AES();

try
{
byte[] key=e.KeyGen(128);
System.out.println(key.length);
InetAddress aHost = InetAddress.getByName("pv");
DatagramSocket aSocket = new DatagramSocket();
DatagramPacket sending = new DatagramPacket(key, key.length,aHost, 222);
aSocket.send(sending);
aSocket.close();
System.out.println("Key sent is:"+key);
}
catch (Exception E){}
}
}

Now below is the AES method KeyGen(Int) which generates a key of a given size


public byte[] KeyGen(int size)throws Exception
{
KeyGenerator kgen = KeyGenerator.getInstance("AES",provider);
kgen.init(size);
//System.out.println(kgen);

// Generate the secret key specs.
SecretKey skey = kgen.generateKey();
byte[] rawByte = skey.getEncoded();

return rawByte;
}

Can someone please tell me whats the problem here.....or else give me an idea where i can generate a key of size (128,192,256) and send it to the server using either UDP or TCP....
Regards
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 1 2006
Added on Feb 1 2006
0 comments
306 views