This is an automated email from the ASF dual-hosted git repository.

shaofengshi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kylin.git

commit 0884c2759dbcf88f11fe089fbe9a8b57d9c9b5df
Author: shaofengshi <shaofeng...@apache.org>
AuthorDate: Tue Jun 9 21:54:40 2020 +0800

    KYLIN-4478 fix unit test
---
 .../org/apache/kylin/common/util/EncryptUtil.java  | 24 ++++++++++++++++------
 .../apache/kylin/common/util/EncryptUtilTest.java  | 16 +++++++++++++--
 2 files changed, 32 insertions(+), 8 deletions(-)

diff --git 
a/core-common/src/main/java/org/apache/kylin/common/util/EncryptUtil.java 
b/core-common/src/main/java/org/apache/kylin/common/util/EncryptUtil.java
index d5b486a..b9b4ddb 100644
--- a/core-common/src/main/java/org/apache/kylin/common/util/EncryptUtil.java
+++ b/core-common/src/main/java/org/apache/kylin/common/util/EncryptUtil.java
@@ -18,10 +18,17 @@
 
 package org.apache.kylin.common.util;
 
+import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
 import org.apache.commons.codec.binary.Base64;
 
 import javax.crypto.Cipher;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
 
 public class EncryptUtil {
@@ -31,14 +38,21 @@ public class EncryptUtil {
     private static byte[] key = { 0x74, 0x68, 0x69, 0x73, 0x49, 0x73, 0x41, 
0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b,
             0x65, 0x79 };
 
+    private static final Cipher getCipher(int cipherMode) throws 
InvalidAlgorithmParameterException,
+            InvalidKeyException, NoSuchPaddingException, 
NoSuchAlgorithmException, UnsupportedEncodingException {
+        Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");
+        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
+        IvParameterSpec ivSpec = new 
IvParameterSpec("AAAAAAAAAAAAAAAA".getBytes("UTF-8"));
+        cipher.init(cipherMode, secretKey, ivSpec);
+        return cipher;
+    }
+
     public static String encrypt(String strToEncrypt) {
         if (strToEncrypt == null) {
             return null;
         }
         try {
-            Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");
-            final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
-            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
+            Cipher cipher = getCipher(Cipher.ENCRYPT_MODE);
             final String encryptedString = 
Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes(
                 StandardCharsets.UTF_8)));
             return encryptedString;
@@ -52,9 +66,7 @@ public class EncryptUtil {
             return null;
         }
         try {
-            Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");
-            final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
-            cipher.init(Cipher.DECRYPT_MODE, secretKey);
+            Cipher cipher = getCipher(Cipher.DECRYPT_MODE);
             final String decryptedString = new 
String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)), 
StandardCharsets.UTF_8);
             return decryptedString;
         } catch (Exception e) {
diff --git 
a/core-common/src/test/java/org/apache/kylin/common/util/EncryptUtilTest.java 
b/core-common/src/test/java/org/apache/kylin/common/util/EncryptUtilTest.java
index f559229..da985ca 100644
--- 
a/core-common/src/test/java/org/apache/kylin/common/util/EncryptUtilTest.java
+++ 
b/core-common/src/test/java/org/apache/kylin/common/util/EncryptUtilTest.java
@@ -26,8 +26,20 @@ public class EncryptUtilTest {
     @Test
     public void testAESEncrypt(){
         String input = "hello world";
-        String result = EncryptUtil.encrypt(input);
-        Assert.assertEquals("4stv/RRleOtvie/8SLHmXA==", result);
+        String result1 = EncryptUtil.encrypt(input);
+        String decrypt1 = EncryptUtil.decrypt(result1);
+        Assert.assertEquals(input, decrypt1);
+        String result2 = EncryptUtil.encrypt(input);
+        Assert.assertEquals(result2, result1);
+
+        input = "this is a long string #&$";
+        result1 = EncryptUtil.encrypt(input);
+        decrypt1 = EncryptUtil.decrypt(result1);
+        Assert.assertEquals(input, decrypt1);
+        result2 = EncryptUtil.encrypt(input);
+        Assert.assertEquals(result2, result1);
+
+
     }
 
     @Test

Reply via email to