Commit fe29ca07 by Simon Cheng

DES加解密方法

parent 245471ab
package com.yd.api.security;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yd.api.result.JsonResult;
import com.yd.api.security.vo.DESCommonVO;
import com.yd.util.deshandler.DESTypeHandler;
@RestController
@RequestMapping(value="/data/security")
public class DataSecurityController {
@RequestMapping("/dataencrypt")
public Object encrypt(@RequestBody DESCommonVO requestVO) throws Exception{
JsonResult result = new JsonResult();
String resultString = "";
try {
DESTypeHandler jpaCryptoConverter = new DESTypeHandler();
resultString = jpaCryptoConverter.encode((requestVO.getContent()));
requestVO.setEncrypted(resultString);
} catch (Exception e) {
e.printStackTrace();
}
result.setData(requestVO);
return result;
}
@RequestMapping("/datadecrypt")
public Object Decrypt(@RequestBody DESCommonVO requestVO) throws Exception{
JsonResult result = new JsonResult();
String content = "";
try {
DESTypeHandler jpaCryptoConverter = new DESTypeHandler();
content = jpaCryptoConverter.decode(requestVO.getEncrypted());
requestVO.setContent(content);
} catch (Exception e) {
e.printStackTrace();
}
result.setData(requestVO);
return result;
}
}
package com.yd.api.security.vo;
import java.util.List;
public class ConvertTable {
private String tableName;//表名
private List<String> fields;
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public List<String> getFields() {
return fields;
}
public void setFields(List<String> fields) {
this.fields = fields;
}
}
package com.yd.api.security.vo;
import java.util.List;
import com.yd.api.result.CommonResult;
import com.yd.util.page.PaginationInfo;
public class ConvertTableList {
private List<ConvertTable> convertTables;
private CommonResult commonResult;
private String dbName;//数据库名
private String operate;//1.加密(DataEncrypt)2.解密(DataDecrypt)
public String getOperate() {
return operate;
}
public void setOperate(String operate) {
this.operate = operate;
}
private PaginationInfo paginationInfo;
public PaginationInfo getPaginationInfo() {
return paginationInfo;
}
public void setPaginationInfo(PaginationInfo paginationInfo) {
this.paginationInfo = paginationInfo;
}
public List<ConvertTable> getConvertTables() {
return convertTables;
}
public void setConvertTables(List<ConvertTable> convertTables) {
this.convertTables = convertTables;
}
public CommonResult getCommonResult() {
return commonResult;
}
public void setCommonResult(CommonResult commonResult) {
this.commonResult = commonResult;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
}
package com.yd.api.security.vo;
import java.io.Serializable;
public class DESCommonVO implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String content;
private String encrypted;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getEncrypted() {
return encrypted;
}
public void setEncrypted(String encrypt) {
encrypted = encrypt;
}
}
package com.yd.api.security.vo;
public class DesDecryptRequestVO {
}
package com.yd.api.security.vo;
import com.yd.api.result.CommonResult;
public class DesDecryptResponseVO {
private String desDecryptResult;
private CommonResult commonResult;
public String getDesDecryptResult() {
return desDecryptResult;
}
public void setDesDecryptResult(String desDecryptResult) {
this.desDecryptResult = desDecryptResult;
}
public CommonResult getCommonResult() {
return commonResult;
}
public void setCommonResult(CommonResult commonResult) {
this.commonResult = commonResult;
}
}
package com.yd.api.security.vo;
public class DesEncryptRequestVO {
private String encryptString;
private String type;
public String getEncryptString() {
return encryptString;
}
public void setEncryptString(String encryptString) {
this.encryptString = encryptString;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
package com.yd.api.security.vo;
import com.yd.api.result.CommonResult;
public class DesEncryptResponseVO {
private String encryptResult;
private CommonResult commonResult;
public String getEncryptResult() {
return encryptResult;
}
public void setEncryptResult(String encryptResult) {
this.encryptResult = encryptResult;
}
public CommonResult getCommonResult() {
return commonResult;
}
public void setCommonResult(CommonResult commonResult) {
this.commonResult = commonResult;
}
}
......@@ -32,30 +32,19 @@ public class DESTypeHandler extends BaseTypeHandler<Object> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType)
throws SQLException {
sensitiveenabled = Boolean.valueOf(properties.get(secret_property_enabled_key).toString());
if (sensitiveenabled==false)
{
return;
}
if (parameter == null)
{
return;
}
sensitivekey = (String)properties.get(secret_property_key);
DESUtils des;
String result = null;
try {
des = new DESUtils(sensitivekey, "utf-8");
if (parameter != null)
{
result = des.encode((String)parameter);
}
ps.setString(i, result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (parameter != null)
{
result = encode((String)parameter);
}
ps.setString(i, result);
}
@Override
......@@ -82,14 +71,47 @@ public class DESTypeHandler extends BaseTypeHandler<Object> {
String result = decode(columnValue);
return result;
}
private String decode(String columnValue) {
/**
* 解密
* @param context
* @return
* @throws SQLException
*/
public String encode(String context) throws SQLException {
sensitiveenabled = Boolean.valueOf(properties.get(secret_property_enabled_key).toString());
if (sensitiveenabled==false)
{
return null;
}
sensitivekey = (String)properties.get(secret_property_key);
DESUtils des;
String result = null;
try {
des = new DESUtils(sensitivekey, "utf-8");
if (context != null)
{
result = des.encode((String)context);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 解密
* @param columnValue
* @return
*/
public String decode(String encrypted) {
sensitiveenabled = Boolean.valueOf(properties.get(secret_property_enabled_key).toString());
if (sensitiveenabled==false)
{
return columnValue;
return encrypted;
}
if (columnValue == null)
if (encrypted == null)
{
return null;
}
......@@ -98,14 +120,15 @@ public class DESTypeHandler extends BaseTypeHandler<Object> {
String result = null;
try {
des = new DESUtils(sensitivekey, "utf-8");
if (columnValue != null)
if (encrypted != null)
{
result = des.decode(columnValue);
result = des.decode(encrypted);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment