You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@streampark.apache.org by GitBox <gi...@apache.org> on 2023/01/08 04:43:08 UTC

[GitHub] [incubator-streampark] 1996fanrui commented on a diff in pull request #2240: [Improve] maven pom cleanup for apache-release

1996fanrui commented on code in PR #2240:
URL: https://github.com/apache/incubator-streampark/pull/2240#discussion_r1064081601


##########
streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/EncryptUtils.java:
##########
@@ -18,85 +18,73 @@
 package org.apache.streampark.console.base.util;
 
 import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.spec.SecretKeySpec;
 
-import java.security.Key;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
 
 public class EncryptUtils {
 
-  private static final String DEFAULT_KEY = "defaultKey";
-  private Cipher encryptCipher = null;
-  private Cipher decryptCipher = null;
+  private static final String CIPHER_KEY = "AES";
 
-  private static String byteArr2HexStr(byte[] arrB) {
-    int iLen = arrB.length;
-    StringBuilder sb = new StringBuilder(iLen * 2);
-    for (byte anArrB : arrB) {
-      int intTmp = anArrB;
-      while (intTmp < 0) {
-        intTmp = intTmp + 256;
-      }
-      if (intTmp < 16) {
-        sb.append("0");
-      }
-      sb.append(Integer.toString(intTmp, 16));
-    }
-    return sb.toString();
-  }
+  private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
 
-  private static byte[] hexStr2ByteArr(String strIn) {
-    byte[] arrB = strIn.getBytes();
-    int iLen = arrB.length;
+  private static final String DEFAULT_KEY = "ApacheStreamPark";
 
-    byte[] arrOut = new byte[iLen / 2];
-    for (int i = 0; i < iLen; i = i + 2) {
-      String strTmp = new String(arrB, i, 2);
-      arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
-    }
-    return arrOut;
+  public static String base64Encode(byte[] bytes) {
+    return Base64.getEncoder().encodeToString(bytes);
   }
 
-  public EncryptUtils() throws Exception {
-    this(DEFAULT_KEY);
+  public static byte[] base64Decode(String base64Code) {
+    return Base64.getDecoder().decode(base64Code);
   }
 
-  EncryptUtils(String strKey) throws Exception {
-    /*
-     * Security.addProvider(new com.sun.crypto.provider.SunJCE());
-     */
-    Key key = getKey(strKey.getBytes());
-
-    encryptCipher = Cipher.getInstance("DES");
-    encryptCipher.init(Cipher.ENCRYPT_MODE, key);
-
-    decryptCipher = Cipher.getInstance("DES");
-    decryptCipher.init(Cipher.DECRYPT_MODE, key);
+  public static String encrypt(String content) throws Exception {
+    return encrypt(content, DEFAULT_KEY);
   }
 
-  private byte[] encrypt(byte[] arrB) throws Exception {
-    return encryptCipher.doFinal(arrB);
+  public static String encrypt(String content, String key) throws Exception {
+    String encryptKey = checkGetKey(key);
+    KeyGenerator keyGenerator = KeyGenerator.getInstance(CIPHER_KEY);
+    keyGenerator.init(128);
+    Cipher cipher = Cipher.getInstance(ALGORITHM);
+    cipher.init(
+        Cipher.ENCRYPT_MODE,
+        new SecretKeySpec(encryptKey.getBytes(StandardCharsets.UTF_8), CIPHER_KEY));
+    byte[] bytes = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
+    return base64Encode(bytes);
   }
 
-  String encrypt(String strIn) throws Exception {
-    return byteArr2HexStr(encrypt(strIn.getBytes()));
+  public static String decrypt(String content) throws Exception {
+    return decrypt(content, DEFAULT_KEY);
   }
 
-  private byte[] decrypt(byte[] arrB) throws Exception {
-    return decryptCipher.doFinal(arrB);
+  public static String decrypt(String content, String key) throws Exception {
+    String decryptKey = checkGetKey(key);
+    KeyGenerator keyGenerator = KeyGenerator.getInstance(CIPHER_KEY);
+    keyGenerator.init(128);
+    Cipher cipher = Cipher.getInstance(ALGORITHM);

Review Comment:
   Hi @wolfboys , thanks for your contribution.
   
   I see these changes check failure, and they shouldn't be included in this PR, could you just refactor maven related pom and CI in this PR?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@streampark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org