zoukankan      html  css  js  c++  java
  • 字符串加密

      1 package com.springcloud.util;
      2 
      3 import java.security.Key;
      4 import java.security.Security;
      5 
      6 import javax.crypto.Cipher;
      7 /**
      8  * 加密类
      9  * @author 
     10  *
     11  */
     12 public class EncryptionDecryption {
     13     /** 默认加密key */
     14     private static String strDefaultKey = "Defaul";
     15 
     16     /** 加密工具 */
     17     private Cipher encryptCipher = null;
     18 
     19     /** 解密工具 */
     20     private Cipher decryptCipher = null;
     21     /**
     22      * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0812, 和public static byte[]
     23      * hexStr2ByteArr(String strIn) 互为可逆的转换过程
     24      * 
     25      * @param arrB
     26      *            需要转换的byte数组
     27      * @return 转换后的字符串
     28      * @throws Exception
     29      * 
     30      */
     31     public static String byteArr2HexStr(byte[] arrB) throws Exception {
     32         int length = arrB.length;
     33         // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
     34         StringBuffer sb = new StringBuffer(length * 2);
     35         for (int i = 0; i < length; i++) {
     36             int intTmp = arrB[i];
     37             // 把负数转换为正数
     38             while (intTmp < 0) {
     39                 intTmp = intTmp + 256;
     40             }
     41             // 小于0F的数需要在前面补0
     42             if (intTmp < 16) {
     43                 sb.append("0");
     44             }
     45             sb.append(Integer.toString(intTmp, 16));
     46         }
     47         return sb.toString();
     48     }
     49 
     50     /**
     51      * 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
     52      * 互为可逆的转换过程
     53      * @param strIn
     54      *            需要转换的字符串
     55      * @return 转换后的byte数组
     56      * @throws Exception
     57      *
     58      */
     59     public static byte[] hexStr2ByteArr(String strIn) throws Exception {
     60         byte[] arrB = strIn.getBytes();
     61         int iLen = arrB.length;
     62         // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
     63         byte[] arrOut = new byte[iLen / 2];
     64         for (int i = 0; i < iLen; i = i + 2) {
     65             String strTmp = new String(arrB, i, 2);
     66             arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
     67         }
     68         return arrOut;
     69     }
     70 
     71     /**
     72      * 默认构造方法,使用默认密钥
     73      * 
     74      * @throws Exception
     75      */
     76     public EncryptionDecryption() throws Exception {
     77         this(strDefaultKey);
     78     }
     79 
     80     /**
     81      * 指定密钥构造方法
     82      * 
     83      * @param strKey
     84      *            指定的密钥
     85      * @throws Exception
     86      */
     87     public EncryptionDecryption(String strKey) throws Exception {
     88         Security.addProvider(new com.sun.crypto.provider.SunJCE());
     89         Key key = getKey(strKey.getBytes());
     90         encryptCipher = Cipher.getInstance("DES");
     91         encryptCipher.init(Cipher.ENCRYPT_MODE, key);
     92         decryptCipher = Cipher.getInstance("DES");
     93         decryptCipher.init(Cipher.DECRYPT_MODE, key);
     94     }
     95     /**
     96      * 加密字节数组
     97      * 
     98      * @param arrB
     99      *            需加密的字节数组
    100      * @return 加密后的字节数组
    101      * @throws Exception
    102      */
    103     public byte[] encrypt(byte[] arrB) throws Exception {
    104         return encryptCipher.doFinal(arrB);
    105     }
    106     /**
    107      * 加密字符串
    108      * 
    109      * @param strIn
    110      *            需加密的字符串
    111      * @return 加密后的字符串
    112      * @throws Exception
    113      */
    114     public String encrypt(String strIn) throws Exception {
    115         return byteArr2HexStr(encrypt(strIn.getBytes()));
    116     }
    117     /**
    118      * 解密字节数组
    119      * 
    120      * @param arrB
    121      *            需解密的字节数组
    122      * @return 解密后的字节数组
    123      * @throws Exception
    124      */
    125     public byte[] decrypt(byte[] arrB) throws Exception {
    126         return decryptCipher.doFinal(arrB);
    127     }
    128 
    129     /**
    130      * 解密字符串
    131      * @param strIn
    132      *            需解密的字符串
    133      * @return 解密后的字符串
    134      * @throws Exception
    135      */
    136     public String decrypt(String strIn) throws Exception {
    137         try {
    138             return new String(decrypt(hexStr2ByteArr(strIn)));
    139         } catch (Exception e) {
    140             return "";
    141         }
    142     }
    143     /**
    144      * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
    145      * @param arrBTmp
    146      *            构成该字符串的字节数组
    147      * @return 生成的密钥
    148      * @throws java.lang.Exception
    149      */
    150     private Key getKey(byte[] arrBTmp) throws Exception {
    151         // 创建一个空的8位字节数组(默认值为0)
    152         byte[] arrB = new byte[8];
    153         // 将原始字节数组转换为8位
    154         for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
    155             arrB[i] = arrBTmp[i];
    156         }
    157         // 生成密钥
    158         Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
    159         return key;
    160     }
    161 
    162 }

    本代码摘自某大神博客。

  • 相关阅读:
    MOS
    1- Bluetooth开发
    1- Bluetooth
    MCU 51-10 Infrared Communication
    1- STM32 HAL库学习 GPIO配置点亮LED灯
    MCU 51-9 DS18B20 Temperature Sensor
    MCU 51-8 AD/DA
    C语言讲义——变量(variable)
    C语言讲义——注释
    C语言讲义——“编译、链接”
  • 原文地址:https://www.cnblogs.com/lfyu/p/9248817.html
Copyright © 2011-2022 走看看