可对内容、文件进行加密
2011-04-07 17:46
501 查看
package com.ahua.secret; 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; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public abstract class DESCoder { // a weak key private final static BASE64Encoder base64encoder = new BASE64Encoder(); private final static BASE64Decoder base64decoder = new BASE64Decoder(); private static String encoding = "GBK"; public static String sKey = "k34AAE4TAABMAQAA9HcAAIdCAADbRQAA"; /** * 加密字符串 */ public static String ebotongEncrypto(String str) { String result = str; if (str != null && str.length() > 0) { try { byte[] encodeByte = symmetricEncrypto(str.getBytes(encoding)); result = base64encoder.encode(encodeByte); } catch (Exception e) { e.printStackTrace(); } } return result; } /** * 解密字符串 */ public static String ebotongDecrypto(String str) { String result = str; if (str != null && str.length() > 0) { try { byte[] encodeByte = base64decoder.decodeBuffer(str); byte[] decoder = symmetricDecrypto(encodeByte); result = new String(decoder, encoding); } catch (Exception e) { e.printStackTrace(); } } return result; } /** * 加密byte[] */ public static byte[] ebotongEncrypto(byte[] str) { byte[] result = null; if (str != null && str.length > 0) { try { byte[] encodeByte = symmetricEncrypto(str); result = base64encoder.encode(encodeByte).getBytes(); } catch (Exception e) { e.printStackTrace(); } } return result; } /** * 解密byte[] */ public static byte[] ebotongDecrypto(byte[] str) { byte[] result = null; if (str != null && str.length > 0) { try { byte[] encodeByte = base64decoder.decodeBuffer(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 static 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 static 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 static 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 static 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 static 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 static 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(); } } } } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Harry_hua/archive/2010/12/02/6050364.aspx
相关文章推荐
- 【开发技术】对文件内容进行加密-java
- 对文件内容进行加密
- 117 用C语言对数据或文件内容进行加密
- 用C语言对数据或文件内容进行加密
- 关于配置文件内容加密处理的感想(二)
- 采用文件逐字节与密码异或方式对文件进行加密
- Android 存储文件方式之一---SharedPreferences 内容提供者,以xml 的方式进行数据 存储。是一种轻量级的文件数据存储
- JAVA采用DES算法进行加密解密文件
- 使用Gnupg对Linux系统中的文件进行加密
- 对WinForm的App.config文件进行加密
- Windows XP 里如何对文件进行加密
- 对WinForm的App.config文件进行加密 转自 http://www.cnblogs.com/Jinjian/archive/2006/09/06/496009.html
- 在Windows XP中对文件进行加密
- 基于ASM开发的一个关于class文件加密程序,可对整个jar进行加密且不影响资源文件
- 给dwg文件进行加密编辑的方法
- 如何先搜索文件然后在文件中搜索具体内容进行输出?
- 关于配置文件内容加密处理的感想
- 大文件加密(实现在文件流中进行加密解密的方法)
- MS SQL Server 对文件内容进行全文检索的查询
- 对文件进行加密