Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ImportExportUtil.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ImportExportUtil.java,v diff -u -r1.1 -r1.2 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ImportExportUtil.java 3 Aug 2006 00:06:27 -0000 1.1 +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ImportExportUtil.java 6 Aug 2006 06:08:52 -0000 1.2 @@ -24,17 +24,37 @@ /* $Id$ */ package org.lamsfoundation.lams.learningdesign.service; +import java.security.GeneralSecurityException; +import java.security.Key; +import java.security.spec.AlgorithmParameterSpec; import java.util.Calendar; import java.util.List; +import javax.crypto.Cipher; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; +import javax.crypto.spec.IvParameterSpec; + +import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; import org.lamsfoundation.lams.learningdesign.LearningDesign; import org.lamsfoundation.lams.learningdesign.dao.ILearningDesignDAO; import org.lamsfoundation.lams.usermanagement.WorkspaceFolder; /** Routines shared by the 2.0 import/export and 1.0.2 import. */ public class ImportExportUtil { + + protected static Logger log = Logger.getLogger(ImportExportUtil.class); + + // encryption / descryption varibles + private final static String ENC_CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding"; + private final static String ENCODED_KEY_BASE64 = "emEjkQfjGZs="; + private final static String KEY_IV_BASE64 = "AToFlf/Ue40="; + private static Key secretKey = null; + private static AlgorithmParameterSpec algorithmParameterSpec = null; + /** * If the learning design has duplicated name in same folder, then rename it with timestamp. * the new name format will be oldname_ddMMYYYY_idx. The idx will be auto incremental index number, start from 1. @@ -78,4 +98,63 @@ return newTitle; } + + private static synchronized void initialiseDecryption() throws GeneralSecurityException { + if ( secretKey == null ) { + byte[] encodedKey = + Base64.decodeBase64(ENCODED_KEY_BASE64.getBytes()); + byte[] keyIv = Base64.decodeBase64(KEY_IV_BASE64.getBytes()); + + DESKeySpec desKeySpec = new DESKeySpec(encodedKey); + SecretKeyFactory factory = SecretKeyFactory.getInstance("DES"); + secretKey = factory.generateSecret(desKeySpec); // class SecretKey + algorithmParameterSpec = new IvParameterSpec(keyIv); + } + } + + /** + * Method decryptDesign. + * @param fileContent + * @return String + */ + public static String decryptImport(String fileContent) throws GeneralSecurityException { + + initialiseDecryption(); + + int start = fileContent.indexOf("") + 13; + int end = fileContent.indexOf(""); + String cipherValue = fileContent.substring(start, end); + log.debug(" cipherValue= " + cipherValue); + + byte[] encryptedBytes = Base64.decodeBase64(cipherValue.getBytes()); + + String decrypted = getDecrypted(encryptedBytes); + log.debug(" cipherValue= " + decrypted); + + return decrypted; + } + + + /** + * Method getDecripted. + * @param cipherValue + * @return String + */ + private static String getDecrypted(byte[] encryptedBytes) throws SecurityException + { + try + { + Cipher cipher = Cipher.getInstance(ENC_CIPHER_ALGORITHM); + cipher.init(Cipher.DECRYPT_MODE, secretKey, algorithmParameterSpec); + + byte[] decryptedBytes = cipher.doFinal(encryptedBytes); + return new String(decryptedBytes); + } + + catch (Exception e) + { + log.error(e); + throw new SecurityException(e.getMessage()); + } + } } Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/LD102Importer.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/LD102Importer.java,v diff -u -r1.4 -r1.5 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/LD102Importer.java 6 Aug 2006 04:24:37 -0000 1.4 +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/LD102Importer.java 6 Aug 2006 06:08:52 -0000 1.5 @@ -24,6 +24,7 @@ /* $Id$ */ package org.lamsfoundation.lams.learningdesign.service; +import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -351,6 +352,17 @@ throw new ImportToolContentException("Invalid packet format - contains nulls. See log for details."); } + if ( ! ldWddxPacket.startsWith("