Skip to main content

JXTA and security problem (RSA)

1 reply [Last post]
pk_elsh
Offline
Joined: 2008-11-11

Hi,
I'm new with JXTA and going through examples with a book by J Gradecki - JXTA Building Java P2P Applications.
However, I stuck on chapter 15 (Implementing Security). Im trying to build a secure data transfer using JXTA Security Toolkit. Furthermore, Im using a code available with a book for "sender" and "receiver".
whenever, I am trying to send an encrypted data from "receiver" to the " sender" I am getting the same error message in my command window:

jxta.security.CryptoException (Invalid RSA padding)
at jxta.security.impl.publickey.RSA.removePadding (Unknown Source)
at jxta.security.impl.publickey.Algorithm (Unknown Source)
at sender$2.pipeMsgEvent(sender.java:243)
at net.jxta.impl.pipe.InputPipeImpl.processIncomingMessage(InputPipeImpl.java:260)
at net.jxta.impl.endpoint.EndpointDemuxListener.doOne(EndpointDemuxListener.java:316)
at net.jxta.impl.endpoint.EndpointDemuxListener$ListenerThread.run(EndpointDemuxListener.java:351)
at java.lang.Thread.run(Unknown Source)
Unable to decrypt
-------------------------------------------------------------------------------
This is a source code for "sender" (from the book)

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import net.jxta.document.*;
import net.jxta.peergroup.*;
import net.jxta.exception.PeerGroupException;
import net.jxta.impl.peergroup.Platform;
import net.jxta.impl.peergroup.GenericPeerGroup;
import net.jxta.id.*;
import net.jxta.discovery.DiscoveryService;
import net.jxta.protocol.*;
import net.jxta.platform.ModuleClassID;
import net.jxta.endpoint.Message;
import net.jxta.pipe.*;

import jxta.security.exceptions.CryptoException;
import jxta.security.util.Util;
import jxta.security.util.GetOpt;
import java.lang.IllegalArgumentException;
import jxta.security.publickey.RSAPublickeyData;
import jxta.security.publickey.RSAPrivatekeyData;

import jxta.security.impl.publickey.RSAKey;
import jxta.security.impl.publickey.RSA;
import jxta.security.impl.cipher.KeyBuilder;

public class sender extends JFrame {

static PeerGroup netPeerGroup = null;
private DiscoveryService myDiscoveryService = null;
private PipeService myPipeService = null;
private ModuleClassID myService1ID = null;
private InputPipe myPipe = null;
private OutputPipe myOutputPipe = null;
private JTextArea displayArea;

private final static MimeMediaType XMLMIMETYPE = new MimeMediaType("text/xml");

RSAKey rKey;
RSA rsa;
RSAPublickeyData rPublicD;
RSAPrivatekeyData rPrivateD;

public static void main(String args[]) {
sender myapp = new sender();

myapp.addWindowListener (
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);

myapp.run();
}

/*
* Default Constructor
*/
public sender() {
super("sender");

Container c = getContentPane();

displayArea = new JTextArea();
c.add (new JScrollPane(displayArea), BorderLayout.CENTER);

setSize(300,150);
show();

launchJXTA();
getServices();
doSecurity();
buildModuleAdvertisement();
buildModuleSpecificationAdvertisement(createPipeAdvertisement());

}

public void run() {
displayArea.append("Waiting for message...\n");
}

private void launchJXTA() {
displayArea.append("Launching Peer into JXTA Network...\n");
try {
netPeerGroup = PeerGroupFactory.newNetPeerGroup();
} catch (PeerGroupException e) {
System.out.println("Unable to create PeerGroup - Failure");
e.printStackTrace();
System.exit(1);
}
}

private void getServices() {
displayArea.append("Obtaining Discovery and Pipe Services....\n");
myDiscoveryService = netPeerGroup.getDiscoveryService();
myPipeService = netPeerGroup.getPipeService();
}

private void doSecurity() {
try {
rKey = (RSAKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA, KeyBuilder.LENGTH_RSA_512, false);
rsa = new RSA(rKey);
rsa.setPublicKey();
rsa.setPrivateKey();
rPublicD = (RSAPublickeyData)rsa.getPublickey();
rPrivateD = (RSAPrivatekeyData)rsa.getPrivatekey();

} catch (Exception e) {
System.out.println("Error during RSA initialization");
System.exit(-1);
}
}

private void buildModuleAdvertisement() {
ModuleClassAdvertisement myService1ModuleAdvertisement = (ModuleClassAdvertisement) AdvertisementFactory.newAdvertisement(ModuleClassAdvertisement.getAdvertisementType());

myService1ModuleAdvertisement.setName("JXTAMOD:JXTA-CH18EX3");
myService1ModuleAdvertisement.setDescription("Service 1 of Chapter 18 example 3");

myService1ID = IDFactory.newModuleClassID();
myService1ModuleAdvertisement.setModuleClassID(myService1ID);

displayArea.append("Publishing our Module Advertisement....\n");
try {
myDiscoveryService.publish(myService1ModuleAdvertisement, DiscoveryService.ADV);
myDiscoveryService.remotePublish(myService1ModuleAdvertisement, DiscoveryService.ADV);
} catch (Exception e) {
System.out.println("Error during publish of Module Advertisement");
System.exit(-1);
}
}

private PipeAdvertisement createPipeAdvertisement() {
PipeAdvertisement myPipeAdvertisement = null;

try {
FileInputStream is = new FileInputStream("service1.adv");
myPipeAdvertisement = (PipeAdvertisement)AdvertisementFactory.newAdvertisement(new MimeMediaType("text/xml"), is);
} catch (Exception e) {
System.out.println("failed to read/parse pipe advertisement");
e.printStackTrace();
System.exit(-1);
}

return myPipeAdvertisement;
}

private void buildModuleSpecificationAdvertisement(PipeAdvertisement myPipeAdvertisement) {

ModuleSpecAdvertisement myModuleSpecAdvertisement = (ModuleSpecAdvertisement) AdvertisementFactory.newAdvertisement(ModuleSpecAdvertisement.getAdvertisementType());

myModuleSpecAdvertisement.setName("JXTASPEC:JXTA-CH18EX3");
myModuleSpecAdvertisement.setVersion("Version 1.0");
myModuleSpecAdvertisement.setCreator("gradecki.com");
myModuleSpecAdvertisement.setModuleSpecID(IDFactory.newModuleSpecID(myService1ID));
myModuleSpecAdvertisement.setSpecURI("");
myModuleSpecAdvertisement.setPipeAdvertisement(myPipeAdvertisement);

displayArea.append("Publishing Module Specification Advertisement....\n");
try {
myDiscoveryService.publish(myModuleSpecAdvertisement, DiscoveryService.ADV);
myDiscoveryService.remotePublish(myModuleSpecAdvertisement, DiscoveryService.ADV);
} catch (Exception e) {
System.out.println("Error during publish of Module Specification Advertisement");
e.printStackTrace();
System.exit(-1);
}

createInputPipe(myPipeAdvertisement);
}

private void createInputPipe(PipeAdvertisement myPipeAdvertisement) {
displayArea.append("Creating Input Pipe....\n");

PipeMsgListener myService1Listener = new PipeMsgListener() {
public void pipeMsgEvent(PipeMsgEvent event) {
Message myMessage = null;
try {
myMessage = event.getMessage();

String myMessageContent;

myMessageContent = myMessage.getString("RequestKey");
if (myMessageContent != null) {
createOutputPipe(myMessageContent);
sendKey();
return;
}
myMessageContent = myMessage.getString("EncryptedData");
if (myMessageContent != null) {
byte[] encryptedBytes = myMessageContent.getBytes();
byte[] decryptedBytes;

try {
rsa.setPrivateKey(rPrivateD);
decryptedBytes = rsa.Algorithm(encryptedBytes, 0, encryptedBytes.length, KeyBuilder.TYPE_RSA_PRIVATE, false);
} catch (Exception eee) {
eee.printStackTrace();
System.out.println("Unable to decrypt\n");
return;
}
String newString = new String(decryptedBytes);
displayArea.append("Message received: " + newString + "\n");
return;
} else {
displayArea.append("Invalid tag\n");
return;
}
} catch (Exception ee) {
ee.printStackTrace();
return;
}
}
};

try {
myPipe = myPipeService.createInputPipe(myPipeAdvertisement, myService1Listener);
} catch (Exception e) {
System.out.println("Error creating Input Pipe");
e.printStackTrace();
System.exit(-1);
}
}

private void createOutputPipe(String myPipeAdvertisementString) {
boolean noPipe = true;
int count = 0;

PipeAdvertisement myOutputPipeAdvertisement = null;
try {
myOutputPipeAdvertisement = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(XMLMIMETYPE, new ByteArrayInputStream(myPipeAdvertisementString.getBytes()));
} catch (Exception e) {
System.out.println("Error creating output Pipe");
e.printStackTrace();
System.exit(-1);
}

myOutputPipe = null;
while (noPipe && count < 10) {
count++;
try {
myOutputPipe = myPipeService.createOutputPipe(myOutputPipeAdvertisement, 100000);
displayArea.append("Output Pipe Created...\n");
noPipe = false;
} catch (Exception e) {
System.out.println("Unable to create output pipe");
System.exit(-1);
}
}

if (count >= 10) {
System.out.println("no Pipe");
System.exit(-1);
}
}

private void sendKey() {

byte[] myBytes = rPublicD.getModulus();
ByteArrayOutputStream myStream = null;
try {
myStream = (ByteArrayOutputStream) new ByteArrayOutputStream();
myStream.write(myBytes, 0, myBytes.length);
} catch (Exception e) {
System.out.println("Unable encode public key");
e.printStackTrace();
System.exit(-1);
}

Message msg = myPipeService.createMessage();
msg.setString("RequestedKey", myStream.toString() );

try {
myOutputPipe.send (msg);
} catch (Exception e) {
System.out.println("Unable to print output pipe");
e.printStackTrace();
System.exit(-1);
}

displayArea.append("key sent to the Server\n");
}
}

-------------------------------------------------------------------------------
and source code for "receiver" (from the book)

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.util.Enumeration;
import net.jxta.document.*;
import net.jxta.peergroup.*;
import net.jxta.exception.PeerGroupException;
import net.jxta.impl.peergroup.Platform;
import net.jxta.impl.peergroup.GenericPeerGroup;
import net.jxta.id.*;
import net.jxta.discovery.*;
import net.jxta.pipe.*;
import net.jxta.protocol.*;
import java.net.MalformedURLException;
import java.net.URL;
import net.jxta.endpoint.Message;

import jxta.security.exceptions.CryptoException;
import jxta.security.util.Util;
import jxta.security.util.GetOpt;
import java.lang.IllegalArgumentException;
import jxta.security.publickey.RSAPublickeyData;
import jxta.security.publickey.RSAPrivatekeyData;

import jxta.security.impl.publickey.RSAKey;
import jxta.security.impl.publickey.RSA;
import jxta.security.impl.cipher.KeyBuilder;

public class receiver extends JFrame {

static PeerGroup netPeerGroup = null;
private DiscoveryService myDiscoveryService = null;
private PipeService myPipeService = null;
private PipeAdvertisement myPipeAdvertisement = null,
myInputPipeAdvertisement = null;
private OutputPipe myOutputPipe;
private InputPipe myInputPipe;
private JTextArea displayArea;
private JButton sendButton;
private String valueString = "JXTA-CH15EX2";

private final static MimeMediaType XMLMIMETYPE = new MimeMediaType("text/xml");

RSAKey rKey;
RSA rsa;
RSAPublickeyData rPublicD;

public static void main(String args[]) {
receiver myapp = new receiver();

myapp.addWindowListener (
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);

myapp.launchJXTA();
myapp.getServices();
myapp.createInputPipe();
myapp.findAdvertisement("Name", "JXTASPEC:JXTA-CH18EX3");
myapp.run();
}

public receiver() {
super("receiver");

Container c = getContentPane();

sendButton = new JButton("Send Key Request");
sendButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendKeyRequest();
}
}
);
c.add(sendButton, BorderLayout.NORTH);

sendButton = new JButton("Send encrypted data");
sendButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendEncryptedData();
}
}
);
c.add(sendButton, BorderLayout.SOUTH);

displayArea = new JTextArea();
c.add(new JScrollPane(displayArea), BorderLayout.CENTER);

setSize(300,150);
show();
}

public void run() {
displayArea.append("Click on Button to send data...\n");
}

private void launchJXTA() {
displayArea.append("Launching Peer into JXTA Network...\n");
try {
netPeerGroup = PeerGroupFactory.newNetPeerGroup();
} catch (PeerGroupException e) {
System.out.println("Unable to create PeerGroup - Failure");
e.printStackTrace();
System.exit(1);
}
}

private void getServices() {
displayArea.append("Getting Services...\n");
myDiscoveryService = netPeerGroup.getDiscoveryService();
myPipeService = netPeerGroup.getPipeService();
}

private void createInputPipe() {
displayArea.append("Creating Input Pipe....\n");

myInputPipeAdvertisement = (PipeAdvertisement)AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
myInputPipeAdvertisement.setName("JXTA:KEYRECEIVE");
myInputPipeAdvertisement.setType("JxtaUnicast");
myInputPipeAdvertisement.setPipeID((ID) net.jxta.id.IDFactory.newPipeID(netPeerGroup.getPeerGroupID()));

PipeMsgListener myService1Listener = new PipeMsgListener() {
public void pipeMsgEvent(PipeMsgEvent event) {
Message myMessage = null;
try {
myMessage = event.getMessage();

String myMessageContent;

myMessageContent = myMessage.getString("RequestedKey");
if (myMessageContent != null) {
doSecurity(myMessageContent);
return;
} else {
displayArea.append("Invalid tag\n");
return;
}
} catch (Exception ee) {
ee.printStackTrace();
return;
}
}
};

try {
myInputPipe = myPipeService.createInputPipe(myInputPipeAdvertisement, myService1Listener);
} catch (Exception e) {
System.out.println("Error creating Input Pipe");
e.printStackTrace();
System.exit(-1);
}
}

private void findAdvertisement(String searchKey, String searchValue) {
Enumeration myLocalEnum = null;
displayArea.append("Trying to find advertisement...\n");

try {
myLocalEnum = myDiscoveryService.getLocalAdvertisements(DiscoveryService.ADV, searchKey, searchValue);

if ((myLocalEnum != null) && myLocalEnum.hasMoreElements()) {
displayArea.append("Found Local Advertisement...\n");
ModuleSpecAdvertisement myModuleSpecAdv = (ModuleSpecAdvertisement)myLocalEnum.nextElement();

myPipeAdvertisement = myModuleSpecAdv.getPipeAdvertisement();
createOutputPipe(myPipeAdvertisement);
}
else {
DiscoveryListener myDiscoveryListener = new DiscoveryListener() {
public void discoveryEvent(DiscoveryEvent e) {
Enumeration enum;
PipeAdvertisement pipeAdv = null;
String str;

displayArea.append("Found Remote Advertisement...\n");
DiscoveryResponseMsg myMessage = e.getResponse();
enum = myMessage.getResponses();
str = (String)enum.nextElement();

try {
ModuleSpecAdvertisement myModSpecAdv = (ModuleSpecAdvertisement) AdvertisementFactory.newAdvertisement(XMLMIMETYPE, new ByteArrayInputStream(str.getBytes()));
myPipeAdvertisement = myModSpecAdv.getPipeAdvertisement();

createOutputPipe(myPipeAdvertisement);
} catch(Exception ee) {
ee.printStackTrace();
System.exit(-1);
}
}
};

displayArea.append("Launching Remote Discovery Service...\n");
myDiscoveryService.getRemoteAdvertisements(null, DiscoveryService.ADV, searchKey, searchValue, 1, myDiscoveryListener);
}
} catch (Exception e) {
System.out.println("Error during advertisement search");
System.exit(-1);
}
}

private void createOutputPipe(PipeAdvertisement myPipeAdvertisement) {
boolean noPipe = true;
int count = 0;

myOutputPipe = null;
while (noPipe && count < 10) {
count++;
try {
myOutputPipe = myPipeService.createOutputPipe(myPipeAdvertisement, 100000);
displayArea.append("Output Pipe Created...\n");
noPipe = false;
} catch (Exception e) {
System.out.println("Unable to create output pipe");
System.exit(-1);
}
}

if (count >= 10) {
System.out.println("no Pipe");
System.exit(-1);
}
}

private void sendKeyRequest() {

ByteArrayOutputStream myStream = (ByteArrayOutputStream) new ByteArrayOutputStream();
StructuredTextDocument paramDoc = (StructuredTextDocument)myInputPipeAdvertisement.getDocument(new MimeMediaType("text/xml"));
try {
paramDoc.sendToStream(myStream);
} catch (Exception e) {
System.out.println("Unable to print output pipe");
e.printStackTrace();
System.exit(-1);
}

Message msg = myPipeService.createMessage();
msg.setString("RequestKey", myStream.toString() );

try {
myOutputPipe.send (msg);
} catch (Exception e) {
System.out.println("Unable to print output pipe");
e.printStackTrace();
System.exit(-1);
}
}

private void sendEncryptedData() {
String sendString = null;

try {
byte[] plainBytes = new byte[rsa.getMaxInputDataBlockLength()];
for (int i=0;i

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pk_elsh
Offline
Joined: 2008-11-11

byte[] encryptedBytes = rsa.Algorithm(plainBytes, 0, plainBytes.length, KeyBuilder.TYPE_RSA_PUBLIC, true);
sendString = new String(encryptedBytes);
} catch (Exception e) {
System.out.println("Unable to encrypted string");
e.printStackTrace();
System.exit(-1);
}

Message msg = myPipeService.createMessage();
msg.setString("EncryptedData", sendString );

try {
myOutputPipe.send (msg);
} catch (Exception e) {
System.out.println("Unable to print output pipe");
e.printStackTrace();
System.exit(-1);
}

displayArea.append("message \"" + sendString + "\" sent to the Server\n");
}

void doSecurity(String myPublicKeyString) {

try {
rKey = (RSAKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA, KeyBuilder.LENGTH_RSA_512, false);
rsa = new RSA(rKey);
rsa.setPublicKey(myPublicKeyString.getBytes());
} catch (Exception e) {
System.out.println("Unable to do client security\n");
System.exit(-1);
}
}

}

-----------------------------------------------------------
I run Java version 1.6.0_10 and use JXTA 1.0 with following files in ..\lib
ant.jar, bcprov-jdk14.jar, beepcore.jar, cryptix32.jar, cryptix-asn1.jar, instantp2p, javax.servlet.jar, jaxen-core.jar, jaxen-jdom.jar, jdom.jar, jxta.jar, jxtacms.jar, jxtaptls.jar, jxtasecurity.jar, jxtashell.jar, log4j.jar, minimalBC.jar, org.mortbay.jetty.jar, pjxta.jar, saxpath.jar, xalan.jar, xerces.jar, xml-apis.jar

Furthermore I am runing both sender and receiver on the same machine.

Finaly, it is working without security implemented, but it is not working with that simple encryption.

I would really appropriate if anyone more experinced can help so I can move further with my JXTA adventure.

Many thanks, Pedro