完善开始搬家接口
parent
71fd49ead1
commit
c0ae832ace
@ -0,0 +1,8 @@
|
||||
package com.ms.api.bo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ConditionBO {
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.ms.api.tool;
|
||||
|
||||
import com.ms.api.tool.security.AES;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
public class SecurityTool {
|
||||
|
||||
public static String key = "D2E31857FB4C4C9439EDED69F5A85775";
|
||||
|
||||
/**
|
||||
* AES加密
|
||||
*/
|
||||
public static String encodeByAES(String context){
|
||||
byte[] aesByte;
|
||||
try {
|
||||
AES aes = new AES(key);
|
||||
aesByte = aes.encode(context);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
// 将ase_encode的结果base64_encode处理
|
||||
Base64.Encoder encode = Base64.getEncoder();
|
||||
return encode.encodeToString(aesByte);
|
||||
}
|
||||
|
||||
/**
|
||||
* AES解密
|
||||
*/
|
||||
public static String decodeByAES(String aesString){
|
||||
Base64.Decoder decoder = Base64.getDecoder();
|
||||
//将加密后的字符串先base64_decode处理
|
||||
byte[] aesByte = decoder.decode(aesString);
|
||||
//解密
|
||||
String desString;
|
||||
try {
|
||||
AES aes = new AES(key);
|
||||
desString = aes.decode(aesByte);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return desString;
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.ms.api.tool.security;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* AES加解密
|
||||
*
|
||||
* https://www.jianshu.com/p/13b7fd018f7c
|
||||
*/
|
||||
public class AES {
|
||||
private String key;
|
||||
|
||||
public AES(String key)
|
||||
{
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密
|
||||
*
|
||||
* @param context
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public byte[] encode(String context) throws Exception
|
||||
{
|
||||
KeyGenerator kgen = KeyGenerator.getInstance("AES");
|
||||
|
||||
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
||||
secureRandom.setSeed(key.getBytes());
|
||||
kgen.init(128, secureRandom);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
|
||||
return cipher.doFinal(context.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
*
|
||||
* @param encryptBytes
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String decode(byte[] encryptBytes) throws Exception
|
||||
{
|
||||
KeyGenerator kgen = KeyGenerator.getInstance("AES");
|
||||
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
||||
secureRandom.setSeed(key.getBytes());
|
||||
kgen.init(128, secureRandom);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES");
|
||||
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
|
||||
byte[] decryptBytes = cipher.doFinal(encryptBytes);
|
||||
return new String(decryptBytes, "UTF-8");
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.ms.test;
|
||||
|
||||
import com.ms.api.tool.SecurityTool;
|
||||
|
||||
public class SecurityTest {
|
||||
public static void main(String[] args) {
|
||||
String word = SecurityTool.encodeByAES("hello world!");
|
||||
System.out.println(word);
|
||||
|
||||
String ss = SecurityTool.decodeByAES(word);
|
||||
System.out.println(ss);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue