넥사크로플랫폼 사용시 참고할 수 있는 암호화 방법에 대해 알아봅니다.
CryptoJS를 이용한 암호화 적용
준비단계
넥사크로플랫폼에서 CryptoJS를 사용하기 위한 준비단계
js 파일을 import
넥사크로플랫폼은 json 파일를 이용하여 js 파일을 import할 수 있습니다.
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"
]
}해당 파일 crypto.json 파일을 작성하여 아래의 폴더 위치에 복사합니다.
C:\Program Files (x86)\nexacro\14\nexacro14lib\component
crypto_1
CryptoJS 폴더 생성
CryptoJS관련된 js파일을 관리하기 위한 폴더를 생성한다.
crypto_2
http://support.tobesoft.co.kr/Next_JSP/nexacro/download/etc/CryptoJS.zip 에서 받을 수 있습니다.
CryptoJS 관련된 최신 js파일은 아래주소의 DownLoad에서 받을 수 있습니다.
https://code.google.com/p/crypto-js
crypto_3
넥사크로플랫폼 준비단계
nexacro studio를 실행하여 작업하고자 하는 프로젝트를 Open합니다.
Typedefinition 설정
Open된 프로젝트에 CryptoJS를 사용할 수 있도록 Typedefinition을 설정합니다.
crypto_4
위의 단계까지 작업을 마무리하면 CryptoJS를 사용할 수 있는 모든 준비과정이 끝납니다.
CryptoJS 샘플구현 1
새로운 폼을 하나 생성합니다.
버튼 컴포넌트를 생성합니다.
crypto_5
3. 클릭 이벤트에 아래의 스크립트 내용을 작성합니다.
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.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();
}
//복호화
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);
}[ 예상결과 ]
인코딩 : i/ARuTqpPAWDxzYGEndZqnj2i7oi3PbQ6cDMqIipvcY=
디코딩 : nexacro platform 14
CryptoJS 샘플구현 2
넥사크로플랫폼과, JSP(서비스) 를 이용한 샘플입니다.
새로운 폼을 하나 생성합니다.
버튼 컴포넌트를 생성합니다.
crypto_5
3. 클릭 이벤트에 아래의 스크립트 내용을 작성합니다.
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));
}
//암호화
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();
}
//복호화
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("넘어온값 : " + value);
System.out.println("암호 해제된값 : " + 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();
%>아래 링크를 통해 JSP파일을 다운로드 받을 수 있습니다.
링크주소에서 마우스 오른쪽을 클릭하여 [다른이름으로 대상 저장]를 통해 PC에 다운로드 가능합니다.
http://support.tobesoft.co.kr/Next_JSP/nexacro/download/etc/CryptoJS.jsp
[ 예상결과 ]
넥사크로 인코딩 : i/ARuTqpPAWDxzYGEndZqnj2i7oi3PbQ6cDMqIipvcY=
넥사크로 디코딩 : uxs (13412): UD 15:3:29:958 nexacro platform 14
JSP 서비스 > dataEncode : i/ARuTqpPAWDxzYGEndZqlPrt8o7+KQu10CRxd9HvZg=
JSP 서비스에서 인코딩 내용을 넥사크로에서 디코딩 결과 : dataDecode : nexacro platform 14