java加密 解密 程式實現

來源:互聯網
上載者:User
 加密解密工具類:package com.mr.util;</p><p>import java.io.FileInputStream;<br />import java.io.FileNotFoundException;<br />import java.io.FileOutputStream;<br />import java.io.IOException;<br />import java.io.InputStream;<br />import java.io.OutputStream;<br />import java.security.*;<br />import javax.crypto.Cipher;<br />import javax.crypto.CipherInputStream;<br />import javax.crypto.CipherOutputStream;<br />import javax.crypto.SecretKeyFactory;<br />import javax.crypto.spec.DESKeySpec;<br />import sun.misc.BASE64Decoder;<br />import sun.misc.BASE64Encoder;</p><p>public abstract class DESCoder {<br />// a weak key<br />private final static BASE64Encoder base64encoder = new BASE64Encoder();<br />private final static BASE64Decoder base64decoder = new BASE64Decoder();<br />private static String encoding = "GBK";<br />public static String sKey = "k34AAE4TAABMAQAA9HcAAIdCAADbRQAA";</p><p>/**<br /> * 加密字串<br /> */<br />public static String ebotongEncrypto(String str) {<br />String result = str;<br />if (str != null && str.length() > 0) {<br />try {<br />byte[] encodeByte = symmetricEncrypto(str.getBytes(encoding));<br />result = base64encoder.encode(encodeByte);</p><p>} catch (Exception e) {<br />e.printStackTrace();<br />}<br />}<br />return result;<br />}</p><p>/**<br /> * 解密字串<br /> */<br />public static String ebotongDecrypto(String str) {<br />String result = str;<br />if (str != null && str.length() > 0) {<br />try {<br />byte[] encodeByte = base64decoder.decodeBuffer(str);</p><p>byte[] decoder = symmetricDecrypto(encodeByte);<br />result = new String(decoder, encoding);<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />}<br />return result;<br />}</p><p>/**<br /> * 加密byte[]<br /> */<br />public static byte[] ebotongEncrypto(byte[] str) {<br />byte[] result = null;<br />if (str != null && str.length > 0) {<br />try {<br />byte[] encodeByte = symmetricEncrypto(str);<br />result = base64encoder.encode(encodeByte).getBytes();</p><p>} catch (Exception e) {<br />e.printStackTrace();<br />}<br />}<br />return result;<br />}</p><p>/**<br /> * 解密byte[]<br /> */<br />public static byte[] ebotongDecrypto(byte[] str) {<br />byte[] result = null;<br />if (str != null && str.length > 0) {<br />try {</p><p>byte[] encodeByte = base64decoder.decodeBuffer(new String(str,<br />encoding));<br />// byte[] encodeByte = base64decoder.decodeBuffer(new<br />// String(str));<br />byte[] decoder = symmetricDecrypto(encodeByte);<br />result = new String(decoder).getBytes(encoding);<br />result = new String(decoder).getBytes();<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />}<br />return result;<br />}</p><p>/**<br /> * 對稱式加密位元組數組並返回<br /> *<br /> * @param byteSource<br /> * 需要加密的資料<br /> * @return 經過加密的資料<br /> * @throws Exception<br /> */<br />public static byte[] symmetricEncrypto(byte[] byteSource) throws Exception {<br />try {<br />int mode = Cipher.ENCRYPT_MODE;<br />SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");<br />byte[] keyData = sKey.getBytes();<br />DESKeySpec keySpec = new DESKeySpec(keyData);<br />Key key = keyFactory.generateSecret(keySpec);<br />Cipher cipher = Cipher.getInstance("DES");<br />cipher.init(mode, key);</p><p>byte[] result = cipher.doFinal(byteSource);<br />return result;<br />} catch (Exception e) {<br />throw e;<br />} finally {<br />}<br />}</p><p>/**<br /> * 對稱解密位元組數組並返回<br /> *<br /> * @param byteSource<br /> * 需要解密的資料<br /> * @return 經過解密的資料<br /> * @throws Exception<br /> */<br />public static byte[] symmetricDecrypto(byte[] byteSource) throws Exception {<br />try {<br />int mode = Cipher.DECRYPT_MODE;<br />SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");<br />byte[] keyData = sKey.getBytes();<br />DESKeySpec keySpec = new DESKeySpec(keyData);<br />Key key = keyFactory.generateSecret(keySpec);<br />Cipher cipher = Cipher.getInstance("DES");<br />cipher.init(mode, key);<br />byte[] result = cipher.doFinal(byteSource);<br />return result;<br />} catch (Exception e) {<br />throw e;<br />} finally {</p><p>}<br />}</p><p>/**<br /> * 散列演算法<br /> *<br /> * @param byteSource<br /> * 需要散列計算的資料<br /> * @return 經過散列計算的資料<br /> * @throws Exception<br /> */<br />public static byte[] hashMethod(byte[] byteSource) throws Exception {<br />try {<br />MessageDigest currentAlgorithm = MessageDigest.getInstance("SHA-1");<br />currentAlgorithm.reset();<br />currentAlgorithm.update(byteSource);<br />return currentAlgorithm.digest();<br />} catch (Exception e) {<br />throw e;<br />}<br />}</p><p>/**<br /> * 對檔案srcFile進行加密輸出到檔案distFile<br /> *<br /> * @param srcFile<br /> * 明文檔案<br /> * @param distFile<br /> * 加密後的檔案<br /> * @throws Exception<br /> */<br />public static void EncryptFile(String srcFile, String distFile)<br />throws Exception {</p><p>InputStream is = null;<br />OutputStream out = null;<br />CipherInputStream cis = null;<br />try {<br />int mode = Cipher.ENCRYPT_MODE;<br />SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");<br />byte[] keyData = sKey.getBytes();<br />DESKeySpec keySpec = new DESKeySpec(keyData);<br />Key key = keyFactory.generateSecret(keySpec);<br />Cipher cipher = Cipher.getInstance("DES");<br />cipher.init(mode, key);<br />is = new FileInputStream(srcFile);<br />out = new FileOutputStream(distFile);<br />cis = new CipherInputStream(is, cipher);<br />byte[] buffer = new byte[1024];<br />int r;<br />while ((r = cis.read(buffer)) > 0) {<br />out.write(buffer, 0, r);<br />}<br />} catch (Exception e) {<br />throw e;<br />} finally {<br />cis.close();<br />is.close();<br />out.close();<br />}<br />}</p><p>/**<br /> * 解密檔案srcFile到目標檔案distFile<br /> *<br /> * @param srcFile<br /> * 密文檔案<br /> * @param distFile<br /> * 解密後的檔案<br /> * @throws Exception<br /> */<br />public static void DecryptFile(String srcFile, String distFile)<br />throws Exception {</p><p>InputStream is = null;<br />OutputStream out = null;<br />CipherOutputStream cos = null;<br />try {<br />int mode = Cipher.DECRYPT_MODE;<br />SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");<br />byte[] keyData = sKey.getBytes();<br />DESKeySpec keySpec = new DESKeySpec(keyData);<br />Key key = keyFactory.generateSecret(keySpec);<br />Cipher cipher = Cipher.getInstance("DES");<br />cipher.init(mode, key);<br />byte[] buffer = new byte[1024];<br />is = new FileInputStream(srcFile);<br />out = new FileOutputStream(distFile);<br />cos = new CipherOutputStream(out, cipher);</p><p>int r;<br />while ((r = is.read(buffer)) >= 0) {<br />cos.write(buffer, 0, r);<br />}</p><p>} catch (Exception e) {<br />throw e;<br />} finally {<br />cos.close();<br />is.close();<br />out.close();<br />}<br />}</p><p>/**<br /> * 對檔案進行加密64位編碼<br /> *<br /> * @param srcFile<br /> * 源檔案<br /> * @param distFile<br /> * 目標檔案<br /> */<br />public static void BASE64EncoderFile(String srcFile, String distFile) {<br />InputStream inputStream = null;<br />OutputStream out = null;<br />try {<br />inputStream = new FileInputStream(srcFile);</p><p>out = new FileOutputStream(distFile);<br />byte[] buffer = new byte[1024];<br />while (inputStream.read(buffer) > 0) {<br />out.write(ebotongEncrypto(buffer));<br />}<br />} catch (FileNotFoundException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />} catch (IOException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />} finally {<br />try {<br />out.close();<br />inputStream.close();<br />} catch (IOException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />}</p><p>}</p><p>}</p><p>/**<br /> * 對檔案進行解密64位解碼<br /> *<br /> * @param srcFile<br /> * 源檔案<br /> * @param distFile<br /> * 目標檔案<br /> */<br />public static void BASE64DecoderFile(String srcFile, String distFile) {<br />InputStream inputStream = null;<br />OutputStream out = null;<br />try {<br />inputStream = new FileInputStream(srcFile);</p><p>out = new FileOutputStream(distFile);<br />byte[] buffer = new byte[1412];</p><p>while (inputStream.read(buffer) > 0) {<br />out.write(ebotongDecrypto(buffer));<br />}<br />} catch (FileNotFoundException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />} catch (IOException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />} finally {<br />try {<br />out.close();<br />inputStream.close();<br />} catch (IOException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />}</p><p>}<br />}</p><p>}</p><p>// 本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/Harry_hua/archive/2010/12/02/6050364.aspx例子:對檔案加密(針對檔案加密,而非檔案內容):

 

public static void encode() {</p><p>File file = new File(fatherDirecoryName + zipDirecoryName);<br />// 判斷檔案夾是否存在,如果不存在則建立檔案夾<br />if (!file.exists()) {<br />file.mkdir();<br />System.out.println("zip 檔案建立");<br />} else {<br />System.out.println("zip 檔案存在,");<br />file.delete();<br />System.out.print("檔案刪除,");<br />file.mkdir();<br />System.out.print("檔案建立");<br />}<br />try {<br />DESCoder.EncryptFile(fatherDirecoryName + srcDirecoryName<br />+ info_file_name_meijiami, fatherDirecoryName<br />+ zipDirecoryName + info_file_name_jiami);<br />DESCoder.EncryptFile(fatherDirecoryName + srcDirecoryName<br />+ input_file_name_meijiami, fatherDirecoryName<br />+ zipDirecoryName + input_file_name_jiami);<br />DESCoder.EncryptFile(fatherDirecoryName + srcDirecoryName<br />+ output_file_name_meijiami, fatherDirecoryName<br />+ zipDirecoryName + output_file_name_jiami);<br />DESCoder.EncryptFile(fatherDirecoryName + srcDirecoryName<br />+ all_table_file_name_meijiami, fatherDirecoryName<br />+ zipDirecoryName + all_table_file_name_jiami);<br />} catch (Exception e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />}<br />}

例子:對檔案解密(針對檔案解密,而非檔案內容):

 public static void decode() {<br />File file = new File(fatherDirecoryName + srcDirecoryName);<br />// 判斷檔案夾是否存在,如果不存在則建立檔案夾<br />if (!file.exists()) {<br />file.mkdir();<br />System.out.println("src 檔案建立");<br />} else {<br />System.out.println("src 檔案存在,");<br />file.delete();<br />System.out.print("檔案刪除,");<br />file.mkdir();<br />System.out.print("檔案建立");<br />}</p><p>try {<br />DESCoder.DecryptFile(fatherDirecoryName + zipDirecoryName<br />+ info_file_name_jiami, fatherDirecoryName<br />+ srcDirecoryName + info_file_name_meijiami);<br />DESCoder.DecryptFile(fatherDirecoryName + zipDirecoryName<br />+ input_file_name_jiami, fatherDirecoryName<br />+ srcDirecoryName + input_file_name_meijiami);<br />DESCoder.DecryptFile(fatherDirecoryName + zipDirecoryName<br />+ output_file_name_jiami, fatherDirecoryName<br />+ srcDirecoryName + output_file_name_meijiami);<br />DESCoder.DecryptFile(fatherDirecoryName + zipDirecoryName<br />+ all_table_file_name_jiami, fatherDirecoryName<br />+ srcDirecoryName + all_table_file_name_meijiami);<br />} catch (Exception e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />}<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.