You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/06/29 05:09:03 UTC

[shardingsphere] branch master updated: Update AESEncryptAlgorithm.java (#10830)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 9845d1b  Update AESEncryptAlgorithm.java (#10830)
9845d1b is described below

commit 9845d1b96a3c2d493af48d88518ed4e1cd7043da
Author: nanzheng <nz...@163.com>
AuthorDate: Tue Jun 29 13:08:36 2021 +0800

    Update AESEncryptAlgorithm.java (#10830)
    
    * Update AESEncryptAlgorithm.java
    
    improvement base64 encode decode performance. Totally increase almost over 200%
    
    add trycatch
---
 .../encrypt/algorithm/AESEncryptAlgorithm.java             | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithm.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithm.java
index 0f8e053..8fdd881 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithm.java
@@ -21,11 +21,8 @@ import com.google.common.base.Preconditions;
 import lombok.Getter;
 import lombok.Setter;
 import lombok.SneakyThrows;
-import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.codec.binary.StringUtils;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
-
 import javax.crypto.Cipher;
 import javax.crypto.NoSuchPaddingException;
 import javax.crypto.spec.SecretKeySpec;
@@ -35,7 +32,7 @@ import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
 import java.util.Properties;
-
+import javax.xml.bind.DatatypeConverter;
 /**
  * AES encrypt algorithm.
  */
@@ -59,14 +56,15 @@ public final class AESEncryptAlgorithm implements EncryptAlgorithm {
         return Arrays.copyOf(DigestUtils.sha1(props.getProperty(AES_KEY)), 16);
     }
     
-    @SneakyThrows(GeneralSecurityException.class)
+    @SneakyThrows({GeneralSecurityException.class, UnsupportedEncodingException.class})
     @Override
     public String encrypt(final Object plaintext) {
         if (null == plaintext) {
             return null;
         }
-        byte[] result = getCipher(Cipher.ENCRYPT_MODE).doFinal(StringUtils.getBytesUtf8(String.valueOf(plaintext)));
-        return Base64.encodeBase64String(result);
+        byte[] result = new byte[0];
+        result = getCipher(Cipher.ENCRYPT_MODE).doFinal(String.valueOf(plaintext).getBytes("UTF-8"));
+        return DatatypeConverter.printBase64Binary(result);
     }
     
     @SneakyThrows(GeneralSecurityException.class)
@@ -75,7 +73,7 @@ public final class AESEncryptAlgorithm implements EncryptAlgorithm {
         if (null == ciphertext) {
             return null;
         }
-        byte[] result = getCipher(Cipher.DECRYPT_MODE).doFinal(Base64.decodeBase64(ciphertext));
+        byte[] result = getCipher(Cipher.DECRYPT_MODE).doFinal(DatatypeConverter.parseBase64Binary(ciphertext));
         return new String(result, StandardCharsets.UTF_8);
     }