使用java自带des加密算法实现文件加密和字符串加密

 更新时间:2014年03月06日 09:12:21   作者:  
这篇文章主要介绍了使用java自带des加密算法实现文件加密和字符串加密的示例,需要的朋友可以参考下

复制代码 代码如下:

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 :对文件进行加密
  * @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 :对字符串进行加密
 * @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 ));
 }
}

相关文章

  • Java动态代理和反射机制详解

    Java动态代理和反射机制详解

    这篇文章主要介绍了Java动态代理和反射机制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • CountDownLatch基于AQS阻塞工具用法详解

    CountDownLatch基于AQS阻塞工具用法详解

    这篇文章主要为大家介绍了CountDownLatch基于AQS阻塞工具用法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • Spring MVC中JSON数据处理方式实战案例

    Spring MVC中JSON数据处理方式实战案例

    Spring MVC是个灵活的框架,返回JSON数据的也有很多五花八门的方式,下面这篇文章主要给大家介绍了关于Spring MVC中JSON数据处理方式的相关资料,需要的朋友可以参考下
    2024-01-01
  • Java10新特性解读

    Java10新特性解读

    这篇文章主要介绍了Java10新特性的相关资料,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
    2021-02-02
  • Java中时间API的基本使用教程

    Java中时间API的基本使用教程

    这篇文章主要介绍了Java中时间API的基本使用教程,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • 简单阐述一下Java集合的概要

    简单阐述一下Java集合的概要

    今天给大家带来的文章是关于Java的相关知识,文章围绕着Java集合的概要展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • MyBatis Plus逻辑删除和分页插件使用详解

    MyBatis Plus逻辑删除和分页插件使用详解

    这篇文章主要介绍了MyBatis Plus之逻辑删除和分页插件使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • IDEA 离线迁移Springboot工程的方法步骤

    IDEA 离线迁移Springboot工程的方法步骤

    这篇文章主要介绍了IDEA 离线迁移Springboot工程的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • java 中模拟TCP传输的客户端和服务端实例详解

    java 中模拟TCP传输的客户端和服务端实例详解

    这篇文章主要介绍了java 中模拟TCP传输的客户端和服务端实例详解的相关资料,需要的朋友可以参考下
    2017-03-03
  • Java简单验证身份证功能示例

    Java简单验证身份证功能示例

    这篇文章主要介绍了Java简单验证身份证功能,涉及java针对字符串的截取、判断相关操作技巧,需要的朋友可以参考下
    2017-06-06

最新评论