32位md5解密_用户名与密码前后加密、后台解密实现方案

随着https的推广,越来越多的网站转到https协议了。但是还是有不少网站没有切换到https,还是使用http。使用http协议的网站,如果没有自己做用户名、密码及敏感信息加密;网络就会明文传输这些数据,如果一些没有用心的人对服务发起攻击,就可能给公司造成损失。

好比如下登录接口,就是使用明文传输,而且是http协议,非常危险。

52387c4a458a27748cd5b11fc13023f2.png

0x01:前端AES加密

crypto-js是谷歌开发的一个纯JavaScript的加密算法类库,可以非常方便的在前端进行其所支持的加解密操作。目前crypto-js已支持的算法有:MD5、SHA-1、SHA-256、AES、RSA、Rabbit、MARC4、HMAC、HMAC-MD5、HMAC-SHA1、HMAC-SHA256、PBKDF2等。使用时可以引用总文件,也可以单独引用某一文件。

仓库:https://github.com/brix/crypto-js/releases

  • 引入crypto-js.js文件
  • 使用crypto-js进行加密
const KEY = CryptoJS.enc.Utf8.parse("1234567890123456");const IV = CryptoJS.enc.Utf8.parse('1234567890123456');let key = KEYlet iv = IVvar pwd = $("#password").val();let pwdEncode = CryptoJS.enc.Utf8.parse(pwd);var encryptedPassword = CryptoJS.AES.encrypt(pwdEncode, key, {     iv: iv,     mode: CryptoJS.mode.CBC,    padding: CryptoJS.pad.ZeroPadding})var encryptPasswordBase64 =  CryptoJS.enc.Base64.stringify(encryptedPassword.ciphertext);$("#password").val(encryptPasswordBase64);        var username = $("#username").val();let nameuserEncode = CryptoJS.enc.Utf8.parse(username);var encryptedUsername = CryptoJS.AES.encrypt(nameuserEncode, key, {      iv: iv,     mode: CryptoJS.mode.CBC,     padding: CryptoJS.pad.ZeroPadding}) var encryptedUsernameBase64 =  CryptoJS.enc.Base64.stringify(encryptedUsername.ciphertext);  $("#username").val(encryptedUsernameBase64 );

0x02:后端解密

后端控制器在接收到前端的username和password,就可以响应在后端解密。因为前端使用的AES加密,所以后端也要使用AES解密;这里一定要对应,否则肯定解密失败。如果前端使用RSA加密,那么后端就必须使用RSA解密。

/** * AES 128bit 加密解密工具类 */import org.apache.commons.codec.binary.Base64;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;public class AesEncryptUtil {    //使用AES-128-CBC加密模式,key需要为16位,key和iv可以相同!    private static String KEY = "1234567890123456";    private static String IV = "1234567890123456";/** * 加密方法 * @param data  要加密的数据 * @param key 加密key * @param iv 加密iv * @return 加密的结果 * @throws Exception */public static String encrypt(String data, String key, String iv) throws Exception {    try {        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//"算法/模式/补码方式"NoPadding PkcsPadding        int blockSize = cipher.getBlockSize();        byte[] dataBytes = data.getBytes();        int plaintextLength = dataBytes.length;        if (plaintextLength % blockSize != 0) {            plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));        }        byte[] plaintext = new byte[plaintextLength];        System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);        byte[] encrypted = cipher.doFinal(plaintext);        return new Base64().encodeToString(encrypted);    } catch (Exception e) {        e.printStackTrace();        return null;    }}/** * 解密方法 * @param data 要解密的数据 * @param key  解密key * @param iv 解密iv * @return 解密的结果 * @throws Exception */public static String desEncrypt(String data, String key, String iv) throws Exception {    try {        byte[] encrypted1 = new Base64().decode(data);        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);        byte[] original = cipher.doFinal(encrypted1);        String originalString = new String(original);        return originalString;    } catch (Exception e) {        e.printStackTrace();        return null;    }}/** * 使用默认的key和iv加密 * @param data * @return * @throws Exception */public static String encrypt(String data) throws Exception {    return encrypt(data, KEY, IV);}/** * 使用默认的key和iv解密 * @param data * @return * @throws Exception */public static String desEncrypt(String data) throws Exception {    return desEncrypt(data, KEY, IV);}}

控制器Controller部分代码:

String username = AesEncryptUtil.desEncrypt(      request.getParameter("username"));String password = AesEncryptUtil.desEncrypt(      request.getParameter("password"));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值