联系人手机号加密新增

master
徐杰盟 2024-06-13 15:32:05 +08:00
parent 1e0b174c74
commit dbd3630914
4 changed files with 94 additions and 1 deletions

View File

@ -39,3 +39,8 @@ alter table "FXKH_TXL"."ADDRESS_BOOK" add column("IS_PASS" TINYINT default (FALS
comment on column "FXKH_TXL"."ADDRESS_BOOK"."IS_PASS" is '是否放行, 放行:true,拦截:false';
# 2024年6月13日15:18:35
alter table "FXKH_TXL"."ADDRESS_BOOK_OLD" add column("SALT" VARCHAR(50));
comment on column "FXKH_TXL"."ADDRESS_BOOK_OLD"."SALT" is '';

View File

@ -74,6 +74,9 @@ public class AddressBookOldController {
}
List<AddressBookOld> data = query.list();
data = data.stream().map(AddressBookOld::decryptPhone).collect(Collectors.toList());
return ResultJson.ok(data);
}
@ -137,7 +140,8 @@ public class AddressBookOldController {
// 新增创建时间
Date date = new Date();
appends = appends.stream().map(vo -> vo.setCreateTime(date))
appends = appends.stream().map(vo -> vo.setCreateTime(date).encryptPhone())
.collect(Collectors.toList());
if (!service.saveBatch(appends)) {

View File

@ -7,16 +7,22 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.whdc.exception.MyException;
import com.whdc.model.group.Update;
import com.whdc.utils.SymmetricEncryption;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import static com.whdc.utils.SymmetricEncryption.decrypt;
/**
* Description:
@ -32,6 +38,7 @@ import java.util.Date;
@TableName("FXKH_TXL.ADDRESS_BOOK_OLD")
public class AddressBookOld extends Model<AddressBookOld> implements Serializable {
@TableId(value = "ID",type = IdType.AUTO)
@ApiModelProperty(value = "id")
@NotNull(message = "id不能为空" , groups = {Update.class})
@ -62,10 +69,34 @@ public class AddressBookOld extends Model<AddressBookOld> implements Serializabl
@ApiModelProperty(value = "手机号")
private String phone;
@TableField("SAIT")
@ApiModelProperty(value = "盐")
private String salt;
@Excel(name = "创建时间")
@TableField("CREATE_TIME")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty(value = "创建时间")
private Date createTime;
public AddressBookOld encryptPhone(){
if (StringUtils.isBlank(this.salt)){
this.salt = UUID.randomUUID().toString().replaceAll("-", "");
}
try {
this.phone = decrypt(this.phone,this.salt);
} catch (Exception e) {
throw new MyException("加密失败",e);
}
return this;
}
public AddressBookOld decryptPhone(){
try {
this.phone = SymmetricEncryption.encrypt(this.phone,this.salt);
}catch (Exception e){
throw new MyException("解密失败",e);
}
return this;
}
}

View File

@ -0,0 +1,53 @@
package com.whdc.utils;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
/**
* Description:
* Created by XuSan on 2024/6/13.
*
* @author XuSan
* @version 1.0
*/
public class SymmetricEncryption {
private static final String ALGORITHM = "AES";
private static final int KEY_SIZE = 128;
public static void main(String[] args) throws Exception {
String data = "Hello World!";
String encryptedData = encrypt(data, "password");
String decryptedData = decrypt(encryptedData, "password");
System.out.println("Original Data: " + data);
System.out.println("Encrypted Data: " + encryptedData);
System.out.println("Decrypted Data: " + decryptedData);
}
public static String encrypt(String data, String password) throws Exception {
SecretKey key = generateKey(password);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedData = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedData);
}
public static String decrypt(String encryptedData, String password) throws Exception {
SecretKey key = generateKey(password);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
return new String(decryptedData);
}
private static SecretKey generateKey(String password) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(KEY_SIZE, new SecureRandom(password.getBytes()));
return new SecretKeySpec(keyGenerator.generateKey().getEncoded(), ALGORITHM);
}
}