使用java自帶des加密算法實(shí)現(xiàn)文件加密和字符串加密
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 :對(duì)文件進(jìn)行加密
* @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 :對(duì)字符串進(jìn)行加密
* @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 ));
}
}
相關(guān)文章
SpringSecurity自定義AuthenticationProvider無(wú)法@Autowire的解決
這篇文章主要介紹了SpringSecurity自定義AuthenticationProvider無(wú)法@Autowire的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java線程池ForkJoinPool(工作竊取算法)的使用
Fork就是把一個(gè)大任務(wù)切分為若干個(gè)子任務(wù)并行地執(zhí)行,Join就是合并這些子任務(wù)的執(zhí)行結(jié)果,最后得到這個(gè)大任務(wù)的結(jié)果。Fork/Join?框架使用的是工作竊取算法。本文主要介紹了ForkJoinPool的使用,需要的可以參考一下2022-11-11
解決mapper自動(dòng)裝配識(shí)別不了,Could not autowire.No beans&
文章介紹了在使用MyBatisX插件和MybatisPlus自動(dòng)生成代碼后,如何解決Spring Boot項(xiàng)目中自動(dòng)注入`UserMapper`時(shí)報(bào)錯(cuò)的問(wèn)題,主要方法包括在主配置類(lèi)或啟動(dòng)類(lèi)上添加`@MapperScan`注解,指定Mapper文件夾所在的包路徑,以及在Mapper類(lèi)上添加`@Repository`注解2024-11-11
關(guān)于HashMap的put方法執(zhí)行全過(guò)程
這篇文章主要介紹了關(guān)于HashMap的put方法執(zhí)行全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Java服務(wù)如何調(diào)用系統(tǒng)指令、Bat腳本記錄
這篇文章主要介紹了Java服務(wù)如何調(diào)用系統(tǒng)指令、Bat腳本記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Java在運(yùn)行時(shí)識(shí)別類(lèi)型信息的方法詳解
這篇文章主要給大家介紹了關(guān)于Java在運(yùn)行時(shí)識(shí)別類(lèi)型信息的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面來(lái)一起看看吧2019-01-01
Idea實(shí)現(xiàn)接口的方法上無(wú)法添加@Override注解的解決方案
文章介紹了在IDEA中實(shí)現(xiàn)接口方法時(shí)無(wú)法添加@Override注解的問(wèn)題及其解決方法,主要步驟包括更改項(xiàng)目結(jié)構(gòu)中的Language level到支持該注解的版本,以及在pom.xml文件中指定maven-compiler-plugin的版本以解決自動(dòng)更新后的問(wèn)題2025-02-02

