des加密文件 java_java DES加密解密文件

在android开发中或者java开发中,经常用一些加密或者解密文件的,下面是我最近测试的一个DES加密解密文件的方式,记录一下,防止以后忘记了!

下面直接贴代码把,详细的注释都写有:

package com.spring.des;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.security.InvalidKeyException;

import java.security.Key;

import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.spec.SecretKeySpec;

/***

* Des文件加密解密

*

* @author spring sky

* Email:vipa1888@163.com

* QQ:840950105

*

*/

public class DesFileEncrypt {

/**

* 要加密的文件路径列表

*/

public static String[] filePath = { "D:/sasdfp.sql" };

/**

* 加密后的文件路径列表

*/

public static String[] outFilePath = new String[filePath.length];

private static final String KEY = "spring sky";

public DesFileEncrypt() {

super();

getKey(KEY);

initCipher();

//初始化开始加密文件

crateEncryptFile();

}

private Key key;

/***

* 解密密码

*/

private Cipher cipherDecrypt;

/**

* 加密密码

*/

private Cipher cipherEncrypt;

/**

* 加密文件平且记录加密后的文件路径

* */

private void crateEncryptFile() {

String outPath = null;

for (int i = 0; i < filePath.length; i++) {

try {

outPath = filePath[i].substring(0,filePath[i].lastIndexOf("."))+".bin";

encrypt(filePath[i], outPath);

outFilePath[i] = outPath;

System.out.println(filePath[i]+"加密完成,加密后的文件是:"+outFilePath[i]);

} catch (Exception e) {

e.printStackTrace();

}

}

System.out.println("=========================加密完成=======================");

}

/**

* 加密文件的核心

*

* @param file

* 要加密的文件

* @param destFile

* 加密后存放的文件名

*/

public void encrypt(String file, String destFile) throws Exception {

InputStream is = new FileInputStream(file);

OutputStream out = new FileOutputStream(destFile);

CipherInputStream cis = new CipherInputStream(is, cipherEncrypt);

byte[] buffer = new byte[1024];

int r;

while ((r = cis.read(buffer)) > 0) {

out.write(buffer, 0, r);

}

cis.close();

is.close();

out.close();

}

/***

* 解密文件

* @param destFile

*/

public void decrypt(String destFile) {

try {

InputStream is = new FileInputStream(destFile);

CipherInputStream cis = new CipherInputStream(is, cipherDecrypt);

BufferedReader reader = new BufferedReader(new InputStreamReader(

cis));

String line = null;

while ((line = reader.readLine()) != null) {

System.out.println(line);

}

reader.close();

cis.close();

is.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

private void initCipher() {

try {

// 加密的cipher

cipherEncrypt = Cipher.getInstance("DES");

cipherEncrypt.init(Cipher.ENCRYPT_MODE, this.key);

// 解密的cipher

cipherDecrypt = Cipher.getInstance("DES");

cipherDecrypt.init(Cipher.DECRYPT_MODE, this.key);

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

}

}

/**

* 自定义一个key

*

* @param string

*/

public Key getKey(String keyRule) {

// Key key = null;

byte[] keyByte = keyRule.getBytes();

// 创建一个空的八位数组,默认情况下为0

byte[] byteTemp = new byte[8];

// 将用户指定的规则转换成八位数组

for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {

byteTemp[i] = keyByte[i];

}

key = new SecretKeySpec(byteTemp, "DES");

return key;

}

/**

* 解密密码

*

* @return

*/

public Cipher getCipherEdcrypt() {

return cipherDecrypt;

}

/**

* 加密密码

*

* @return

*/

public Cipher getCipherEncrypt() {

return cipherEncrypt;

}

/***

* 测试加密解密

* @param args

*/

public static void main(String[] args) {

DesFileEncrypt desFileEncrypt = new DesFileEncrypt();

desFileEncrypt.decrypt(outFilePath[0]); //解密第一个文件平且测试解密后的结果

}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值