diff --git a/src/main/java/com/lhstack/controller/CryptoController.java b/src/main/java/com/lhstack/controller/CryptoController.java index 1cfbb56b8b297dcbee98975ed7b10c2c10be9908..9ebe0d885d7e1ddb53d7ebed98f6578f659f3898 100644 --- a/src/main/java/com/lhstack/controller/CryptoController.java +++ b/src/main/java/com/lhstack/controller/CryptoController.java @@ -1,8 +1,12 @@ package com.lhstack.controller; +import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; +import org.bouncycastle.util.encoders.Base64; +import org.bouncycastle.util.encoders.Hex; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -13,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController; import com.lhstack.utils.Aes; import io.reactivex.Single; +import io.reactivex.exceptions.CompositeException; @RestController @RequestMapping("crypto") @@ -26,14 +31,19 @@ public class CryptoController { @PostMapping("decrypt") public Single decrypt(@RequestBody Map body){ - return Single.defer(() -> { - String aesKey = body.getOrDefault("key", defaultAesKey); - String aesIv = body.getOrDefault("iv", defaultAesIv); - byte[] content = body.getOrDefault("content","").getBytes(); - return Single.fromCallable(() -> { - Optional resulOptional = Aes.decrypt(aesKey, aesIv, content); - return "hello world"; - }); + String aesKey = body.getOrDefault("key", defaultAesKey); + String aesIv = body.getOrDefault("iv", defaultAesIv); + byte[] content = body.getOrDefault("content","").getBytes(); + return Single + .fromCallable(() -> Aes.decrypt(aesKey, aesIv, Base64.decode(content))) + .onErrorResumeNext(e ->Single.just(Aes.decrypt(aesKey, aesIv, Hex.decode(content)))) + .map(bytes -> new String(bytes,StandardCharsets.UTF_8)) + .onErrorReturn(e -> { + if(e instanceof CompositeException){ + CompositeException compositeException = (CompositeException)e; + return compositeException.getExceptions().stream().map(item -> String.format("%s=%s",item.getClass().getSimpleName(),item.getMessage())).collect(Collectors.joining(",")); + } + return e.getMessage(); }); } diff --git a/src/main/java/com/lhstack/utils/Aes.java b/src/main/java/com/lhstack/utils/Aes.java index 2a15427775856b5998aa2cd4173e9e0c654f579c..931c35a5bfb92828153bf45b586ef41a7c1a9c21 100644 --- a/src/main/java/com/lhstack/utils/Aes.java +++ b/src/main/java/com/lhstack/utils/Aes.java @@ -24,7 +24,7 @@ public class Aes { } } - public static Optional decrypt(String key,String iv,byte[] encryptBytes){ + public static byte[] decrypt(String key,String iv,byte[] encryptBytes){ try{ Cipher cipher = StringUtils.hasText(iv) ? Cipher.getInstance(CBC) : Cipher.getInstance(ECB); if(StringUtils.hasText(iv)){ @@ -32,8 +32,7 @@ public class Aes { }else { cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES")); } - byte[] bytes = cipher.doFinal(encryptBytes); - return Optional.of(bytes); + return cipher.doFinal(encryptBytes); }catch(Exception e){ throw new RuntimeException(e); } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9e5e1a62f62fb01eefe4a435e632b74f3fbf352f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -0,0 +1,2 @@ +aes: + key: axiwlazlf1456175 \ No newline at end of file diff --git a/target/classes/application.yml b/target/classes/application.yml index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9e5e1a62f62fb01eefe4a435e632b74f3fbf352f 100644 --- a/target/classes/application.yml +++ b/target/classes/application.yml @@ -0,0 +1,2 @@ +aes: + key: axiwlazlf1456175 \ No newline at end of file diff --git a/target/classes/com/lhstack/controller/CryptoController.class b/target/classes/com/lhstack/controller/CryptoController.class index 67a585432b330ec90814d3b65b2c6683c107344d..be5059801947bf6b3968159336a48454e8a7fa29 100644 Binary files a/target/classes/com/lhstack/controller/CryptoController.class and b/target/classes/com/lhstack/controller/CryptoController.class differ diff --git a/target/classes/com/lhstack/utils/Aes.class b/target/classes/com/lhstack/utils/Aes.class index 7e5ebd2a1e9050f62db5ba48c5ea334a965afc37..ba0dd662b1530603f7bf4f93d68e62bc9e9853f7 100644 Binary files a/target/classes/com/lhstack/utils/Aes.class and b/target/classes/com/lhstack/utils/Aes.class differ