Java code for Signing and encoding of PI message payload |
---|
package com.javamapping.signing; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.security.KeyStore; import java.security.PrivateKey; import java.security.Security; import java.security.cert.CertStore; import java.security.cert.CollectionCertStoreParameters; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Enumeration; import org.apache.commons.codec.binary.Base64; import org.bouncycastle.cms.CMSProcessableByteArray; import org.bouncycastle.cms.CMSSignedData; import org.bouncycastle.cms.CMSSignedDataGenerator; import org.bouncycastle.jce.provider.BouncyCastleProvider; 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; import com.sap.aii.utilxi.core.io.IOUtil; public class SigningBouncyCastle extends AbstractTransformation { public void transform(TransformationInput input, TransformationOutput output) throws StreamTransformationException { String finalString = ""; String pass = ""; try { InputStream ins = input.getInputPayload().getInputStream(); String input_data = IOUtil.copyToString(ins, "UTF-8"); finalString = signRequest(input_data, pass); output.getOutputPayload().getOutputStream().write( finalString.getBytes()); } catch (Exception ie) { // do nothing } } private String signRequest(String strPaymentRequest, String strPassword) { X509Certificate cert = null; PrivateKey priv = null; try { // Below we are using BouncyCastle classes to sign the Request Message. Security.addProvider(new BouncyCastleProvider()); String pass = "Your Private Key Password"; File file = new File("Path of the SAP Application where Private key is stored"); InputStream stream = new FileInputStream(file); KeyStore store = KeyStore.getInstance("PKCS12"); store.load(stream, pass.toCharArray()); PrivateKey key = (PrivateKey)store.getKey("Your Private Key File name", pass.toCharArray()); Enumeration e = store.aliases(); String name = ""; if (e != null) { while (e.hasMoreElements()) { String n = (String) e.nextElement(); if (store.isKeyEntry(n)) { name = n; } } } // Get the private key and the certificate priv = key; cert = (X509Certificate) store.getCertificate(name); java.security.cert.Certificate[] certChain = store.getCertificateChain(name); ArrayList certList = new ArrayList(); CertStore certs = null; for (int i = 0; i < certChain.length; i++) certList.add(certChain[i]); certs = CertStore.getInstance("Collection",new CollectionCertStoreParameters(certList), "BC"); // Encrypt data CMSSignedDataGenerator sgen = new CMSSignedDataGenerator(); // What digest algorithm i must use? SHA1? MD5? RSA?... // In our case we are using SHA1 algorithm // CMSSignedDataGenerator.DIGEST_SHA1 = "1.3.14.3.2.26" sgen.addSigner(priv, (X509Certificate) cert,CMSSignedDataGenerator.DIGEST_SHA1); sgen.addCertificatesAndCRLs(certs); // Convert the message to UTF8 encoding byte[] utf8 = strPaymentRequest.getBytes("UTF-8"); // Initialize signer object using UTF8 encoded string, detached = // true, and //Bouncy Castle provider (BC). // The 2nd parameter need to be true (detached form) we need to // attach //original message to signed message CMSSignedData csd = sgen.generate(new CMSProcessableByteArray(utf8), true, "BC"); // Get signed message byte[] signedData = csd.getEncoded(); // Get base 64 representation of signed message byte[] signedDataB64 = Base64.encodeBase64(signedData); String str = new String(signedDataB64); //output.getOutputPayload().getOutputStream().write(str.getBytes()); // Write Base64 encoded message to file - If needed // FileWriter fw = new FileWriter("Base64Encoded.txt", false); // fw.write(signedDataB64); // fw.flush(); // fw.close(); // FileOutputStream out = new FileOutputStream("Signed.txt"); // out.write(signedData); // out.close(); return str; } catch (Exception ex) { System.out.println("Error signing payment request. Please verify the certificate."); ex.printStackTrace(); return ""; } } } |
Main Class |
---|
public static void main(String[] args) throws FileNotFoundException { try { InputStream in = new FileInputStream(new File("Your Input File Path")); //Make Sure this is the Workspace path where your Java code is running. OutputStream out = new FileOutputStream(new File("Your Output File Path")); SigningBouncyCastle bouncyCastle = new SigningBouncyCastle(); bouncyCastle.transform(in, out); } catch (Exception e) { e.printStackTrace(); } } |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
5 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |