欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中使用DES加密解密實例

 更新時間:2014年01月04日 16:09:14   作者:  
這篇文章主要介紹了java中使用DES加密解密實例,需要的朋友可以參考一下

在前面介紹了一些加密解密類的使用,這里綜合起來做一個簡單的測試,代碼如下:

MainActivity:

復制代碼 代碼如下:

package com.home.testdes;

import android.os.Bundle;
import android.util.Log;
import android.app.Activity;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  DESUtil u = new DESUtil();
  String mi = u.getEnc("I love you");
  Log.i("加密后", mi);
  String ming = u.getDec(mi);
  Log.i("解密后", ming);
 }
}

加密解密工具類:

復制代碼 代碼如下:

package com.home.testdes;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import android.util.Base64;

/**
 * 使用DES加密和解密工具類
 *
 * @author Administrator
 *
 */
public class DESUtil {

 private Key key;// 密鑰的key值
 private byte[] DESkey;
 private byte[] DESIV = { 0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xAB,
   (byte) 0xCD, (byte) 0xEF };
 private AlgorithmParameterSpec iv = null;// 加密算法的參數接口

 public DESUtil() {
  try {
   this.DESkey = "abcdefghijk".getBytes("UTF-8");// 設置密鑰
   DESKeySpec keySpec = new DESKeySpec(DESkey);// 設置密鑰參數
   iv = new IvParameterSpec(DESIV);// 設置向量
   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 獲得密鑰工廠
   key = keyFactory.generateSecret(keySpec);// 得到密鑰對象
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * 加密String 明文輸入密文輸出
  *
  * @param inputString
  *            待加密的明文
  * @return 加密后的字符串
  */
 public String getEnc(String inputString) {
  byte[] byteMi = null;
  byte[] byteMing = null;
  String outputString = "";
  try {
   byteMing = inputString.getBytes("UTF-8");
   byteMi = this.getEncCode(byteMing);
   byte[] temp = Base64.encode(byteMi, Base64.DEFAULT);
   outputString = new String(temp);
  } catch (Exception e) {
  } finally {
   byteMing = null;
   byteMi = null;
  }
  return outputString;
 }

 /**
  * 解密String 以密文輸入明文輸出
  *
  * @param inputString
  *            需要解密的字符串
  * @return 解密后的字符串
  */
 public String getDec(String inputString) {
  byte[] byteMing = null;
  byte[] byteMi = null;
  String strMing = "";
  try {
   byteMi = Base64.decode(inputString.getBytes(), Base64.DEFAULT);
   byteMing = this.getDesCode(byteMi);
   strMing = new String(byteMing, "UTF8");
  } catch (Exception e) {
  } finally {
   byteMing = null;
   byteMi = null;
  }
  return strMing;
 }

 /**
  * 加密以byte[]明文輸入,byte[]密文輸出
  *
  * @param bt
  *            待加密的字節(jié)碼
  * @return 加密后的字節(jié)碼
  */
 private byte[] getEncCode(byte[] bt) {
  byte[] byteFina = null;
  Cipher cipher;
  try {
   // 得到Cipher實例
   cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
   cipher.init(Cipher.ENCRYPT_MODE, key, iv);
   byteFina = cipher.doFinal(bt);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   cipher = null;
  }
  return byteFina;
 }

 /**
  * 解密以byte[]密文輸入,以byte[]明文輸出
  *
  * @param bt
  *            待解密的字節(jié)碼
  * @return 解密后的字節(jié)碼
  */
 private byte[] getDesCode(byte[] bt) {
  Cipher cipher;
  byte[] byteFina = null;
  try {
   // 得到Cipher實例
   cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
   cipher.init(Cipher.DECRYPT_MODE, key, iv);
   byteFina = cipher.doFinal(bt);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   cipher = null;
  }
  return byteFina;
 }
}

相關文章

  • 詳解Java中wait和sleep的區(qū)別

    詳解Java中wait和sleep的區(qū)別

    這篇文章主要介紹了Java中wait和sleep的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • SpringBoot項目執(zhí)行腳本 自動拉取最新代碼并重啟的實例內容

    SpringBoot項目執(zhí)行腳本 自動拉取最新代碼并重啟的實例內容

    在本篇文章里小編給大家整理的是一篇關于SpringBoot項目執(zhí)行腳本 自動拉取最新代碼并重啟的實例內容,有需要的朋友們參考下。
    2019-12-12
  • idea插件在線和離線安裝方法

    idea插件在線和離線安裝方法

    這篇文章主要介紹了idea插件在線和離線安裝方法,文末補充介紹了IntelliJ IDEA 安裝mybaits當前運行sql日志插件在線與離線安裝方法
    ,感興趣的朋友一起看看吧
    2023-12-12
  • 比較Java數組和各種List的性能小結

    比較Java數組和各種List的性能小結

    這篇文章主要是分別對Java數組、ArrayList、LinkedList和Vector進行隨機訪問和迭代等操作,并比較這種集合的性能。有需要的可以參考借鑒。
    2016-08-08
  • java8時間 yyyyMMddHHmmss格式轉為日期的代碼

    java8時間 yyyyMMddHHmmss格式轉為日期的代碼

    這篇文章主要介紹了java8時間 yyyyMMddHHmmss格式轉為日期的代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Java基礎之FastJson詳解

    Java基礎之FastJson詳解

    今天給大家復習Java基礎FastJson,文中有非常詳細的代碼示例,對正在學習java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • Java中i++的一些問題總結

    Java中i++的一些問題總結

    這篇文章主要給大家介紹了關于Java中i++的一些問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • Java使用Spring發(fā)送郵件的實現(xiàn)代碼

    Java使用Spring發(fā)送郵件的實現(xiàn)代碼

    本篇文章主要介紹了使用Spring發(fā)送郵件的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Spring Cloud 網關服務 zuul  動態(tài)路由的實現(xiàn)方法

    Spring Cloud 網關服務 zuul 動態(tài)路由的實現(xiàn)方法

    網關服務是流量的唯一入口。不能隨便停服務。所以動態(tài)路由就顯得尤為必要。這篇文章主要介紹了Spring Cloud 網關服務 zuul 三 動態(tài)路由的相關知識,需要的朋友可以參考下
    2019-10-10
  • 使用springboot對linux進行操控的方法示例

    使用springboot對linux進行操控的方法示例

    這篇文章主要介紹了使用springboot對linux進行操控的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11

最新評論