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

使用java自帶des加密算法實(shí)現(xiàn)文件加密和字符串加密

 更新時(shí)間:2014年03月06日 09:12:21   作者:  
這篇文章主要介紹了使用java自帶des加密算法實(shí)現(xiàn)文件加密和字符串加密的示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
public class DesTool {
 private static final String PASSKEY = "afasdf";
 private static final String DESKEY = "asfsdfsdf";
 /**
  * @Comments :對(duì)文件進(jìn)行加密
  * @param filePath  要加密的文件路徑
  * @param fileName 文件
  * @param mode 加密模式  加密:Cipher.ENCRYPT_MODE 解密:Cipher.DECRYPT_MODE
  * @return
  */
 public static String encoderOrdecoder(String filePath, String fileName, int mode) {

  InputStream is = null;
  OutputStream out = null;
  CipherInputStream cis = null;

  try {
   SecureRandom sr = new SecureRandom();
   DESKeySpec dks = new DESKeySpec(DESKEY.getBytes());
   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
   SecretKey securekey = keyFactory.generateSecret(dks);
   IvParameterSpec iv = new IvParameterSpec(PASSKEY.getBytes());
   Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
   cipher.init(mode, securekey, iv, sr);

   File encoderFile = new File(filePath + File.separator + "encoder");
   if (!encoderFile.exists()) {
    encoderFile.mkdir();
   }

   is = new FileInputStream(filePath + File.separator + fileName);
   out = new FileOutputStream(filePath + File.separator + "encoder"
     + File.separator + fileName);

   cis = new CipherInputStream(is, cipher);
   byte[] buffer = new byte[1024];
   int r;
   while ((r = cis.read(buffer)) > 0) {
    out.write(buffer, 0, r);
   }

  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    if (is != null) {
     is.close();
    }
    if (cis != null) {
     cis.close();
    }
    if (out != null) {
     out.close();
    }
   } catch (Exception e1){

   }
  }
  return filePath + File.separator + "encoder" + File.separator
    + fileName;
 }

/**@Comments :對(duì)字符串進(jìn)行加密
 * @param src 源字符串
 * @param mode 加密模式  加密:Cipher.ENCRYPT_MODE 解密:Cipher.DECRYPT_MODE
 * @return
 */
public static String encoderOrdecoder( String src, int mode) {
  String tag="";
  InputStream is = null;
  OutputStream out = null;
  CipherInputStream cis = null;

  try {
   SecureRandom sr = new SecureRandom();
   DESKeySpec dks = new DESKeySpec(DESKEY.getBytes());
   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
   SecretKey securekey = keyFactory.generateSecret(dks);
   IvParameterSpec iv = new IvParameterSpec(PASSKEY.getBytes());
   Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
   cipher.init(mode, securekey, iv, sr);
   cis = new CipherInputStream(new ByteArrayInputStream(src.getBytes()) , cipher);
   out=new ByteArrayOutputStream();
   byte[] buffer = new byte[1024];
   int r;
   while ((r = cis.read(buffer)) > 0) {
    out.write(buffer, 0, r);
   }
   tag=out.toString();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    if (is != null) {
     is.close();
    }
    if (cis != null) {
     cis.close();
    }
    if (out != null) {
     out.close();
    }
   } catch (Exception e1){

   }
  }
  return tag;
 }
 public static void main(String[] args) {
  System.out.println("aaa"); 
  String t=encoderOrdecoder("aaa", Cipher.ENCRYPT_MODE );
  System.out.println(t); 
  System.out.println(encoderOrdecoder(t, Cipher.DECRYPT_MODE ));
 }
}

相關(guān)文章

  • spring如何解決循環(huán)依賴問題詳解

    spring如何解決循環(huán)依賴問題詳解

    這篇文章主要給大家介紹了關(guān)于spring如何解決循環(huán)依賴問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Spring MVC 攔截器 interceptor 用法詳解

    Spring MVC 攔截器 interceptor 用法詳解

    這篇文章主要介紹了Spring MVC 攔截器 interceptor 用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • JAVA實(shí)現(xiàn)社會(huì)統(tǒng)一信用代碼校驗(yàn)的方法

    JAVA實(shí)現(xiàn)社會(huì)統(tǒng)一信用代碼校驗(yàn)的方法

    這篇文章主要介紹了JAVA實(shí)現(xiàn)社會(huì)統(tǒng)一信用代碼校驗(yàn)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Java實(shí)現(xiàn)獲取某年某月第一天/最后一天的方法

    Java實(shí)現(xiàn)獲取某年某月第一天/最后一天的方法

    這篇文章主要介紹了Java實(shí)現(xiàn)獲取某年某月第一天/最后一天的方法,涉及java日期運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下
    2018-02-02
  • MybatisPlus結(jié)合groupby實(shí)現(xiàn)分組和sum求和的步驟

    MybatisPlus結(jié)合groupby實(shí)現(xiàn)分組和sum求和的步驟

    這篇文章主要介紹了MybatisPlus結(jié)合groupby實(shí)現(xiàn)分組和sum求和的步驟,這次使用的是LambdaQueryWrapper,使用QueryWrapper相對(duì)來說簡(jiǎn)單點(diǎn)就不寫了,本文分步驟給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2023-12-12
  • RestTemplate未使用線程池問題的解決方法

    RestTemplate未使用線程池問題的解決方法

    今天給大家?guī)淼氖顷P(guān)于Springboot的相關(guān)知識(shí),文章圍繞著RestTemplate未使用線程池展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • SpringBoot jar包大小優(yōu)化問題及解決

    SpringBoot jar包大小優(yōu)化問題及解決

    這篇文章主要介紹了SpringBoot jar包大小優(yōu)化問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • JavaEE中用response向客戶端輸出中文數(shù)據(jù)亂碼問題分析

    JavaEE中用response向客戶端輸出中文數(shù)據(jù)亂碼問題分析

    這篇文章主要介紹了JavaEE中用response向客戶端輸出中文數(shù)據(jù)亂碼問題分析,需要的朋友可以參考下
    2014-10-10
  • Mybatis調(diào)用SQL?Server存儲(chǔ)過程的實(shí)現(xiàn)示例

    Mybatis調(diào)用SQL?Server存儲(chǔ)過程的實(shí)現(xiàn)示例

    在軟件開發(fā)過程中,經(jīng)常會(huì)使用到存儲(chǔ)過程,本文就來介紹一下Mybatis調(diào)用SQL?Server存儲(chǔ)過程的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Spring MVC的web.xml配置詳解

    Spring MVC的web.xml配置詳解

    這篇文章主要介紹了Spring MVC的web.xml配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06

最新評(píng)論