Applying encryption

This explains about ways to encrypt in nexacro platform.

Applying encryption using CryptoJS

Preparation

Preparation for using CryptoJS in nexacro platform

Importing js file

We can import js file using json file in nexacro platform.

crypto.json

{
 "name": "CryptoJS",
 "version": "14.0.0.1",
 "description": "",
 "license": "",
 "scripts": [ 
  "/CryptoJS/jquery-1.11.0.min.js", 
  "/CryptoJS/jquery-migrate-1.2.1.min.js",
  "/CryptoJS/core-min.js", 
  "/CryptoJS/aes.js", 
  "/CryptoJS/pad-zeropadding.js"
 ]
}

Create crypto.json using above script and then copy it into following path.

C:\Program Files (x86)\nexacro\14\nexacro14lib\component

crypto_1

Creating Crypto JS folder

Create folder to manage js files related CryptoJS.

crypto_1

You can download CryptoJS.zip file from URL below.
http://support.tobesoft.co.kr/Next_JSP/nexacro/download/etc/CryptoJS.zip

You can download latest js file related CryptoJS from Downloads menu on URL below.

https://code.google.com/p/crypto-js

crypto_1

nexacro platform preparation

Run nexacro studio and then open the project which you want to work in.

Setting Typedefinition

Set Typedefinition in opened project to use CryptoJS

crypto_4

All preparations to use CryptoJS are done after completing above steps.

CryptoJS Sample Source 1

  1. Create new form.

  2. Create new button component on form.

crypto_5

3.  Write following script in Click Event.
this.key = CryptoJS.enc.Utf8.parse("nexacro platform");
this.iv  = CryptoJS.enc.Utf8.parse(1234567812345678);
this.Button00_onclick = function(obj:Button,  e:nexacro.ClickEventInfo)
{
 var encrypted = this.dataEncode("nexacro platform 14", this.key, this.iv);
 var decrypted = this.dataDecode(encrypted, this.key, this.iv);
 
 trace(encrypted);
 trace(decrypted);

}

 //Encryption
this.dataEncode = function(message, key, iv){
   var encrypted = CryptoJS.AES.encrypt(message, key, { iv: iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding});
   return encrypted.toString();
 }
  //Decryption
this.dataDecode = function(encrypted, key, iv){
   var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,padding:CryptoJS.pad.ZeroPadding}); 
   return decrypted.toString(CryptoJS.enc.Utf8);
}

[ Expected Results ]

Encryption : i/ARuTqpPAWDxzYGEndZqnj2i7oi3PbQ6cDMqIipvcY=

Decryption : nexacro platform 14

CryptoJS Sample Source 2

This is sample using nexacro platform with JSP.
  1. Create new form.

  2. Create new button component on form.

crypto_5

3. Write following script in click event.
this.callValue = "";
this.key = CryptoJS.enc.Utf8.parse("nexacro platform");
this.iv  = CryptoJS.enc.Utf8.parse('1234567812345678');
this.Button00_onclick = function(obj:Button,  e:nexacro.ClickEventInfo)
{
 var encrypted = this.dataEncode("nexacro platform 14", this.key, this.iv);
 var decrypted = this.dataDecode(encrypted, this.key, this.iv);
 
 trace(encrypted);
 trace(decrypted);
 
 this.transaction("service","http://localhost:8080/nexacro/CryptoJS.jsp","","","encrypted="+encrypted,"fn_search_after");
 
}   
this.fn_search_after =function (a,b,c)
{
 trace("callback > dataEncode : " + this.callValue);
 trace("callback > dataDecode : " + this.dataDecode(this.callValue, this.key, this.iv));
}
   
  
 //Encryption
this.dataEncode = function(message, key, iv){
       var encrypted = CryptoJS.AES.encrypt(message, key, { iv: iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding});
       return encrypted.toString();
 }
  //Decryption
this.dataDecode = function(encrypted, key, iv){
       var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,padding:CryptoJS.pad.ZeroPadding}); 
       return decrypted.toString(CryptoJS.enc.Utf8);
}

CryptoJS.jsp

<%@page import="sun.misc.BASE64Decoder"%>
<%@page import="sun.misc.BASE64Encoder"%>
<%@page import="javax.crypto.spec.IvParameterSpec"%>
<%@page import="javax.crypto.SecretKey"%>
<%@page import="javax.crypto.spec.SecretKeySpec"%>
<%@page import="java.io.IOException"%>
<%@ page import="com.nexacro.xapi.data.*" %>
<%@ page import="com.nexacro.xapi.tx.*" %>
<%@ page import = "java.util.*" %>
<%@ page import = "javax.crypto.Cipher" %>
<%@ page import = "javax.crypto.KeyGenerator" %>
<%@ page import="org.apache.commons.logging.*" %>  
<%@ page contentType="text/xml; charset=utf-8" %>
<%!
 private static final String algorithm = "AES/CBC/NoPadding";
 private static final byte[] keyValue = "nexacro platform".getBytes();
 private static final byte[] ivValue = "1234567812345678".getBytes();
 private static final IvParameterSpec ivspec = new IvParameterSpec(ivValue);
 private static final SecretKeySpec keyspec = new SecretKeySpec(keyValue, "AES");
 final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
 public static String encrypt(String Data) throws Exception {
  Cipher c = Cipher.getInstance(algorithm);
  c.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
  byte[] encVal = c.doFinal(Data.getBytes());
  String encryptedValue = new BASE64Encoder().encode(encVal);
  return encryptedValue;
 }
 public static String decrypt(String encryptedData) throws Exception {
  Cipher c = Cipher.getInstance(algorithm);
  c.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
  byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
  byte[] decValue = c.doFinal(decordedValue);
  String decryptedValue = new String(decValue);
  return decryptedValue;
 }
 public static String bytesToHex(byte[] bytes) {
  char[] hexChars = new char[bytes.length * 2];
  int v;
  for (int j = 0; j < bytes.length; j++) {
   v = bytes[j] & 0xFF;
   hexChars[j * 2] = hexArray[v >>> 4];
   hexChars[j * 2 + 1] = hexArray[v & 0x0F];
  }
  return new String(hexChars);
 }
 private static String padString(String source) {
  char paddingChar = ' ';
  int size = 16;
  int x = source.length() % size;
  int padLength = size - x;
  for (int i = 0; i < padLength; i++) {
   source += paddingChar;
  }
  return source;
 }
%>
<%
 
 HttpPlatformRequest platformRequest = new HttpPlatformRequest(request);
 platformRequest.receiveData();
 
 
  PlatformData o_xpData = platformRequest.getData();
  Variable ss = o_xpData.getVariable("encrypted");
  String value = ss.getString();
   
    String passwordDec = decrypt(value);
     
    System.out.println("Passed value : " + value);
    System.out.println("Decrypted value : " + passwordDec);
 
  String password = "nexacro platform 14";
   String passwordEnc = encrypt(padString(password));
 
 VariableList varList = o_xpData.getVariableList();
 
 varList.add("ErrorCode", "0");
 varList.add("ErrorMsg", "SUCC");
 varList.add("callValue", passwordEnc);
 HttpPlatformResponse pRes = new HttpPlatformResponse(response, PlatformType.CONTENT_TYPE_XML, "euc-kr");
 pRes.setData(o_xpData);
 pRes.sendData();
 
 
%>

You can download JSP file from URL below.

To download JSP file to your computer, click the [save target as] after clicking mouse right button on URL below.

http://support.tobesoft.co.kr/Next_JSP/nexacro/download/etc/CryptoJS.jsp

[ Expected Results ]

nexacro Encryption : i/ARuTqpPAWDxzYGEndZqnj2i7oi3PbQ6cDMqIipvcY=

nexacro Decryption : uxs (13412): UD 15:3:29:958 nexacro platform 14

JSP > dataEncode : i/ARuTqpPAWDxzYGEndZqlPrt8o7+KQu10CRxd9HvZg=

Decryption result in nexacro about encoding content in JSP : dataDecode : nexacro platform 14