最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java加密解密工具(適用于JavaSE/JavaEE/Android)

 更新時(shí)間:2016年04月18日 16:29:26   作者:捕實(shí)者_(dá)說  
這篇文章主要介紹了Java加密解密工具,適用于JavaSE/JavaEE/Android,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了一個(gè)適用于JavaSE/JavaEE/Android的Java加密解密工具,供大家學(xué)習(xí),具體內(nèi)容如下

package longshu.utils.security;
 
import java.lang.reflect.Method;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
 
/**
 * Java加密解密工具.
 * JavaSE/JavaEE/Android都適用
 * 
 * @author longshu 2016年4月13日
 */
public class EncryptDecrypt {
 // 無需創(chuàng)建對(duì)象
 private EncryptDecrypt() {
 }
 
 /**
  * SHA1加密Bit數(shù)據(jù)
  * @param source byte數(shù)組
  * @return 加密后的byte數(shù)組
  */
 public static byte[] SHA1Bit(byte[] source) {
  try {
   MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");
   sha1Digest.update(source);
   byte targetDigest[] = sha1Digest.digest();
   return targetDigest;
  } catch (NoSuchAlgorithmException e) {
   throw new RuntimeException(e);
  }
 }
 
 /**
  * SHA1加密字符串?dāng)?shù)據(jù)
  * @param source 要加密的字符串
  * @return 加密后的字符串
  */
 public static String SHA1(String source) {
  return byte2HexStr(SHA1Bit(source.getBytes()));
 }
 
 /**
  * MD5加密Bit數(shù)據(jù)
  * @param source byte數(shù)組
  * @return 加密后的byte數(shù)組
  */
 public static byte[] MD5Bit(byte[] source) {
  try {
   // 獲得MD5摘要算法的 MessageDigest對(duì)象
   MessageDigest md5Digest = MessageDigest.getInstance("MD5");
   // 使用指定的字節(jié)更新摘要
   md5Digest.update(source);
   // 獲得密文
   return md5Digest.digest();
  } catch (NoSuchAlgorithmException e) {
   throw new RuntimeException(e);
  }
 }
 
 /**
  * MD5加密字符串,32位長(zhǎng)
  * @param source 要加密的內(nèi)容
  * @return 加密后的內(nèi)容
  */
 public static String MD5(String source) {
  return byte2HexStr(MD5Bit(source.getBytes()));
 }
 
 /**
  * BASE64編碼
  * @param source 要編碼的字符串
  * @return 編碼過的字符串
  */
 public static String encodeBASE64(String source) {
  Class<?> clazz = null;
  Method encodeMethod = null;
  try {// 優(yōu)先使用第三方庫(kù)
   clazz = Class.forName("org.apache.commons.codec.binary.Base64");
   encodeMethod = clazz.getMethod("encodeBase64", byte[].class);
   System.out.println("encodeBASE64-->" + clazz);
   System.out.println("encodeMethod-->" + encodeMethod);
   // 反射方法 靜態(tài)方法執(zhí)行無需對(duì)象
   return new String((byte[]) encodeMethod.invoke(null, source.getBytes()));
  } catch (ClassNotFoundException e) {
   String vm = System.getProperty("java.vm.name");
   System.out.println(vm);
   try {
    if ("Dalvik".equals(vm)) {// Android
     clazz = Class.forName("android.util.Base64");
     // byte[] Base64.encode(byte[] input,int flags)
     encodeMethod = clazz.getMethod("encode", byte[].class, int.class);
     System.out.println("encodeBASE64-->" + clazz);
     System.out.println("encodeMethod-->" + encodeMethod);
     return new String((byte[]) encodeMethod.invoke(null, source.getBytes(), 0));
    } else {// JavaSE/JavaEE
     clazz = Class.forName("sun.misc.BASE64Encoder");
     encodeMethod = clazz.getMethod("encode", byte[].class);
     System.out.println("encodeBASE64-->" + clazz);
     System.out.println("encodeMethod-->" + encodeMethod);
     return (String) encodeMethod.invoke(clazz.newInstance(), source.getBytes());
    }
   } catch (ClassNotFoundException e1) {
    return null;
   } catch (Exception e1) {
    return null;
   }
  } catch (Exception e) {
   return null;
  }
  /*
   * Android
   * android.util.Base64
   */
  // return Base64.encodeToString(source, Base64.DEFAULT);
  // return new String(Base64.encode(source.getBytes(), Base64.DEFAULT));
 
  /*
   * JavaSE/JavaEE
   */
  // sun.misc.BASE64Encoder
  // BASE64Encoder encoder = new BASE64Encoder();
  // return encoder.encode(source.getBytes());
 
  // org.apache.commons.codec.binary.Base64
  // return new String(Base64.encodeBase64(source.getBytes()));
 }
 
 /**
  * BASE64解碼
  * @param encodeSource 編碼過的字符串
  * @return 編碼前的字符串
  */
 public static String decodeBASE64(String encodeSource) {
  Class<?> clazz = null;
  Method decodeMethod = null;
 
  try {// 優(yōu)先使用第三方庫(kù)
   clazz = Class.forName("org.apache.commons.codec.binary.Base64");
   decodeMethod = clazz.getMethod("decodeBase64", byte[].class);
   System.out.println("decodeBASE64-->" + clazz);
   System.out.println("decodeMethod-->" + decodeMethod);
   // 反射方法 靜態(tài)方法執(zhí)行無需對(duì)象
   return new String((byte[]) decodeMethod.invoke(null, encodeSource.getBytes()));
  } catch (ClassNotFoundException e) {
   String vm = System.getProperty("java.vm.name");
   System.out.println(vm);
   try {
    if ("Dalvik".equals(vm)) {// Android
     clazz = Class.forName("android.util.Base64");
     // byte[] Base64.decode(byte[] input, int flags)
     decodeMethod = clazz.getMethod("decode", byte[].class, int.class);
     System.out.println("decodeBASE64-->" + clazz);
     System.out.println("decodeMethod-->" + decodeMethod);
     return new String((byte[]) decodeMethod.invoke(null, encodeSource.getBytes(), 0));
    } else { // JavaSE/JavaEE
     clazz = Class.forName("sun.misc.BASE64Decoder");
     decodeMethod = clazz.getMethod("decodeBuffer", String.class);
     System.out.println("decodeBASE64-->" + clazz);
     System.out.println("decodeMethod-->" + decodeMethod);
     return new String((byte[]) decodeMethod.invoke(clazz.newInstance(), encodeSource));
    }
   } catch (ClassNotFoundException e1) {
    return null;
   } catch (Exception e1) {
    return null;
   }
  } catch (Exception e) {
   return null;
  }
  /*
   * Android
   * android.util.Base64
   */
  // return new
  // String(Base64.decode(encodeSource.getBytes(),Base64.DEFAULT));
 
  /*
   * JavaSE/JavaEE
   */
  // sun.misc.BASE64Decoder
  // try {
  // BASE64Decoder decoder = new BASE64Decoder();
  // return new String(decoder.decodeBuffer(encodeSource));
  // } catch (IOException e) {
  // throw new RuntimeException(e);
  // }
 
  // org.apache.commons.codec.binary.Base64
  // return new String(Base64.decodeBase64(encodeSource.getBytes()));
 }
 
 /**
  * AES加密
  * @param content 待加密的內(nèi)容
  * @param password 加密密碼
  * @return
  */
 public static byte[] encryptBitAES(byte[] content, String password) {
  try {
   Cipher encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 創(chuàng)建密碼器
   encryptCipher.init(Cipher.ENCRYPT_MODE, getKey(password));// 初始化
   byte[] result = encryptCipher.doFinal(content);
   return result; // 加密
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   e.printStackTrace();
  } catch (InvalidKeyException e) {
   e.printStackTrace();
  } catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  } catch (BadPaddingException e) {
   e.printStackTrace();
  }
  return null;
 }
 
 /**
  * AES解密
  * @param content 待解密內(nèi)容
  * @param password 解密密鑰
  * @return
  */
 public static byte[] decryptBitAES(byte[] content, String password) {
  try {
   Cipher decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 創(chuàng)建密碼器
   decryptCipher.init(Cipher.DECRYPT_MODE, getKey(password));// 初始化
   byte[] result = decryptCipher.doFinal(content);
   return result; // 加密結(jié)果
  } catch (InvalidKeyException e) {
   e.printStackTrace();
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   e.printStackTrace();
  } catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  } catch (BadPaddingException e) {
   e.printStackTrace();
  }
  return null;
 }
 
 /**
  * AES字符串加密
  * @param content 待加密的內(nèi)容
  * @param password 加密密碼
  * @return
  */
 public static String encryptAES(String content, String password) {
  return byte2HexStr(encryptBitAES(content.getBytes(), password));
 }
 
 /**
  * AES字符串解密
  * @param content 待解密內(nèi)容
  * @param password 解密密鑰
  * @return
  */
 public static String decryptAES(String content, String password) {
  return new String(decryptBitAES(hexStr2Bytes(content), password));
 }
 
 /**
  * 從指定字符串生成密鑰
  * @param password 構(gòu)成該秘鑰的字符串
  * @return 生成的密鑰
  * @throws NoSuchAlgorithmException
  */
 private static Key getKey(String password) throws NoSuchAlgorithmException {
  SecureRandom secureRandom = new SecureRandom(password.getBytes());
  // 生成KEY
  KeyGenerator kgen = KeyGenerator.getInstance("AES");
  kgen.init(128, secureRandom);
  SecretKey secretKey = kgen.generateKey();
  byte[] enCodeFormat = secretKey.getEncoded();
  // 轉(zhuǎn)換KEY
  SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
  return key;
 }
 
 /**
  * 將byte數(shù)組轉(zhuǎn)換為表示16進(jìn)制值的字符串.
  * 如:byte[]{8,18}轉(zhuǎn)換為:0812 
  * 和 byte[] hexStr2Bytes(String strIn) 互為可逆的轉(zhuǎn)換過程.
  * @param bytes 需要轉(zhuǎn)換的byte數(shù)組
  * @return 轉(zhuǎn)換后的字符串
  */
 public static String byte2HexStr(byte[] bytes) {
  int bytesLen = bytes.length;
  // 每個(gè)byte用兩個(gè)字符才能表示,所以字符串的長(zhǎng)度是數(shù)組長(zhǎng)度的兩倍
  StringBuffer hexString = new StringBuffer(bytesLen * 2);
  for (int i = 0; i < bytesLen; i++) {
   // 將每個(gè)字節(jié)與0xFF進(jìn)行與運(yùn)算,然后轉(zhuǎn)化為10進(jìn)制,然后借助于Integer再轉(zhuǎn)化為16進(jìn)制
   String hex = Integer.toHexString(bytes[i] & 0xFF);
   if (hex.length() < 2) {
    hexString.append(0);// 如果為1位 前面補(bǔ)個(gè)0
   }
   hexString.append(hex);
  }
  return hexString.toString();
 }
 
 /**
  * 將表示16進(jìn)制值的字符串轉(zhuǎn)換為byte數(shù)組,
  * 和 String byte2HexStr(byte[] bytes) 互為可逆的轉(zhuǎn)換過程.
  * @param bytes
  * @return 轉(zhuǎn)換后的byte數(shù)組
  */
 public static byte[] hexStr2Bytes(String strIn) {
  byte[] arrB = strIn.getBytes();
  int iLen = arrB.length;
 
  // 兩個(gè)字符表示一個(gè)字節(jié),所以字節(jié)數(shù)組長(zhǎng)度是字符串長(zhǎng)度除以2
  byte[] arrOut = new byte[iLen / 2];
  for (int i = 0; i < iLen; i = i + 2) {
   String strTmp = new String(arrB, i, 2);
   arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
  }
  return arrOut;
 }
 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Java實(shí)現(xiàn)DBF文件讀寫操作的完整指南

    Java實(shí)現(xiàn)DBF文件讀寫操作的完整指南

    DBF是一種數(shù)據(jù)庫(kù)文件格式,主要存儲(chǔ)結(jié)構(gòu)化數(shù)據(jù),本文將詳細(xì)介紹如何在Java中使用JDBF庫(kù)來讀取和創(chuàng)建DBF文件,有需要的小伙伴可以參考一下
    2025-04-04
  • SpringBoot響應(yīng)處理之以Json數(shù)據(jù)返回的實(shí)現(xiàn)方法

    SpringBoot響應(yīng)處理之以Json數(shù)據(jù)返回的實(shí)現(xiàn)方法

    這篇文章主要介紹了SpringBoot整合Web開發(fā)其中Json數(shù)據(jù)返回的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-09-09
  • 輕松搞定SpringBoot JPA使用配置過程詳解

    輕松搞定SpringBoot JPA使用配置過程詳解

    Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,該框架使用了特定的方式來進(jìn)行配置,它默認(rèn)配置了很多框架的使用方式,就像 Maven整合了所有的Jar包,Spring Boot 整合了所有的框架
    2021-06-06
  • Java如何把int類型轉(zhuǎn)換成byte

    Java如何把int類型轉(zhuǎn)換成byte

    這篇文章主要介紹了Java如何把int類型轉(zhuǎn)換成byte,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 淺談Java中FastJson的使用

    淺談Java中FastJson的使用

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著FastJson的使用展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • MybatisPlus處理大表查詢的實(shí)現(xiàn)步驟

    MybatisPlus處理大表查詢的實(shí)現(xiàn)步驟

    在實(shí)際工作中當(dāng)指定查詢數(shù)據(jù)過大時(shí),我們一般使用分頁查詢的方式一頁一頁的將數(shù)據(jù)放到內(nèi)存處理,本文主要介紹了MybatisPlus處理大表查詢的實(shí)現(xiàn)步驟,感興趣的可以了解一下
    2024-08-08
  • spring的applicationContext.xml文件與NamespaceHandler解析

    spring的applicationContext.xml文件與NamespaceHandler解析

    這篇文章主要介紹了spring的applicationContext.xml文件與NamespaceHandler解析,Spring容器啟動(dòng),在創(chuàng)建BeanFactory時(shí),需要加載和解析當(dāng)前ApplicationContext對(duì)應(yīng)的配置文件applicationContext.xml,從而獲取bean相關(guān)的配置信息,需要的朋友可以參考下
    2023-12-12
  • JAVA多線程搶紅包的實(shí)現(xiàn)示例

    JAVA多線程搶紅包的實(shí)現(xiàn)示例

    這篇文章主要介紹了JAVA多線程搶紅包的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java設(shè)計(jì)模式之創(chuàng)建者模式簡(jiǎn)介

    Java設(shè)計(jì)模式之創(chuàng)建者模式簡(jiǎn)介

    這篇文章主要介紹了Java設(shè)計(jì)模式之創(chuàng)建者模式,需要的朋友可以參考下
    2014-07-07
  • FactoryBean?BeanFactory方法使用示例詳解講解

    FactoryBean?BeanFactory方法使用示例詳解講解

    這篇文章主要為大家介紹了FactoryBean?BeanFactory方法使用示例詳解講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12

最新評(píng)論

富川| 绩溪县| 竹北市| 皋兰县| 锡林郭勒盟| 彭泽县| 汝州市| 介休市| 原阳县| 仁化县| 苗栗市| 鞍山市| 岳普湖县| 汤阴县| 开封县| 荥经县| 丽江市| 宁晋县| 张北县| 漳浦县| 平湖市| 鸡西市| 临潭县| 江孜县| 平顺县| 巴中市| 甘南县| 威海市| 鄯善县| 东丰县| 海盐县| 含山县| 嘉善县| 沙田区| 洮南市| 嵩明县| 开江县| 连南| 芜湖县| 五家渠市| 甘肃省|