package com.itaoo.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DESCoder {
// a weak key
private static String encoding = "UTF-8";
// 密钥
private String sKey = "";
public DESCoder(String sKey) {
this.sKey = sKey;
}
/**
* 加密字符串
*/
public String ebotongEncrypto(String str) {
String result = str;
if (str != null && str.length() > 0) {
try {
byte[] encodeByte = symmetricEncrypto(str.getBytes(encoding));
result = Base64.encode(encodeByte);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 解密字符串
*/
public String ebotongDecrypto(String str) {
String result = str;
if (str != null && str.length() > 0) {
try {
byte[] encodeByte = Base64.decode(str);
byte[] decoder = symmetricDecrypto(encodeByte);
result = new String(decoder, encoding);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 加密byte[]
*/
public byte[] ebotongEncrypto(byte[] str) {
byte[] result = null;
if (str != null && str.length > 0) {
try {
byte[] encodeByte = symmetricEncrypto(str);
result = Base64.encode(encodeByte).getBytes();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 解密byte[]
*/
public byte[] ebotongDecrypto(byte[] str) {
byte[] result = null;
if (str != null && str.length > 0) {
try {
byte[] encodeByte = Base64.decode(new String(str, encoding));
//byte[] encodeByte = base64decoder.decodeBuffer(new String(str));
byte[] decoder = symmetricDecrypto(encodeByte);
result = new String(decoder).getBytes(encoding);
result = new String(decoder).getBytes();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 对称加密字节数组并返回
*
* @param byteSource 需要加密的数据
* @return 经过加密的数据
* @throws Exception
*/
public byte[] symmetricEncrypto(byte[] byteSource) throws Exception {
try {
int mode = Cipher.ENCRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = sKey.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
byte[] result = cipher.doFinal(byteSource);
return result;
} catch (Exception e) {
throw e;
} finally {
}
}
/**
* 对称解密字节数组并返回
*
* @param byteSource 需要解密的数据
* @return 经过解密的数据
* @throws Exception
*/
public byte[] symmetricDecrypto(byte[] byteSource) throws Exception {
try {
int mode = Cipher.DECRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = sKey.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
byte[] result = cipher.doFinal(byteSource);
return result;
} catch (Exception e) {
throw e;
} finally {
}
}
/**
* 散列算法
*
* @param byteSource
* 需要散列计算的数据
* @return 经过散列计算的数据
* @throws Exception
*/
public static byte[] hashMethod(byte[] byteSource) throws Exception {
try {
MessageDigest currentAlgorithm = MessageDigest.getInstance("SHA-1");
currentAlgorithm.reset();
currentAlgorithm.update(byteSource);
return currentAlgorithm.digest();
} catch (Exception e) {
throw e;
}
}
/**
* 对文件srcFile进行加密输出到文件distFile
* @param srcFile 明文文件
* @param distFile 加密后的文件
* @throws Exception
*/
public void EncryptFile(String srcFile,String distFile) throws Exception{
InputStream is=null;
OutputStream out = null;
CipherInputStream cis =null;
try {
int mode = Cipher.ENCRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = sKey.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
is= new FileInputStream(srcFile);
out = new FileOutputStream(distFile);
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) {
throw e;
} finally {
cis.close();
is.close();
out.close();
}
}
/**
* 解密文件srcFile到目标文件distFile
* @param srcFile 密文文件
* @param distFile 解密后的文件
* @throws Exception
*/
public void DecryptFile(String srcFile,String distFile) throws Exception{
InputStream is=null;
OutputStream out = null;
CipherOutputStream cos =null;
try {
int mode = Cipher.DECRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = sKey.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
byte[] buffer = new byte[1024];
is= new FileInputStream(srcFile);
out = new FileOutputStream(distFile);
cos = new CipherOutputStream(out,cipher);
int r;
while((r=is.read(buffer))>=0){
cos.write(buffer, 0, r);
}
} catch (Exception e) {
throw e;
} finally {
cos.close();
is.close();
out.close();
}
}
/**
* 对文件进行加密64位编码
* @param srcFile 源文件
* @param distFile 目标文件
*/
public void BASE64EncoderFile(String srcFile,String distFile){
InputStream inputStream =null;
OutputStream out = null;
try {
inputStream = new FileInputStream(srcFile);
out = new FileOutputStream(distFile);
byte[] buffer = new byte[1024];
while(inputStream.read(buffer)>0){
out.write(ebotongEncrypto(buffer));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
out.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 对文件进行解密64位解码
* @param srcFile 源文件
* @param distFile 目标文件
*/
public void BASE64DecoderFile(String srcFile,String distFile){
InputStream inputStream =null;
OutputStream out = null;
try {
inputStream = new FileInputStream(srcFile);
out = new FileOutputStream(distFile);
byte[] buffer = new byte[1412];
while(inputStream.read(buffer)>0){
out.write(ebotongDecrypto(buffer));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
out.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
分享到:
相关推荐
VB实现DES加密解密算法已测试通过如有更新另行发布
DES加密解密算法的C语言实现,只要调用函数,即可实现数据的加密解密,我已经在DSP上实现。
sun java 3DES加密解密算法
DES加密解密算法 C语言源代码,STM32 51单片机可用。提高产品的安全性,RAM占用小。 /* DES加密,binput:明文,boutput:密文, bkey:密钥 */
加密为直接输入十六进制的(消息,密钥,1);解密为(密文,密钥,2);Index=1为加密,不为1为解密
根据DES加密标准,用C++设计编写符合DES算法思想的加、解密程序,实现对字符串和数组的加密和解密。
Android Des加密解密算法
DES对于文件的加密解密,采用CBC-DES模式编写。可联系maibox_krj@163.com获取。
这个可以用做与android php iOS 三个平台,des加解密,支持中文 英文,长字符串,又问题,欢迎留言
DES加密与解密算法实现源代码,是我们上课时做的作业
DES加密解密算法的C++实现,可以直接编译运行。
DES加密算法VC源码,只包含了一个源文件,自己在VC 中建工程即可编译,代码中有详细的注释,适于学习
基于STM32的软件加解密算法,包括DES,3DES的ECB,CBC模式。但是验证时CBC模式的初始向量为0时,数据的...已上传新的验证代码:DES_3DES加解密算法(更新),如有需要请自行在本人上传资源中查找,如有测试不过的请私信。
DES加密解密算法,要的自行下载。
DES算法加密解密1M文件,并计算加密和解密时间
使用DES算法加密、解密文本、文件,十六进制显示。 提供源代码。 缺点:没有处理文件末尾。
DES加密解密算法
des加解密算法,可加解密文件,及内存 非常好用。
C++写的DES加密解密算法,里面包含了对txt文本文件的加密,支持分组加密.是做课程设计的时候做的,难度不大.希望对做课程设计的朋友有帮助
DES加密解密算法,供学习使用,没有密码