io.afu.utils.encryption.DESFileEncryption Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils Show documentation
Show all versions of utils Show documentation
RffanLAB Utils For Many Way use
package io.afu.utils.encryption;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.spi.LoggerContextFactory;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.Key;
import java.util.ArrayList;
import java.util.List;
public class DESFileEncryption {
public DESFileEncryption(String keyStr) {
genKey(keyStr);
initCipher();
}
private Key key;
// 解密密码
private Cipher cipherDecrypt;
// 加密密码
private Cipher cipherEncrypt;
/**
* 加密文件的核心
*
* @param srcFileName
* 要加密的文件
* @param destFileName
* 加密后存放的文件名
* @return boolean 是否加密成功
*/
public boolean encryptFile(String srcFileName, String destFileName) {
try {
InputStream is = new FileInputStream(srcFileName);
OutputStream out = new FileOutputStream(destFileName);
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();
return true;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
/***
* 解密文件
*
* @param fileName 文件名(包含具体路劲)
* @return List 返回的字符串列表
*/
public List decryptFileContent(String fileName) {
List list = new ArrayList();
try {
InputStream is = new FileInputStream(fileName);
CipherInputStream cis = new CipherInputStream(is, cipherDecrypt);
BufferedReader reader = new BufferedReader(new InputStreamReader(cis));
String line = null;
while ((line = reader.readLine()) != null) {
list.add(line);
}
reader.close();
cis.close();
is.close();
}
catch (Exception e) {
e.printStackTrace();
}
return list;
}
public void decryptFileContent(String fileName,String dstFilePath){
try {
InputStream is = new FileInputStream(fileName);
CipherInputStream cis = new CipherInputStream(is, cipherDecrypt);
BufferedReader reader = new BufferedReader(new InputStreamReader(cis));
FileWriter fileWriter = new FileWriter(dstFilePath);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String line = null;
while ((line = reader.readLine()) != null) {
bufferedWriter.write(line);
bufferedWriter.newLine();
}
bufferedWriter.close();
reader.close();
cis.close();
is.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解密文件,返回reader
* @param fileName 文件名字(包含具体路径)
* @return BufferedReader
*/
public BufferedReader decryptFile(String fileName) {
BufferedReader reader = null;
try {
InputStream is = new FileInputStream(fileName);
CipherInputStream cis = new CipherInputStream(is, cipherDecrypt);
reader = new BufferedReader(new InputStreamReader(cis));
}
catch (Exception e) {
e.printStackTrace();
}
return reader;
}
private void initCipher() {
try {
// 加密的cipher
cipherEncrypt = Cipher.getInstance("DES");
cipherEncrypt.init(Cipher.ENCRYPT_MODE, key);
// 解密的cipher
cipherDecrypt = Cipher.getInstance("DES");
cipherDecrypt.init(Cipher.DECRYPT_MODE, key);
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 自定义一个key
*
* @param keyRule 加密的key规则
*/
public void genKey(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");
}
/***
* 测试加密解密
*
* @param args 命令行传入的参数
*/
public static void main(String[] args) {
DESFileEncryption deEncrypt = new DESFileEncryption("STP FILE DE-ENCRYPT");
deEncrypt.encryptFile("D:\\tmp\\T20180913044001.xml", "D:\\tmp\\T20180913044001.bin");
try {
String originMd5 = Md5Encryption.md5(new File("D:\\tmp\\T20180913044001.xml"));
System.out.println("args = [" + originMd5 + "]");
}catch (Exception e){
e.printStackTrace();
}
List list = deEncrypt.decryptFileContent("D:\\tmp\\T20180913044001.bin");
deEncrypt.decryptFileContent("D:\\tmp\\T20180913044001.bin","D:\\tmp\\23333.xml");
try {
String resultMd5 = Md5Encryption.md5(new File("D:\\tmp\\23333.xml"));
System.out.println("args = [" + resultMd5 + "]");
}catch (Exception e){
e.printStackTrace();
}
for (String str : list) {
System.out.println(str);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy