使用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 ));
}
}
相关文章
SpringSecurity自定义AuthenticationProvider无法@Autowire的解决
这篇文章主要介绍了SpringSecurity自定义AuthenticationProvider无法@Autowire的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-12-12
Java线程池ForkJoinPool(工作窃取算法)的使用
Fork就是把一个大任务切分为若干个子任务并行地执行,Join就是合并这些子任务的执行结果,最后得到这个大任务的结果。Fork/Join 框架使用的是工作窃取算法。本文主要介绍了ForkJoinPool的使用,需要的可以参考一下2022-11-11
解决mapper自动装配识别不了,Could not autowire.No beans&
文章介绍了在使用MyBatisX插件和MybatisPlus自动生成代码后,如何解决Spring Boot项目中自动注入`UserMapper`时报错的问题,主要方法包括在主配置类或启动类上添加`@MapperScan`注解,指定Mapper文件夹所在的包路径,以及在Mapper类上添加`@Repository`注解2024-11-11
Idea实现接口的方法上无法添加@Override注解的解决方案
文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Language level到支持该注解的版本,以及在pom.xml文件中指定maven-compiler-plugin的版本以解决自动更新后的问题2025-02-02


最新评论