欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-23 16:47:40
复制代码 代码如下:import java.io.bytearrayinputstream;import java.io.bytearrayoutputstream;imp...

复制代码 代码如下:

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 ));
 }
}