Auto commit

上级 e8293a77
package com.lhstack.controller; package com.lhstack.controller;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import java.util.Optional; 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.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -13,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -13,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.lhstack.utils.Aes; import com.lhstack.utils.Aes;
import io.reactivex.Single; import io.reactivex.Single;
import io.reactivex.exceptions.CompositeException;
@RestController @RestController
@RequestMapping("crypto") @RequestMapping("crypto")
...@@ -26,14 +31,19 @@ public class CryptoController { ...@@ -26,14 +31,19 @@ public class CryptoController {
@PostMapping("decrypt") @PostMapping("decrypt")
public Single<String> decrypt(@RequestBody Map<String,String> body){ public Single<String> decrypt(@RequestBody Map<String,String> body){
return Single.defer(() -> { String aesKey = body.getOrDefault("key", defaultAesKey);
String aesKey = body.getOrDefault("key", defaultAesKey); String aesIv = body.getOrDefault("iv", defaultAesIv);
String aesIv = body.getOrDefault("iv", defaultAesIv); byte[] content = body.getOrDefault("content","").getBytes();
byte[] content = body.getOrDefault("content","").getBytes(); return Single
return Single.fromCallable(() -> { .fromCallable(() -> Aes.decrypt(aesKey, aesIv, Base64.decode(content)))
Optional<byte[]> resulOptional = Aes.decrypt(aesKey, aesIv, content); .onErrorResumeNext(e ->Single.just(Aes.decrypt(aesKey, aesIv, Hex.decode(content))))
return "hello world"; .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();
}); });
} }
......
...@@ -24,7 +24,7 @@ public class Aes { ...@@ -24,7 +24,7 @@ public class Aes {
} }
} }
public static Optional<byte[]> decrypt(String key,String iv,byte[] encryptBytes){ public static byte[] decrypt(String key,String iv,byte[] encryptBytes){
try{ try{
Cipher cipher = StringUtils.hasText(iv) ? Cipher.getInstance(CBC) : Cipher.getInstance(ECB); Cipher cipher = StringUtils.hasText(iv) ? Cipher.getInstance(CBC) : Cipher.getInstance(ECB);
if(StringUtils.hasText(iv)){ if(StringUtils.hasText(iv)){
...@@ -32,8 +32,7 @@ public class Aes { ...@@ -32,8 +32,7 @@ public class Aes {
}else { }else {
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES")); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
} }
byte[] bytes = cipher.doFinal(encryptBytes); return cipher.doFinal(encryptBytes);
return Optional.of(bytes);
}catch(Exception e){ }catch(Exception e){
throw new RuntimeException(e); throw new RuntimeException(e);
} }
......
aes:
key: axiwlazlf1456175
\ No newline at end of file
aes:
key: axiwlazlf1456175
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册