• java 加密解密工具类(实用!!!)


    最近发现了一个加密解密的好例子,很方便使用,可以作为平时开发的工具集,记录一下。

      1 package com.sh.springboottdemo2.util;
      2 
      3 
      4 import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
      5 
      6 import javax.crypto.Cipher;
      7 import javax.crypto.KeyGenerator;
      8 import javax.crypto.Mac;
      9 import javax.crypto.SecretKey;
     10 import javax.crypto.spec.SecretKeySpec;
     11 import java.security.MessageDigest;
     12 import java.security.SecureRandom;
     13 
     14 public class EncryptUtil {
     15     public static final String MD5 = "MD5";
     16     public static final String SHA1 = "SHA1";
     17     public static final String HmacMD5 = "HmacMD5";
     18     public static final String HmacSHA1 = "HmacSHA1";
     19     public static final String DES = "DES";
     20     public static final String AES = "AES";
     21 
     22     /**编码格式;默认使用uft-8*/
     23     public String charset = "utf-8";
     24     /**DES*/
     25     public int keysizeDES = 0;
     26     /**AES*/
     27     public int keysizeAES = 128;
     28 
     29     public static EncryptUtil me;
     30 
     31     private EncryptUtil(){
     32         //单例
     33     }
     34     //双重锁
     35     public static EncryptUtil getInstance(){
     36         if (me==null) {
     37            synchronized (EncryptUtil.class) {
     38                if(me == null){
     39                    me = new EncryptUtil();
     40                }
     41            }
     42         }
     43         return me;
     44     }
     45 
     46     /**
     47      * 使用MessageDigest进行单向加密(无密码)
     48      * @param res 被加密的文本
     49      * @param algorithm 加密算法名称
     50      * @return
     51      */
     52     private String messageDigest(String res,String algorithm){
     53         try {
     54             MessageDigest md = MessageDigest.getInstance(algorithm);
     55             byte[] resBytes = charset==null?res.getBytes():res.getBytes(charset);
     56             return base64(md.digest(resBytes));
     57         } catch (Exception e) {
     58             e.printStackTrace();
     59         }
     60         return null;
     61     }
     62 
     63     /**
     64      * 使用KeyGenerator进行单向/双向加密(可设密码)
     65      * @param res 被加密的原文
     66      * @param algorithm  加密使用的算法名称
     67      * @param key 加密使用的秘钥
     68      * @return
     69      */
     70     private String keyGeneratorMac(String res,String algorithm,String key){
     71         try {
     72             SecretKey sk = null;
     73             if (key==null) {
     74                 KeyGenerator kg = KeyGenerator.getInstance(algorithm);
     75                 sk = kg.generateKey();
     76             }else {
     77                 byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);
     78                 sk = new SecretKeySpec(keyBytes, algorithm);
     79             }
     80             Mac mac = Mac.getInstance(algorithm);
     81             mac.init(sk);
     82             byte[] result = mac.doFinal(res.getBytes());
     83             return base64(result);
     84         } catch (Exception e) {
     85             e.printStackTrace();
     86         }
     87         return null;
     88     }
     89 
     90     /**
     91      * 使用KeyGenerator双向加密,DES/AES,注意这里转化为字符串的时候是将2进制转为16进制格式的字符串,不是直接转,因为会出错
     92      * @param res 加密的原文
     93      * @param algorithm 加密使用的算法名称
     94      * @param key  加密的秘钥
     95      * @param keysize
     96      * @param isEncode
     97      * @return
     98      */
     99     private String keyGeneratorES(String res,String algorithm,String key,int keysize,boolean isEncode){
    100         try {
    101             KeyGenerator kg = KeyGenerator.getInstance(algorithm);
    102             if (keysize == 0) {
    103                 byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);
    104                 kg.init(new SecureRandom(keyBytes));
    105             }else if (key==null) {
    106                 kg.init(keysize);
    107             }else {
    108                 byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);
    109                 kg.init(keysize, new SecureRandom(keyBytes));
    110             }
    111             SecretKey sk = kg.generateKey();
    112             SecretKeySpec sks = new SecretKeySpec(sk.getEncoded(), algorithm);
    113             Cipher cipher = Cipher.getInstance(algorithm);
    114             if (isEncode) {
    115                 cipher.init(Cipher.ENCRYPT_MODE, sks);
    116                 byte[] resBytes = charset==null?res.getBytes():res.getBytes(charset);
    117                 return parseByte2HexStr(cipher.doFinal(resBytes));
    118             }else {
    119                 cipher.init(Cipher.DECRYPT_MODE, sks);
    120                 return new String(cipher.doFinal(parseHexStr2Byte(res)));
    121             }
    122         } catch (Exception e) {
    123             e.printStackTrace();
    124         }
    125         return null;
    126     }
    127 
    128     private String base64(byte[] res){
    129         return Base64.encode(res);
    130     }
    131 
    132     /**将二进制转换成16进制 */
    133     public static String parseByte2HexStr(byte buf[]) {
    134         StringBuffer sb = new StringBuffer();
    135         for (int i = 0; i < buf.length; i++) {
    136             String hex = Integer.toHexString(buf[i] & 0xFF);
    137             if (hex.length() == 1) {
    138                 hex = '0' + hex;
    139             }
    140             sb.append(hex.toUpperCase());
    141         }
    142         return sb.toString();
    143     }
    144     /**将16进制转换为二进制*/
    145     public static byte[] parseHexStr2Byte(String hexStr) {
    146         if (hexStr.length() < 1)
    147             return null;
    148         byte[] result = new byte[hexStr.length()/2];
    149         for (int i = 0;i< hexStr.length()/2; i++) {
    150             int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
    151             int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
    152             result[i] = (byte) (high * 16 + low);
    153         }
    154         return result;
    155     }
    156 
    157     /**
    158      * md5加密算法进行加密(不可逆)
    159      * @param res 需要加密的原文
    160      * @return
    161      */
    162     public String MD5(String res) {
    163         return messageDigest(res, MD5);
    164     }
    165 
    166     /**
    167      * md5加密算法进行加密(不可逆)
    168      * @param res  需要加密的原文
    169      * @param key  秘钥
    170      * @return
    171      */
    172     public String MD5(String res, String key) {
    173         return keyGeneratorMac(res, HmacMD5, key);
    174     }
    175 
    176     /**
    177      * 使用SHA1加密算法进行加密(不可逆)
    178      * @param res 需要加密的原文
    179      * @return
    180      */
    181     public String SHA1(String res) {
    182         return messageDigest(res, SHA1);
    183     }
    184 
    185     /**
    186      * 使用SHA1加密算法进行加密(不可逆)
    187      * @param res 需要加密的原文
    188      * @param key 秘钥
    189      * @return
    190      */
    191     public String SHA1(String res, String key) {
    192         return keyGeneratorMac(res, HmacSHA1, key);
    193     }
    194 
    195     /**
    196      * 使用DES加密算法进行加密(可逆)
    197      * @param res 需要加密的原文
    198      * @param key 秘钥
    199      * @return
    200      */
    201     public String DESencode(String res, String key) {
    202         return keyGeneratorES(res, DES, key, keysizeDES, true);
    203     }
    204 
    205     /**
    206      * 对使用DES加密算法的密文进行解密(可逆)
    207      * @param res 需要解密的密文
    208      * @param key 秘钥
    209      * @return
    210      */
    211     public String DESdecode(String res, String key) {
    212         return keyGeneratorES(res, DES, key, keysizeDES, false);
    213     }
    214 
    215     /**
    216      * 使用AES加密算法经行加密(可逆)
    217      * @param res 需要加密的密文
    218      * @param key 秘钥
    219      * @return
    220      */
    221     public String AESencode(String res, String key) {
    222         return keyGeneratorES(res, AES, key, keysizeAES, true);
    223     }
    224 
    225     /**
    226      * 对使用AES加密算法的密文进行解密
    227      * @param res 需要解密的密文
    228      * @param key 秘钥
    229      * @return
    230      */
    231     public String AESdecode(String res, String key) {
    232         return keyGeneratorES(res, AES, key, keysizeAES, false);
    233     }
    234 
    235     /**
    236      * 使用异或进行加密
    237      * @param res 需要加密的密文
    238      * @param key 秘钥
    239      * @return
    240      */
    241     public String XORencode(String res, String key) {
    242         byte[] bs = res.getBytes();
    243         for (int i = 0; i < bs.length; i++) {
    244             bs[i] = (byte) ((bs[i]) ^ key.hashCode());
    245         }
    246         return parseByte2HexStr(bs);
    247     }
    248 
    249     /**
    250      * 使用异或进行解密
    251      * @param res 需要解密的密文
    252      * @param key 秘钥
    253      * @return
    254      */
    255     public String XORdecode(String res, String key) {
    256         byte[] bs = parseHexStr2Byte(res);
    257         for (int i = 0; i < bs.length; i++) {
    258             bs[i] = (byte) ((bs[i]) ^ key.hashCode());
    259         }
    260         return new String(bs);
    261     }
    262 
    263     /**
    264      * 直接使用异或(第一调用加密,第二次调用解密)
    265      * @param res 密文
    266      * @param key 秘钥
    267      * @return
    268      */
    269     public int XOR(int res, String key) {
    270         return res ^ key.hashCode();
    271     }
    272 
    273     /**
    274      * 使用Base64进行加密
    275      * @param res 密文
    276      * @return
    277      */
    278     public String Base64Encode(String res) {
    279         return Base64.encode(res.getBytes());
    280     }
    281 
    282     /**
    283      * 使用Base64进行解密
    284      * @param res
    285      * @return
    286      */
    287     public String Base64Decode(String res) {
    288         return new String(Base64.decode(res));
    289     }
    290 }
  • 相关阅读:
    <%# %>数据绑定
    BUTTON在界面上位置的移动
    双击树节点,将其添加到ListBar中
    关于魔方的故事 来源:未知
    xml做TreeView
    子父窗体
    word中,文档更改标记
    Ogre中实现 几何面正反面不同纹理贴图
    利用模板化的空闲块列表克服内存碎片问题
    window平台下 实时高效打印其他窗口,并作为D3D纹理使用
  • 原文地址:https://www.cnblogs.com/zfding/p/9268245.html
Copyright © 2020-2023  润新知