Hi Folks,
I have a requirement to generate security code which has the algorithmfor the calculation ofsecuritycode canbe summarizedas follows:
MD5hash(Electronic signature by private key(Unique invoice number + date +timeof issue +numerical code of invoice+business space+ charging devices +totalinvoice amount))
A decimal separator inthetotal amountof datais necessary to usea point and nota comma (eg 1245.56).
Using theMD5cryptographichashfunction (in standard RFC1321The MD5Message-Digest Algorithm) getsthe result: 32-digit number written inhexadecimal format(numbers andlowercase:0-9,a-f), which is printed on thebill.
Exampleof protectivecode: a9e6b1488f0cc775f0c82aa7a1372e53
Electronic signaturesis performedusingRSA-SHA1 electronic signaturewith the relevantcertificate that is assigned toa 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