cancel
Showing results for 
Search instead for 
Did you mean: 

Executable jar file handling in SAP PI 7.31

former_member387652
Participant
0 Kudos

Hi Experts,

We are working on SAP PI 7.31 single stack which is installed on Linux SUSE OS and the requirement is to encrypt a file using an executable jar file, Can we achieve this using Java mapping where the input and output data structure will be of dummy type/ through command line feature. The executable jar file is given by receiver. Manually the encryption is done by command prompt on windows using below command.

java -jar  <<JARfilename>>.jar  <<Absolute file path of the file>>

As soon as the above command is executed a file gets generated on folder.Here I used my PC with windows OS but same thing need to be automated in case of Linux OS environment.

Where Passwords.txt  is the actual file & ePasswords.txt is the encrypted file.

The above command is creating an encrypted file with file name starting with "e<<actual file name>>" on same folder where the JAR file is present & the actual file . Actually requirement is a TXT file gets generated in a path on application server & PI needs to just encrypt the file using executable JAR file with out any manual intervention and no transformation of file is required & just need to encrypt the file and post the file as it is. Hence, I am not using any ESR objects only ID objects i.e., an ICO to pull & paste the file in receiver FTP but how to encrypt is the challenge.

Thanks,

Nithin.

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Naveen,

                  Did you download this jar file or created by your team?

If you have downloaded the jar file and using it for project purpose please ensure that you know the algorithm behind the jar file. This will in future run in production environment  of business so please be careful before you use it and verify its source.

In case the jar file is generated by your team and you do not posses the source java file then you can extract the source file using java decompiler. More on this in this link http://stackoverflow.com/questions/5107187/extract-source-code-from-jar-file.

Then create a java mapping code out of this source file. Add ESR objects an OM, an archive for jar file. You do not need an FCC.  In case you do not need ESR object , you need to convert the code to adapter module.

This way you can see the java mapping or adapter module  logs of whether the file was processed or not or errors if any.



Regards

Anupam


former_member387652
Participant
0 Kudos

Hi Anupam,

Thank you very much for your time. It is created by receiver system who needs the encrypted file & they have shared the executable JAR with us. I downloaded JD-GUI which is from which I can see the below snippet.

I have observed a class file with same name of that JAR file.I copied the java code present in that class file & pasting here since this is a test encryption file but the code to need to changed in such a way that file can be encrypted using java mapping . Kindly check on the feasibility and please help to change the code accordingly to achieve the above requirement of encryption of file using Java mapping. Thank you very much in advance!

Below is the code for TESTENC.class file, if you want content of others class files also I can give you.

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.PrintStream;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

public class TESTENC

{

  public static String asHex(byte[] buf)

  {

    StringBuffer strbuf = new StringBuffer(buf.length * 2);

    for (int i = 0; i < buf.length; i++)

    {

      if ((buf[i] & 0xFF) < 16) {

        strbuf.append("0");

      }

      strbuf.append(Long.toString(buf[i] & 0xFF, 16));

    }

    return strbuf.toString();

  }

 

  public static void encrypt(String file_in, String file_out)

    throws Exception

  {

    KeyGenerator kgen = KeyGenerator.getInstance("AES");

    kgen.init(128);

   

    SecretKey skey = kgen.generateKey();

    byte[] raw = skey.getEncoded();

    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

   

    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(1, skeySpec);

    String encryptedtext = "FUTURECAP0150847";

    byte[] encrypted = cipher.doFinal(encryptedtext.getBytes());

   

    cipher.init(2, skeySpec);

    byte[] original = cipher.doFinal(encrypted);

    String originalString = new String(original);

   

    byte[] arr = new byte[asHex(original).length() / 2];

    for (int start = 0; start < asHex(original).length(); start += 2)

    {

      String thisByte = asHex(original).substring(start, start + 2);

      arr[(start / 2)] = Byte.parseByte(thisByte, 16);

    }

    byte[] cipher1 = arr;

    SecretKeySpec skeySpec1 = new SecretKeySpec(cipher1, "AES");

   

    Cipher cipher2 = Cipher.getInstance("AES");

    cipher2.init(1, skeySpec1);

    CipherInputStream is = new CipherInputStream(new FileInputStream(new File(file_in)), cipher2);

    FileOutputStream os = new FileOutputStream(new File(file_out));

   

    byte[] b = new byte['?'];

    int i;

    while ((i = is.read(b)) != -1)

    {

      int i;

      os.write(b, 0, i);

    }

    os.close();

    is.close();

    System.out.println("\nThe selected file, (" +

      file_in + ") " + "has successfully encrypted to " + "(" +

      file_out + ").");

  }

 

  public static void main(String[] args)

    throws Exception

  {

    File input_file = null;

    File output_file = null;

    if (args.length > 0)

    {

      input_file = new File(args[0]);

      if (args.length > 1) {

        output_file = new File(args[1]);

      } else {

        output_file = new File(input_file.getAbsolutePath().replace(

          input_file.getName(),

          "e" +

          input_file.getName()));

      }

      if (input_file.exists()) {

        try

        {

          encrypt(input_file.getPath(), output_file.getPath());

        }

        catch (Exception e)

        {

          System.out.println("Error in encryption, contact Support team.");

        }

      } else {

        System.out.println("Input file not found please check the path.");

      }

    }

    else

    {

      System.out.println("Error : Parameters missing. \nUsage : java AESENC input_file_path [output_file_path]");

    }

  }

}

-Nithin.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Naveen,

                  I got busy with a project, now finally made out some time for this code.

Created a java mapping code as shown below. Input will be txt file output will also be the same file with same name, only difference will be the fact that output will be encrypted. Tick on ASMA , file name in both sender and receiver adapter and you are done.

All the best.

NB : if your query has been resolved kindly close the thread as discussed in this blog

Regards

Anupam


import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

public class TESTENC extends AbstractTransformation

{

  public static String asHex(byte[] buf)

  {

    StringBuffer strbuf = new StringBuffer(buf.length * 2);

    for (int i = 0; i < buf.length; i++)

    {

      if ((buf[i] & 0xFF) < 16) {

        strbuf.append("0");

      }

      strbuf.append(Long.toString(buf[i] & 0xFF, 16));

    }

    return strbuf.toString();

  }

  public static void encrypt(InputStream in, OutputStream out)

    throws Exception

  {

    KeyGenerator kgen = KeyGenerator.getInstance("AES");

    kgen.init(128);

 

    SecretKey skey = kgen.generateKey();

    byte[] raw = skey.getEncoded();

    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

 

    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(1, skeySpec);

    String encryptedtext = "FUTURECAP0150847";

    byte[] encrypted = cipher.doFinal(encryptedtext.getBytes());

 

    cipher.init(2, skeySpec);

    byte[] original = cipher.doFinal(encrypted);

    byte[] arr = new byte[asHex(original).length() / 2];

    for (int start = 0; start < asHex(original).length(); start += 2)

    {

      String thisByte = asHex(original).substring(start, start + 2);

      arr[(start / 2)] = Byte.parseByte(thisByte, 16);

    }

    byte[] cipher1 = arr;

    SecretKeySpec skeySpec1 = new SecretKeySpec(cipher1, "AES");

 

    Cipher cipher2 = Cipher.getInstance("AES");

    cipher2.init(1, skeySpec1);

    CipherInputStream is = new CipherInputStream(in, cipher2);

  

 

    byte[] b = new byte['?'];

    int i;

    while ((i = is.read(b)) != -1)

    {

      //int i;

      out.write(b, 0, i);

    }

    out.close();

    is.close();

 

  }

  public static void main(String[] args)

    throws Exception

  {

    File input_file = null;

    File output_file = null;

    if (args.length ==0)

    {

      input_file = new File("C:/apps/cat/1599_Senderonerecorddx3.xml");

      if (args.length ==0 ) {

        output_file = new File("C:/apps/cat/e1599_Senderonerecorddx3.txt");

      } else {

        output_file = new File(input_file.getAbsolutePath().replace(

          input_file.getName(),

          "e" +

          input_file.getName()));

      }

      if (input_file.exists()) {

        try

        {

        InputStream in = new FileInputStream(input_file);

        OutputStream out=new FileOutputStream(output_file);

          encrypt(in,out);

        }

        catch (Exception e)

        {

          System.out.println("Error in encryption, contact Support team.");

        }

      } else {

        System.out.println("Input file not found please check the path.");

      }

    }

    else

    {

      System.out.println("Error : Parameters missing. \nUsage : java AESENC input_file_path [output_file_path]");

    }

  }

@Override

public void transform(TransformationInput arg0, TransformationOutput arg1)

throws StreamTransformationException {

// TODO Auto-generated method stub

try {

  encrypt(arg0.getInputPayload().getInputStream(),arg1.getOutputPayload().getOutputStream());

} catch (Exception e) {

  e.printStackTrace();

  throw new StreamTransformationException(e.getMessage());

  // TODO Auto-generated catch block

}

}

}

former_member387652
Participant
0 Kudos

Hi Anupam,

Thank you very much for your response.

After importing the JAR file to JD-GUI I can see there are several class file but the code above is of .class file which with the same name as that of JAR file name. So do I need to import other class files also in to PI ESR or not? Also in your code I can see the input & output path as below.


        input_file = new File("C:/apps/cat/1599_Senderonerecorddx3.xml"); 

        if (args.length ==0 ) { 

        output_file = new File("C:/apps/cat/e1599_Senderonerecorddx3.txt"); 


Can you please suggest whether do I need to keep these as it is before importing or not?


Thanks,

Nithin.



former_member387652
Participant
0 Kudos

Hi Anupam,

I am facing below error when I try to open the class file in imported archive of ESR.

Also when I execute the OM after importing the TESTENC class file in to OM a linkage error as shown below.

Thanks,

Naveen.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Naveen,

                    I am not sure how you are exporting the jar file. Let me show you few screen shots. In eclipse or NWDS compile the java code.

After compiling the code export it as jar file as shown below

save it as a jar file and import the same in imported archive in ESR.

More about this process in my article

Beginners guide to Java mapping using DOM parser in SAP XI - Process Integration - SCN Wiki

In the ESR make message type

MT_Data

  Data  String 0.... 1

make two service interfaces

SI_Inbound and other SI_outbound.

In both interfaces include the same message type as explained above.

In the imported archive the jar file which you imported will have only two files TESTENC.class and TESTENC.class.

create a Operation Mapping and import the class TESTENC.class.

save activate and test run the mapping.

Rest all things follow as I have already explained in my last response.

There should not be any issue. Keep all line in code as I have uploaded and do not worry about those file names.

One more thing ensure that when you compile tha code in eclipse the compilation env should be J2SE 1.5.

check this path in eclipse or NWDS  . Right click on file name and go to build path as shown below

then check the build path for libraries before you proceed to export as jar file.

I did all steps and there was no error.

All the best.

Regards

Anupam

former_member387652
Participant
0 Kudos

Hi Anupam,

Thank you for your detailed response! Our PI is installed on JRE 1.7 , hope that should not cause any problem. Also my input file is not xml file & I see the below code lines where the input file is xml.

  1. input_file = new File("C:/apps/cat/1599_Senderonerecorddx3.xml"); 
  2.       if (args.length ==0 ) { 
  3.         output_file = new File("C:/apps/cat/e1599_Senderonerecorddx3.txt"); 


Should I comment those lines actually our input is xls file. Hope the above code will not work if i/p is xls/xlsx file.


For suppose if the input is xml also should I include these lines or comment them before importing it to PI.


Thanks,

Nithin.


anupam_ghosh2
Active Contributor
0 Kudos

you can safely include those lines. No need to comment them.

Regards

Anupam

former_member387652
Participant
0 Kudos

Hi Anupam,

Thank you for detailed explanation. It works only when the input file is xml in utf8. Can ABAPer generate xml in utf-8 with 777 permissions? or should I opt for sender proxy to enable ecc system to post the data in xml format? Kindly suggest.

Thanks,

Naveen.

anupam_ghosh2
Active Contributor
0 Kudos

This works for all types of file. Do not use FCC in sender or receiver channel.

Post any type of file.

Regards

Anupam

former_member387652
Participant
0 Kudos

Hi Anupam,

Thanks a ton it is working for both txt/xls files.Hope the encrypted file will be one & the same which got generated by PI & by manually executing the command in the command prompt as explained above.

-Nithin.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Naveen,

                   Output will be the same , as I preserved the original algorithm.

Thank you so much for closing the thread and marking my response as correct answer.

Regards

Anupam

former_member387652
Participant
0 Kudos

Hi Anupam,

Thank u for reply, this code is for testing as of now what if they change key in this code during live?  So can I change only key & follow the same procedure right?

Thanks,

Nithin.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Naveen,

                    In case the key changes , change the same in the code, compile the code, recreate jar file and upload again in PI. That should resolve the issue.

Regards

Anupam

former_member387652
Participant
0 Kudos

Hi Anupam,

Thank u for your response!

-Naveen.

Answers (2)

Answers (2)

former_member387652
Participant
0 Kudos

Hello All,

Please help me on the same.

Thank you!

Naveen.

nitindeshpande
Active Contributor
0 Kudos

Hi Naveen,

I do not really understand your requirement. There seem to be some confusion. Please answer the below questions -

1. Do you want to encrypt a JAR file?

2. What do you mean by, you want to encrypt using JAR file?

3. How do you get the input file which needs to be encrypted? This needs to be encrypted and dropped into which folder?

From the Java code, i see that, this is the code to be used for Encryption. And this is normal Java code and you need to tweak it to make it usable in SAP PI.

My suggestion is why to go for custom Java code, when we already have inbuilt methods for Encryption in SAP PI. Please find the below links which can be helpful for encryption. I would suggest you to go for PGP, In this you can use "AES" as Encryption Algorithm, same has been used in the Java code posted by you above.

Regards,

Nitin

iaki_vila
Active Contributor
0 Kudos