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 sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; public class DESUtil {
Key key ; public DESUtil() { } public DESUtil(String str) {
setKey(str); // 生成密匙
} public Key getKey() {
return key ;
} public void setKey(Key key) {
this . key = key;
} // /**
// * 根据参数生成 KEY
// */
// public void setKey(String strKey) {
// try {
// KeyGenerator _generator = KeyGenerator.getInstance ( "DES" );
// _generator.init( new SecureRandom(strKey.getBytes()));
// this . key = _generator.generateKey();
// _generator = null ;
// } catch (Exception e) {
// throw new RuntimeException(
// "Error initializing SqlMap class. Cause: " + e);
// }
// } /**
* 根据参数生成 KEY
*/
public void setKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance ( "DES" );
_generator.init( new SecureRandom(strKey.getBytes("utf-8")));
SecureRandom secureRandom=
SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(strKey.getBytes());
_generator.init(56, secureRandom);
this . key = _generator.generateKey();
_generator = null ;
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
}
} /**
* 加密 String 明文输入 ,String 密文输出
*/
public String encryptStr(String strMing) {
byte [] byteMi = null ;
byte [] byteMing = null ;
String strMi = "" ;
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes( "UTF-8" );
byteMi = this .encryptByte(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
base64en = null ;
byteMing = null ;
byteMi = null ;
}
return strMi;
} /**
* 解密 以 String 密文输入 ,String 明文输出
*
* @param strMi
* @return
*/
public String decryptStr(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte [] byteMing = null ;
byte [] byteMi = null ;
String strMing = "" ;
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this .decryptByte(byteMi);
strMing = new String(byteMing, "UTF-8" );
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
base64De = null ;
byteMing = null ;
byteMi = null ;
}
return strMing;
} /**
* 加密以 byte[] 明文输入 ,byte[] 密文输出
*
* @param byteS
* @return
*/
private byte [] encryptByte( byte [] byteS) {
byte [] byteFina = null ;
Cipher cipher;
try {
cipher = Cipher.getInstance ( "DES" );
cipher.init(Cipher. ENCRYPT_MODE , key );
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null ;
}
return byteFina;
} /**
* 解密以 byte[] 密文输入 , 以 byte[] 明文输出
*
* @param byteD
* @return
*/
private byte [] decryptByte( byte [] byteD) {
Cipher cipher;
byte [] byteFina = null ;
try {
cipher = Cipher.getInstance ( "DES" );
cipher.init(Cipher. DECRYPT_MODE , key );
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null ;
}
return byteFina;
} /**
* 文件 file 进行加密并保存目标文件 destFile 中
*
* @param file
* 要加密的文件 如 c:/test/srcFile.txt
* @param destFile
* 加密后存放的文件名 如 c:/ 加密后文件 .txt
*/
public void encryptFile(String file, String destFile) throws Exception {
Cipher cipher = Cipher.getInstance ( "DES" );
// cipher.init(Cipher.ENCRYPT_MODE, getKey());
cipher.init(Cipher. ENCRYPT_MODE , this . key );
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte [] buffer = new byte [1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
} /**
* 文件采用 DES 算法解密文件
*
* @param file
* 已加密的文件 如 c:/ 加密后文件 .txt *
* @param destFile
* 解密后存放的文件名 如 c:/ test/ 解密后文件 .txt
*/
public void decryptFile(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance ( "DES" );
cipher.init(Cipher. DECRYPT_MODE , this . key );
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte [] buffer = new byte [1024];
int r;
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
} public static void main(String[] args) throws Exception {
DESUtil des = new DESUtil("jrkj123");
// DES 加密文件
// des.encryptFile("G:/test.doc", "G:/ 加密 test.doc");
// DES 解密文件
// des.decryptFile("G:/ 加密 test.doc", "G:/ 解密 test.doc");
String str1 = "taskId=68&correctDate=2015-04-13&flag=1" ;
// DES 加密字符串
String str2 = des.encryptStr(str1);
// DES 解密字符串
String deStr = des.decryptStr(str2);
System. out .println( " 加密前: " + str1);
System. out .println( " 加密后: " + str2);
System. out .println( " 解密后: " + deStr);
}
}

Java-DES算法加密解密工具类的更多相关文章

  1. Base64加密解密工具类

    使用Apache commons codec类Base64进行加密解密 maven依赖 <dependency> <groupId>commons-codec</grou ...

  2. 加密解密工具类(Java,DES)

    一个Java版的DES加密工具类,能够用来进行网络传输数据加密,保存password的时候进行加密. import java.security.Key; import java.security.sp ...

  3. .Net(c#)加密解密工具类:

    /// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelper { #region des实 ...

  4. java 加密解密工具类(实用!!!)

    最近发现了一个加密解密的好例子,很方便使用,可以作为平时开发的工具集,记录一下. package com.sh.springboottdemo2.util; import com.sun.org.ap ...

  5. java加密解密工具类

    package com.founder.mrp.util; import java.nio.charset.StandardCharsets; import java.security.Key; im ...

  6. des 加密解密工具类

    最近在做des的双对称加密解密,特此记录一下. des对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码 ...

  7. AES加密解密工具类封装(AESUtil)

    package club.codeapes.common.utils; import org.springframework.util.Base64Utils; import javax.crypto ...

  8. base加密解密工具类

    public class MLDUtil { public static Key DEFAULT_KEY = null; public static final String DEFAULT_SECR ...

  9. java中加密解密工具类

    在工作中经常遇到需要加密.解密的场景.例如用户的手机号等信息,在保存到数据库的过程中,需要对数据进行加密.取出时进行解密. public class DEStool { private String ...

随机推荐

  1. 经纪xx系统节点VIP案例介绍和深入分析异常

    系统环境    硬件平台 &  操作 IBM 570 操作系统版本号  AIX 5.3 物理内存  32G Oracle 产品及版本号  10.2.0.5 RAC 业务类型  OLTP 背 ...

  2. hdu 4912 Paths on the tree(树链拆分+贪婪)

    题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LC ...

  3. Spark第一个研究笔记1一片 - Spark一个简短的引论

    该公司推出的在线项目Spark拥有近1随着时间的推移.有效,Spark事实上,优秀的分布式计算平台,以提高生产力. 开始本篇笔记.此前的研究会Spark研究报告共享出来(由于篇幅的限制,它将被划分成制 ...

  4. 基于ORACLE建表和循环回路来创建数据库存储过程SQL语句来实现

    一个.概要 在实际的软件开发项目.我们经常会遇到需要创造更多的相同类型的数据库表或存储过程时,.例如.假设按照尾号点表的ID号,然后,你需要创建10用户信息表,的用户信息放在同一个表中. 对于类型同样 ...

  5. Caused by: Unable to locate parent package [json-package] for [class com.you.action.ColumnAction] -

    1.错误叙述性说明 三月 15, 2015 7:53:25 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error 严重: Dispat ...

  6. Java下获取可用CPU数

    1.获取cpu核心数:Runtime.getRuntime().availableProcessors();创建线程池:Executors.newFixedThreadPool(nThreads);/ ...

  7. 工作介绍xml书包文件

    光开放平台一个非常重要的特点就是简化了对xml文件的操作,您能非常轻松地引入xml文件.定位到随意节点.增删属性和文本以及节点本身,以下咱们用实例来介绍对xml的操作 引入xml文件: <cht ...

  8. 排序(4)---------希尔(shell)排序(C语言实现)

    由于考试耽搁了几天,不好意思~~~ 前面的介绍的三种排序算法,都属于简单排序,大家能够看下详细算法,时间复杂度基本都在0(n^2),这样呢,非常多计算机界.数学界的牛人就非常不爽了,他们在家里想啊想, ...

  9. ubuntu分屏终端

    一:更新source 列表: 将/etc/apt/sources.list的镜像源改为网易源:http://mirrors.163.com/ubuntu/dists/ deb http://mirro ...

  10. 我国常用的坐标系统WKID列表[转]

    原文链接:http://blog.sina.com.cn/s/blog_62f9ffcd0102uw8x.html Geographic Coordinate System 地理坐标 4214  GC ...