Skip to Content
1
Apr 02, 2019 at 01:50 PM

Add dynamic HTTP headers in REST adapter, but first encrypt the header using Rijndael algo

4935 Views Last edit Apr 02, 2019 at 01:51 PM 2 rev

Hello Experts,

I need to encrypt a string using Rijndael algorithm and then supply it to HTTP headers in REST receiver adapter.

I am trying to right the below UDF to build and encrypt the string, but facing issues in getting the right output.

//	DynamicConfiguration conf = (DynamicConfiguration) 
	//	container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
		
		String finalString = ""; // String that needs to be signed
		String RequestIp = "182.18.180.253";
		String SessionId = "";
		String UserAgent = "";
		String UserName = "";
		long ReqDtTicks = -999;
		String AppId = "";
		String AuthToken="Finance-2-APTSGOIR";
		String AppVersion = ""; 
		String cd ="";
		String IP = "";
		String sKey = "*************";
		String finalOutput = "";
			
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		
		String currentDate = dateFormat.format(date);
		
		try {
			 cd = URLEncoder.encode(currentDate,"UTF-8");
		} catch (UnsupportedEncodingException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		try {
			 IP = URLEncoder.encode(RequestIp, "UTF-8");
		} catch (UnsupportedEncodingException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		finalString ="RequestDt="+cd+"&"+"RequestIp="+IP+"&"+"SessionId="+SessionId+"&"+"UserAgent="+UserAgent+"&"+
				   "UserName="+UserName+"&"+"ReqDtTicks="+ReqDtTicks+"&"+"AppId="+AppId+"&"+"AuthToken="+AuthToken+"&"+
				   "AppVersion="+AppVersion;
				
		
			
		byte[] keyBytes = new byte[16];
		byte[] iv = keyBytes ; //Ditto
		byte[] plaintext;
	
		 
		try {
			byte[] sessionKey = sKey.getBytes("UTF-8"); 
			plaintext = finalString.getBytes();
			Cipher cipher;
			
		//You can use ENCRYPT_MODE
		cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv));
		byte[] ciphertext = cipher.doFinal(plaintext);
		finalOutput = Base64.getEncoder().encodeToString(ciphertext);
		} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
//		 DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/REST","XHeaderName"); 
//		conf.put(key, finalOutput);
	}


request-header.txt

Have attached the equivalent .net code for the same. Need your expert advices.

Not a expert in JAVA, so please ignore the code faults.

Regards,

Sushant

Attachments