From: ho.namkoong Date: Fri, 31 Aug 2012 09:02:16 +0000 (+0900) Subject: [Title] Make CipherUtil for signing widget X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bc8e6fe3a32528321210caf362634828d8c4b611;p=sdk%2Fide%2Fcommon-eplugin.git [Title] Make CipherUtil for signing widget [Type] [Module] [Priority] [Jira#] [Redmine#] [Problem] [Cause] [Solution] [TestCase] Change-Id: I185d0edd45f374c581e5fdf45afdf7374ab8326f --- diff --git a/org.tizen.common/src/org/tizen/common/util/CipherUtil.java b/org.tizen.common/src/org/tizen/common/util/CipherUtil.java new file mode 100644 index 0000000..dbaec53 --- /dev/null +++ b/org.tizen.common/src/org/tizen/common/util/CipherUtil.java @@ -0,0 +1,97 @@ +/* +* Common +* +* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. +* +* Contact: +* Ho Namkoong +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* Contributors: +* - S-Core Co., Ltd +* +*/ + +package org.tizen.common.util; + +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESedeKeySpec; + +import org.tizen.common.util.log.Logger; + +import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; + +/** + * PluginUtil + * + * This is a util class for encrypting and decrypting plain texts. + * + * @author Ho Namkoong{@literal } (S-Core) + */ +public class CipherUtil { + + private static final String password = "ANYLINKhijklmnopqrstuvwx"; + private static SecretKey SECRETE_KEY; + private static Cipher DES_CIPHER; + private static final String ALGORITHM = "DESede"; + + static { + try { + byte key[] = password.getBytes(); + DESedeKeySpec desKeySpec = new DESedeKeySpec(key); + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); + SECRETE_KEY = keyFactory.generateSecret(desKeySpec); + + DES_CIPHER = Cipher.getInstance(ALGORITHM + "/ECB/PKCS5Padding"); + } catch (Throwable t) { + Logger.error("Exception occurred while creating secrete key", t); + } + } + + private static byte[] decryptByDES(byte[] bytes) throws Exception{ + DES_CIPHER.init(Cipher.DECRYPT_MODE, SECRETE_KEY); + + return DES_CIPHER.doFinal(bytes); + } + + private static byte[] encryptByDES(String s) throws Exception { + DES_CIPHER.init(Cipher.ENCRYPT_MODE, SECRETE_KEY); + + return DES_CIPHER.doFinal(s.getBytes()); + } + + /** + * Decrypt cipher message + * + * @param s cipher message which will be decrypted. + * @return decrypted plain text. + * @author Ho Namkoong {@literal } (S-Core) + */ + public static String getDecryptedString(String s) throws Exception { + return new String(decryptByDES(Base64.decode(s))); + } + + /** + * Encrypt plain text + * + * @param s plain text which will be encrypted. + * @return encrypted cipher text. + * @author Ho Namkoong {@literal } (S-Core) + */ + public static String getEncryptedString(String s) throws Exception { + return new String(Base64.encode(encryptByDES(s))); + } +} diff --git a/org.tizen.common/test/src/org/tizen/common/util/CipherUtilTest.java b/org.tizen.common/test/src/org/tizen/common/util/CipherUtilTest.java new file mode 100644 index 0000000..f518d37 --- /dev/null +++ b/org.tizen.common/test/src/org/tizen/common/util/CipherUtilTest.java @@ -0,0 +1,94 @@ +/* +* Common +* +* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. +* +* Contact: +* Ho Namkoong +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* Contributors: +* - S-Core Co., Ltd +* +*/ + +package org.tizen.common.util; + +import org.junit.Assert; +import org.junit.Test; + +/** + * CipherUtilTest + * + * Test case for {@link CipherUtil} + * + * @author Ho Namkoong{@literal } (S-Core) + * + * @see CipherUtil + */ +public class CipherUtilTest { + + /** + * Original password list which should be encrypted + */ + private static final String[] passList = {"1234", "wiehj2j3hxcgf", "0932hsdhgfasdf", "kj2h3ejaswdas"}; + + /** + * Encrypted password + */ + private String[] encryptedPassList = new String[passList.length]; + + /** + * Decrypted password + */ + private String[] decryptedPassList = new String[passList.length]; + + /** + * Test {@link CipherUtil#getEncryptedString(String)} + * + * @throws Exception in case of failure in test + * + * @see CipherUtil#getEncryptedString(String) + */ + private void test_getEncryptedBase64String() throws Exception { + for (int i = 0; i < passList.length; i++) { + encryptedPassList[i] = CipherUtil.getEncryptedString(passList[i]); + Assert.assertNotNull(encryptedPassList[i]); + } + } + + /** + * Test {@link CipherUtil#getDecryptedString(String)} + * + * @throws Exception in case of failure in test + * + * @see CipherUtil#getDecryptedString(String) + */ + private void test_getDecryptedString() throws Exception { + for(int i=0; i