java des文件加密_Java DES文件加密解密 javax.crypto.BadPaddingException: Given final block not properly padded...

package demo.security;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.security.Key;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.CipherOutputStream;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;/**

*

* DES加密解密工具包

*

*

* @author IceWee

* @date 2012-5-19

* @version 1.0*/public class DESUtils {

private static final String ALGORITHM= "DES";

private static finalint CACHE_SIZE = 1024;/**

*

* 生成随机密钥

*

*

* @return

* @throws Exception*/public static String getSecretKey() throws Exception {return getSecretKey(null);

}/**

*

* 生成密钥

*

*

* @param seed 密钥种子

* @return

* @throws Exception*/public static String getSecretKey(String seed) throws Exception {

SecureRandom secureRandom;if (seed != null && !"".equals(seed))

secureRandom= newSecureRandom(seed.getBytes());elsesecureRandom= newSecureRandom();

KeyGenerator keyGenerator=KeyGenerator.getInstance(ALGORITHM);

keyGenerator.init(secureRandom);

SecretKey secretKey=keyGenerator.generateKey();returnBase64Utils.encode(secretKey.getEncoded());

}/**

*

* 加密

*

*

* @param data

* @param key

* @return

* @throws Exception*/public staticbyte[] encrypt(byte[] data, String key) throws Exception {

Key k=toKey(Base64Utils.decode(key));

Cipher cipher=Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.ENCRYPT_MODE, k);returncipher.doFinal(data);

}/**

*

* 文件加密

*

*

* @param key

* @param sourceFilePath

* @param destFilePath

* @throws Exception*/public staticvoidencryptFile(String key, String sourceFilePath, String destFilePath) throws Exception {

File sourceFile= newFile(sourceFilePath);

File destFile= newFile(destFilePath);if (sourceFile.exists() &&sourceFile.isFile()) {if (!destFile.getParentFile().exists()) {

destFile.getParentFile().mkdirs();

}

destFile.createNewFile();

InputStreamin = newFileInputStream(sourceFile);

OutputStream out= newFileOutputStream(destFile);

Key k=toKey(Base64Utils.decode(key));

Cipher cipher=Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.ENCRYPT_MODE, k);

CipherInputStream cin= new CipherInputStream(in, cipher);byte[] cache = new byte[CACHE_SIZE];int nRead = 0;while ((nRead = cin.read(cache)) != -1) {

out.write(cache,0, nRead);

out.flush();

}

out.close();

cin.close();in.close();

}

}/**

*

* 解密

*

*

* @param data

* @param key

* @return

* @throws Exception*/public staticbyte[] decrypt(byte[] data, String key) throws Exception {

Key k=toKey(Base64Utils.decode(key));

Cipher cipher=Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE, k);returncipher.doFinal(data);

}/**

*

* 文件解密

*

*

* @param key

* @param sourceFilePath

* @param destFilePath

* @throws Exception*/public staticvoiddecryptFile(String key, String sourceFilePath, String destFilePath) throws Exception {

File sourceFile= newFile(sourceFilePath);

File destFile= newFile(destFilePath);if (sourceFile.exists() &&sourceFile.isFile()) {if (!destFile.getParentFile().exists()) {

destFile.getParentFile().mkdirs();

}

destFile.createNewFile();

InputStreamin = newFileInputStream(sourceFile);

OutputStream out= newFileOutputStream(destFile);

Key k=toKey(Base64Utils.decode(key));

Cipher cipher=Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE, k);

CipherOutputStream cout= newCipherOutputStream(out, cipher);byte[] cache = new byte[CACHE_SIZE];int nRead = 0;while ((nRead = in.read(cache)) != -1) {

cout.write(cache,0, nRead);

cout.flush();

}

cout.close();

out.close();in.close();

}

}/**

*

* 转换密钥

*

*

* @param key

* @return

* @throws Exception*/private static Key toKey(byte[] key) throws Exception {

DESKeySpec dks= newDESKeySpec(key);

SecretKeyFactory keyFactory=SecretKeyFactory.getInstance(ALGORITHM);

SecretKey secretKey=keyFactory.generateSecret(dks);returnsecretKey;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值