EldoS | Feel safer!

Software components for data protection, secure storage and transfer

DC Module (Multiple Files at once)

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
#28628
Posted: 02/28/2014 03:11:37
by Pablo Cervera (Standard support level)
Joined: 02/26/2014
Posts: 17

Hi Ken, i've a little doubt

On the applet side, at processData() method

Code
            XmlDocument doc = new XmlDocument();

            doc.Load(compStateStm);  // compStateStm should be in fact this.data, or not ?

            if (doc.DocumentElement.Name == "states")
            {
                for (int i = 0; i < doc.DocumentElement.ChildNodes.Count; i++)
                {
                    if (doc.DocumentElement.ChildNodes[i].Name == "state")
                    {
                        string b64 = doc.DocumentElement.ChildNodes[i].InnerXml;
                        byte[] state = Convert.FromBase64String(b64);
                        states.Add(new MemoryStream(state));
                    }
                }
            }

#28632
Posted: 02/28/2014 05:29:03
by Ken Ivanov (EldoS Corp.)

Hello Pablo,

Yes, correct.
#28645
Posted: 03/03/2014 06:25:29
by Pablo Cervera (Standard support level)
Joined: 02/26/2014
Posts: 17

Hi Ken,

I've almost complete all the packing unpacking.
But I'm stucked with sending the result XML back to the server, from the applet.

Here is my code.

Assuming outStreams contains now the instreams processed.

Code
                            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                            Document docOut = docBuilder.newDocument();
                            Element rootElement = docOut.createElement("states");
                            docOut.appendChild(rootElement);
                                  
                            for (int i=0;i<outStreams.length;i++)
                            {
                            
                                 Element stateOut = docOut.createElement("state");
                                 String b64out=Base64.encode(outStreams[i].toByteArray());
                                 stateOut.setNodeValue(b64out);
                                 rootElement.appendChild(stateOut);
                            }

  


on the first stage at C# I do

Code
MemoryStream compStateStm = new MemoryStream();
doc.Save(compStateStm)


But in Java, I don't know how to convert "docOut" XML to byteArray and encodetobase64 to send to the server.

What I need to do with to send it in the correct way to this.signature?

Thank's a lot!!

Best Regards
#28646
Posted: 03/03/2014 06:44:58
by Vsevolod Ievgiienko (EldoS Corp.)

Quote
But in Java, I don't know how to convert "docOut" XML to byteArray and encodetobase64 to send to the server.

The code may look this way:

Code
Document docOut = docBuilder.newDocument();
...
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource source = new DOMSource(docOut);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
StreamResult result = new StreamResult(bs);
transformer.transform(source, result);
byte[] xml = bs.toByteArray(); // xml as a byte array


For Base64 encoding you may use samples described here: http://stackoverflow.com/questions/13...ng-in-java
#28647
Posted: 03/03/2014 07:02:05
by Pablo Cervera (Standard support level)
Joined: 02/26/2014
Posts: 17

Thank you very much Vsevolod

I think I can do directly this

this.signature=Base64.encode(bs.toByteArray());


or not?

Thanks again!
#28648
Posted: 03/03/2014 07:13:55
by Pablo Cervera (Standard support level)
Joined: 02/26/2014
Posts: 17

Sorry, incompatible types.
Should be:

Code
byte[] xmlOut = Base64.encode(bs.toByteArray()).getBytes(); // xml as a byte array
                      
his.signature=xmlOut;
                      

#28649
Posted: 03/03/2014 07:16:56
by Vsevolod Ievgiienko (EldoS Corp.)

Yes you can.
#28650
Posted: 03/03/2014 08:24:23
by Pablo Cervera (Standard support level)
Joined: 02/26/2014
Posts: 17

Hi Vsevolod

I think i get the last error...hope :)

It's inside server.process method, at line that writing response document


I send to process.server an array of ByteArrayInputStream (inStream[]) for first parameter and for the second,
another array of ByteArrayOutputSteam (outSream[]) created by this way:

Code
  ByteArrayInputStream[] inStreams = new ByteArrayInputStream[2];
                        
                        if (doc.getDocumentElement().getNodeName().equalsIgnoreCase("states"))
                        {
                            NodeList listaEstados = doc.getElementsByTagName("state");
                            int totalStates = listaEstados.getLength();
                          
                            
                            for (int i = 0;i<totalStates;i++)
                            {

                                    Node stateNode = listaEstados.item(i);
                            
                                    String b64= stateNode.getTextContent();
                                    byte[] state = Base64.decode(b64);
                                    inStreams[i]=new ByteArrayInputStream(state);
                                                
                                    

                            }

                        
                        }
                        

ByteArrayOutputStream[] outStreams = new ByteArrayOutputStream[2]; //I'm doing test with only two documents.
ElDCStandardServer server = new ElDCStandardServer();
         try {
            ElDCX509SignOperationHandler handler = new ElDCX509SignOperationHandler();
            handler.setKeyStore(certificates);
            handler.setSigningCertificate(signingCert, signingKey);
            server.addOperationHandler(handler);
                                
            for (int i=0;i<inStreams.length;i++)
                                {
    
             //this one causes error. (the result object that has been passed to ''{0}''  isn't valid
                                    
                                     server.process(inStreams[i], outStreams[i]);
                                    
                                }
                        
                        
                        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Failed to sign the data.\nError: " + e.getMessage());
            labelProgressText.setText(PROGRESS_FAILED_TEXT);
            return false;
         }
      



At server.process method

Code
   // writing response document
         TransformerFactory transformerFactory = TransformerFactory.newInstance();
         Transformer transformer = transformerFactory.newTransformer();
         DOMSource source = new DOMSource(response);
         StreamResult result = new StreamResult(outStream);
         transformer.transform(source, result); //error here




What I'm doing wrong?
#28651
Posted: 03/03/2014 08:32:03
by Vsevolod Ievgiienko (EldoS Corp.)

Quote
transformer.transform(source, result); //error here

What kind of error?
#28652
Posted: 03/03/2014 09:35:24
by Pablo Cervera (Standard support level)
Joined: 02/26/2014
Posts: 17

A transformer exception
I've attached a screencap


Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.

Reply

Statistics

Topic viewed 3210 times

Number of guests: 2, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!