使用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)文章
Spring MVC 攔截器 interceptor 用法詳解
這篇文章主要介紹了Spring MVC 攔截器 interceptor 用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07JAVA實(shí)現(xiàn)社會(huì)統(tǒng)一信用代碼校驗(yàn)的方法
這篇文章主要介紹了JAVA實(shí)現(xiàn)社會(huì)統(tǒng)一信用代碼校驗(yàn)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Java實(shí)現(xiàn)獲取某年某月第一天/最后一天的方法
這篇文章主要介紹了Java實(shí)現(xiàn)獲取某年某月第一天/最后一天的方法,涉及java日期運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2018-02-02MybatisPlus結(jié)合groupby實(shí)現(xiàn)分組和sum求和的步驟
這篇文章主要介紹了MybatisPlus結(jié)合groupby實(shí)現(xiàn)分組和sum求和的步驟,這次使用的是LambdaQueryWrapper,使用QueryWrapper相對(duì)來說簡(jiǎn)單點(diǎn)就不寫了,本文分步驟給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2023-12-12JavaEE中用response向客戶端輸出中文數(shù)據(jù)亂碼問題分析
這篇文章主要介紹了JavaEE中用response向客戶端輸出中文數(shù)據(jù)亂碼問題分析,需要的朋友可以參考下2014-10-10Mybatis調(diào)用SQL?Server存儲(chǔ)過程的實(shí)現(xiàn)示例
在軟件開發(fā)過程中,經(jīng)常會(huì)使用到存儲(chǔ)過程,本文就來介紹一下Mybatis調(diào)用SQL?Server存儲(chǔ)過程的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01