Java tutorial
/* * <pre> * Copyright (c) 2014 Samsung SDS. * All right reserved. * * This software is the confidential and proprietary information of Samsung * SDS. You shall not disclose such Confidential Information and * shall use it only in accordance with the terms of the license agreement * you entered into with Samsung SDS. * * Author : Takkies * Date : 2014. 04. 01. * Description : * </pre> */ package com.sds.acube.ndisc.xnapi; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import org.apache.commons.codec.binary.Base64; /** * XNApi (DES3 ?) * * @author Takkies * */ public class XNApiDesCipher { /** * * * @param data ? * @return ? */ public static String decrypt(String data) { try { Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, getKey()); byte d[] = cipher.doFinal(Base64.decodeBase64(data.getBytes())); return new String(d); } catch (Exception e) { e.printStackTrace(); } return null; } /** * * * @param data ? * @return ? */ public static String encrypt(String data) { try { Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, getKey()); byte e[] = cipher.doFinal(data.getBytes()); return Base64.encodeBase64String(e); } catch (Exception e) { e.printStackTrace(); } return null; } /** * ?<br> * 3DES ?? 24bit KEY ?<br> * DES ?? 16bit KEY ?.<br> * * @return */ private static Key getKey() { String key = "x0134-ad17s658601j56-q75k2we0des-key".substring(0, 24); // ?? . try { DESedeKeySpec desKeySpec = new DESedeKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); return keyFactory.generateSecret(desKeySpec); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String args[]) { String data = "sds000"; String enc = encrypt(data); System.out.println("encrypt : " + enc); String dec = decrypt("7eau4E0t1U0="); System.out.println("decrypt : " + dec); } }