zoukankan      html  css  js  c++  java
  • 后端使用aes 加密

    package com.midea.ccs.mobile.tools.impl;

    import org.apache.commons.codec.binary.Hex;

    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.Key;


    public class AesUtil {
    /**
    * Description: 加密操作 <br>
    *
    * @param data 待加密数据
    * @param key 密钥
    * @return 加解密后的信息
    * @throws Exception <br>
    */
    public static byte[] encrypt(byte[] data, byte[] key)
    throws Exception {
    Key secretKey = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return cipher.doFinal(data);
    }

    /**
    * Description: 解密操作 <br>
    *
    * @param data 待解密数据
    * @param key 密钥
    * @return 解密后的信息
    * @throws Exception <br>
    */
    public static byte[] decrypt(byte[] data, byte[] key)
    throws Exception {
    Key secretKey = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    return cipher.doFinal(data);
    }

    /**
    * Description: 加密操作 <br>
    *
    * @param data 待加密数据
    * @param key 密钥
    * @return 加解密后的信息,16进制
    * @throws Exception <br>
    */
    public static String encryptHex(String data, String key)
    throws Exception {
    byte[] bytes = encrypt(data.getBytes(), key.getBytes());
    return Hex.encodeHexString(bytes);
    }

    /**
    * Description: 解密操作 <br>
    *
    * @param data 待解密数据
    * @param key 密钥
    * @return 解密后的信息,16进制
    * @throws Exception <br>
    */
    public static String decryptHex(String data, String key)
    throws Exception {
    byte[] datas = Hex.decodeHex(data.toCharArray());
    byte[] bytes = decrypt(datas, key.getBytes());
    return new String(bytes, "utf-8");
    }


    public static void main(String[] args) throws Exception {
    //2f3e660c0ff68c9fcec4e1bd511c3d3e
    String md5 = "067d1e0c2512a009";
    String mobile = "15994583505";

    String hex = encryptHex(mobile, md5);

    decryptHex(hex, md5);

    }

    }

  • 相关阅读:
    flink-cdc读取postgres报异常,没有发布表
    yum 安装高版本Git
    分布式存储FastDFS搭建
    ElasticSearch6.5.1集群部署
    CentOS7 OpenSSH编译安装升级
    K8S使用ceph实现持久化存储
    ceph分布式集群的搭建
    canal服务搭建
    MySQL-5.7.31的搭建
    基于CentOS7.6使用KubeOperator安装Kubernetes集群
  • 原文地址:https://www.cnblogs.com/zhangzonghua/p/9302211.html
Copyright © 2011-2022 走看看