cancel
Showing results for 
Search instead for 
Did you mean: 

Need help for generating security code which follow MD5 hash algorithm by using java mapping

Former Member
0 Kudos

Hi Folks,

I have a requirement to generate security code which has the algorithm for the calculation of security code can be summarized as follows:
MD5 hash (Electronic signature by private key (Unique invoice number + date +time of issue + numerical code of invoice + business space + charging devices + total invoice amount))
A decimal separator in the total amount of data is necessary to use a point and not a comma (eg 1245.56).

Using the MD5 cryptographic hash function (in standard RFC 1321 The MD5 Message-Digest Algorithm) gets the result: 32-digit number written in hexadecimal format (numbers and lowercase: 0-9,a-f), which is printed on the bill.
Example of protective code: a9e6b1488f0cc775f0c82aa7a1372e53

Electronic signatures is performed using RSA-SHA1 electronic signature with the relevant certificate that is assigned to a each customer.

I tried to write java code like below using the pseudo code given.

I would be very much thankful to the experts if any one provide the java code for java mapping.

In the java mapping I have to read the values from the payload, but for my sake I tool all the values as constant.  

Java code:

package com.sap.pi.demo;

import java.io.FileInputStream;

import java.security.Key;

import java.security.KeyStore;

import java.security.PrivateKey;

import java.security.Signature;

import java.text.SimpleDateFormat;

import java.util.Date;

import com.sun.security.sasl.digest.DigestUtils;

public class SecuritycodeCalcualtion {

          public static void main( String[] args ) {

 

//             reading unique invoice number (uin)

          String uin = "00169331406";

//             subscore  = uin

          String subscore  = uin;

//             read (datTime – date and time of invoice issuing saved as a text in format  'dd.MM.yyyy HH:mm:ss')

          String datTime = new SimpleDateFormat( "dd.MM.yyyy HH:mm:ss" ).format( new Date() );

//             subscore = subscore  + datTime

          subscore = subscore + datTime;

//             read (nim – numeric invoice mark)

          String nim = "12345";

//             subscore  = subscore + nim

          subscore  = subscore + nim;

//             read (bsm –business space mark)

          String bsm = "blag001";

//             subscore = subscore + bsm

          subscore = subscore + bsm;

//            read (cdm – charging device mark)

          String cdm = "11245";

//             subscore = subscore + cdm

          subscore = subscore + cdm;

//             read ( tia – total invoice amount)

          String tia = "1245.56";

//             subscore = subscore + tia

          subscore = subscore + tia;

//             electronically sign the subscore using RSA-SHA1 signature

          byte[] signed  = null;

 

          try {

                    PrivateKey privatekey = null;

 

          KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

          FileInputStream fis = new FileInputStream("key_Store_Name");

          ks.load( fis, "pwd".toCharArray() );

          Key k = ks.getKey( "alias", "pwd".toCharArray());

          Signature notary = Signature.getInstance( "SHA1withRSA" );

          notary.initSign( ( PrivateKey ) privatekey );

          notary.update( subscore.getBytes() );

          signed= notary.sign();

          }

          catch ( Exception e ) {

//             Failed reading the private key

          e.printStackTrace();

          }

//             resultsPrint = calculate MD5(electronically signed subscore )

          String resultPrint = DigestUtils.md5Hex(signed);

//             end

          System.out.println( "received  32-char Security code is : " + resultPrint);

          }

 

}

Best Regards

Raja

Accepted Solutions (1)

Accepted Solutions (1)

sunil_singh13
Active Contributor
0 Kudos

Hi Raja,

I had used MD5 algorithm in one of my project. My requirement was similar to one you have but I used UDF rather going for full fledged Java Mapping. Below is sample code that I had used and concatenated few fields and provided as input. There are few online tools wherein you can test the output of MD5 (decoding).

1 import java.security.*;

02.2 import java.math.*;

03.3

04.4 public class MD5 {

05.5    public static void main(String args[]) throws Exception{

06.6       String s="This is a test";

07.7       MessageDigest m=MessageDigest.getInstance("MD5");

08.8       m.update(s.getBytes(),0,s.length());

09.9       System.out.println("MD5: "+newBigInteger(1,m.digest()).toString(16));

10.10    }

11.11 }

Answers (1)

Answers (1)

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Have you seen this example?  You can follow the same procedure either in your java mapping or UDF...

http://www.mkyong.com/java/java-md5-hashing-example/