加密工具

This commit is contained in:
Administrator 2020-09-18 17:31:04 +08:00
parent 548a2a2df6
commit 3410ac9ea8
5 changed files with 168 additions and 18 deletions

View File

@ -3,12 +3,23 @@ package com.ruoyi.business.ajax;
import com.ruoyi.business.service.IBizMemberService; import com.ruoyi.business.service.IBizMemberService;
import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.security.Md5Utils;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import sun.security.provider.MD5;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
/** /**
* 前端用户登录 * 前端用户登录

View File

@ -1,7 +1,10 @@
package com.ruoyi.business.controller; package com.ruoyi.business.controller;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import com.ruoyi.business.utils.Encrypt;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -114,6 +117,21 @@ public class BizMemberController extends BaseController
return toAjax(bizMemberService.updateBizMemberAndDou(bizMember)); return toAjax(bizMemberService.updateBizMemberAndDou(bizMember));
} }
/**
* 查看会员密码
*/
@RequiresPermissions("business:member:edit")
@Log(title = "会员密码", businessType = BusinessType.UPDATE)
@PostMapping("/showPassword")
@ResponseBody
public AjaxResult showPassword(Long memberID)
{
BizMember bizMember = bizMemberService.selectBizMemberSimple(memberID);
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("pwd", Encrypt.decrypt(bizMember.getPassword()));
return AjaxResult.success(resultMap);
}
/** /**
* 修改会员密码 * 修改会员密码
*/ */
@ -125,7 +143,8 @@ public class BizMemberController extends BaseController
{ {
BizMember bizMember = bizMemberService.selectBizMemberSimple(memberID); BizMember bizMember = bizMemberService.selectBizMemberSimple(memberID);
if(bizMember == null || StringUtils.isEmpty(password)) return toAjax(0); if(bizMember == null || StringUtils.isEmpty(password)) return toAjax(0);
bizMember.setPassword(password); //加密
bizMember.setPassword(Encrypt.encrypt(password));
return toAjax(bizMemberService.updateBizMember(bizMember)); return toAjax(bizMemberService.updateBizMember(bizMember));
} }

View File

@ -0,0 +1,110 @@
package com.ruoyi.business.utils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.UnsupportedEncodingException;
/* DES加密工具 */
public class Encrypt {
//秘钥
private static final String KEY = "ZF_desencrypt_2020";
//加密解密工具
private static Cipher enCipher = null;
private static Cipher deCipher = null;
//初始化加密工具
static {
try {
int keyLen = 8;
String key8 = Md5Utils.hash(KEY).substring(0, keyLen).toUpperCase();
//C# MD5 ResultString Fix
StringBuffer keys = new StringBuffer();
for (int i = 0; i < keyLen; i += 2) {
keys.append(key8.substring(i, i + 2)).append("-");
}
byte[] KeyStr = keys.substring(0, keyLen).getBytes("utf-8");
DESKeySpec keySpec = new DESKeySpec(KeyStr);// 设置密钥参数
IvParameterSpec iv = new IvParameterSpec(KeyStr);// 设置向量
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
SecretKey key = keyFactory.generateSecret(keySpec);
enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式给出密钥和向量
deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
deCipher.init(Cipher.DECRYPT_MODE, key, iv);// 设置工作模式为加密模式给出密钥和向量
} catch (Exception e) {
e.printStackTrace();
}
}
//加密
public static String encrypt(String text)
{
if (StringUtils.isEmpty(text)) return null;
byte[] pasByte = null;
try {
pasByte = enCipher.doFinal(text.getBytes("utf-8"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
return new String(byte2hex(pasByte));
}
//解密
public static String decrypt(String crypt)
{
if (StringUtils.isEmpty(crypt)) return null;
byte[] pasByte = null;
try {
pasByte = deCipher.doFinal(hex2byte(crypt.getBytes("utf-8")));
} catch (Exception e) {
e.printStackTrace();
return null;
}
return new String(pasByte);
}
public static void main(String args[]) {
//System.out.println(encrypt("x123456"));
System.out.println(decrypt("248E135E28C103B4"));
}
private static final String HEX_CHAR = "0123456789ABCDEF";
private static final byte[] HEX_STRING_BYTE = HEX_CHAR.getBytes();
//10进制转16进制
private static byte[] byte2hex(byte[] b) {
int length = b.length;
byte[] b2 = new byte[length << 1];
int pos;
for(int i=0; i<length; i++) {
pos = 2*i;
b2[pos] = HEX_STRING_BYTE[(b[i] & 0xf0) >> 4];
b2[pos+1] = HEX_STRING_BYTE[b[i] & 0x0f];
}
return b2;
}
//16进制转10进制
public static byte[] hex2byte(byte[] b) {
if(b.length%2 != 0) {
throw new IllegalArgumentException("byte array length is not even!");
}
int length = b.length >> 1;
byte[] b2 = new byte[length];
int pos;
for(int i=0; i<length; i++) {
pos = i << 1;
b2[i] = (byte) (HEX_CHAR.indexOf( b[pos] ) << 4 | HEX_CHAR.indexOf( b[pos+1] ) );
}
return b2;
}
}

View File

@ -50,7 +50,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</select> </select>
<select id="selectBizMemberSimple" parameterType="Long" resultMap="BizMemberResult"> <select id="selectBizMemberSimple" parameterType="Long" resultMap="BizMemberResult">
select id, mobile, member_name, recommend_id, recommend_mobile, recommend_name, member_type, is_delete, is_enable, create_by, create_time, update_by, update_time select id, mobile, member_name, password, recommend_id, recommend_mobile, recommend_name, member_type, is_delete, is_enable, create_by, create_time, update_by, update_time
from biz_member where id = #{id} from biz_member where id = #{id}
</select> </select>

View File

@ -131,7 +131,7 @@
align: 'center', align: 'center',
formatter: function(value, row, index) { formatter: function(value, row, index) {
var actions = []; var actions = [];
actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="updatePwd(\'' + row.id + '\', \'' + row.password + '\')"><i class="fa fa-cog"></i>修改密码</a> '); actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="updatePwd(\'' + row.id + '\')"><i class="fa fa-cog"></i>修改密码</a> ');
actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> '); actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>'); actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
return actions.join(''); return actions.join('');
@ -142,23 +142,33 @@
}); });
//修改密码 //修改密码
function updatePwd(memberID, oldPwd){ function updatePwd(memberID){
$.modal.confirm("<div id='anch'>请输入新的密码<br/><input class='form-control' id='newPwd' value='" + oldPwd + "' maxlength='30'></div>", function() { $.post(prefix + "/showPassword", {
let pwd = $("#newPwd").val(); memberID:memberID
if(pwd.length == 0){ }, function(response){
$.modal.msgError("请输入新密码"); if(response.code == 0){
return false; showPwd(response.data.pwd)
} }
$.operate.post(prefix + "/editPassword", {
memberID:memberID,
password:pwd
}, function(response){
if(response.code == 0){
$.modal.alertSuccess('操作成功!')
}
});
}); });
$("#anch").parent().css("padding", "10px 20px").children(".layui-layer-ico").hide();
let showPwd = function(oldPwd){
$.modal.confirm("<div id='anch'>请输入新的密码<br/><input class='form-control' id='newPwd' value='" + oldPwd + "' maxlength='30'></div>", function() {
let pwd = $("#newPwd").val();
if(pwd.length == 0){
$.modal.msgError("请输入新密码");
return false;
}
$.operate.post(prefix + "/editPassword", {
memberID:memberID,
password:pwd
}, function(response){
if(response.code == 0){
$.modal.alertSuccess('操作成功!')
}
});
});
$("#anch").parent().css("padding", "10px 20px").children(".layui-layer-ico").hide();
}
} }
</script> </script>
</body> </body>