Auto commit

上级 e8293a77
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<String> decrypt(@RequestBody Map<String,String> 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<byte[]> 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();
});
}
......
......@@ -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{
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);
}
......
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.
先完成此消息的编辑!
想要评论请 注册