字节数组java加密与解密
生活随笔
收集整理的這篇文章主要介紹了
字节数组java加密与解密
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這兩天一直在查找字節(jié)數(shù)組之類的問題,今天正好有機會和大家共享一下.
package com.wf.security;import java.security.Key; import java.security.Security;import javax.crypto.Cipher;/*** 加密密解類* @author wangfeng* @since 2013-4-27 15:50:26* @version 1.0**/ public class EncryptionDecryption {private static String strDefaultKey = "wfkey";/** 加密具工 */private Cipher encryptCipher = null;/** 密解具工 */private Cipher decryptCipher = null;/*** 將byte數(shù)組轉換為表現(xiàn)16進制的字符串* @param arrB 須要轉換的byte數(shù)組* @return 16進制表現(xiàn)的字符串* @throws Exception*/public static String byteArr2HexStr(byte[] arrB) throws Exception{int bLen = arrB.length;//每一個字符占用兩個字節(jié),所以字符串的度長需是數(shù)組度長的2倍StringBuffer strBuffer = new StringBuffer(bLen*2);for(int i=0; i != bLen; ++i){int intTmp = arrB[i];//把正數(shù)轉化為正數(shù)while(intTmp < 0){intTmp = intTmp + 256;//因為字一個字節(jié)是8位,從低往高數(shù),第9位為符號為,加256,相當于在第九位加1}//小于0F的數(shù)據須要在后面補0,(因為原來是一個字節(jié),在現(xiàn)成變String是兩個字節(jié),如果小于0F的話,明說大最也盛不滿第一個字節(jié)。第二個需彌補0)if(intTmp < 16){strBuffer.append("0");}strBuffer.append(Integer.toString(intTmp,16));}return strBuffer.toString();}/*** 將表現(xiàn)16進制的字符串轉化為byte數(shù)組* @param hexStr* @return* @throws Exception*/public static byte[] hexStr2ByteArr(String hexStr) throws Exception{byte[] arrB = hexStr.getBytes();int bLen = arrB.length;byte[] arrOut = new byte[bLen/2];for(int i=0; i<bLen; i = i+2){String strTmp = new String(arrB,i,2);arrOut[i/2] = (byte)Integer.parseInt(strTmp,16);}return arrOut;}/*** 認默構造器,應用認默密匙* @throws Exception*/public EncryptionDecryption() throws Exception {this(strDefaultKey);}/*** 指定密匙構造方法* @param strKey 指定的密匙* @throws Exception*/@SuppressWarnings("restriction")public EncryptionDecryption(String strKey) throws Exception {Security.addProvider(new com.sun.crypto.provider.SunJCE());Key key = getKey(strKey.getBytes());encryptCipher = Cipher.getInstance("DES");encryptCipher.init(Cipher.ENCRYPT_MODE, key);decryptCipher = Cipher.getInstance("DES");decryptCipher.init(Cipher.DECRYPT_MODE, key);}/*** 加密字節(jié)數(shù)組* @param arrB 需加密的字節(jié)數(shù)組* @return 加密后的字節(jié)數(shù)組* @throws Exception*/public byte[] encrypt(byte[] arrB) throws Exception{return encryptCipher.doFinal(arrB);}/*** 加密字符串* @param strIn 需加密的字符串* @return 加密后的字符串* @throws Exception*/public String encrypt(String strIn) throws Exception{return byteArr2HexStr(encrypt(strIn.getBytes()));}/*** 密解字節(jié)數(shù)組* @param arrB 需密解的字節(jié)數(shù)組* @return 密解后的字節(jié)數(shù)組* @throws Exception*/public byte[] decrypt(byte[] arrB) throws Exception{return decryptCipher.doFinal(arrB);}/*** 密解字符串* @param strIn 需密解的字符串* @return 密解后的字符串* @throws Exception*/public String decrypt(String strIn) throws Exception{try{return new String(decrypt(hexStr2ByteArr(strIn)));}catch (Exception e) {return "";}}/*** 從指定字符串生成密匙,密匙所需的字節(jié)數(shù)組度長為8位,缺乏8位時,面后補0,超越8位時,只取后面8位* @param arrBTmp 成構字符串的字節(jié)數(shù)組* @return 生成的密匙* @throws Exception*/private Key getKey(byte[] arrBTmp) throws Exception{byte[] arrB = new byte[8]; //認默為0for(int i=0; i<arrBTmp.length && i < arrB.length; ++i){arrB[i] = arrBTmp[i];}//生成密匙Key key = new javax.crypto.spec.SecretKeySpec(arrB,"DES");return key;}} 每日一道理嶺上嬌艷的鮮花,怎敵她美麗的容顏?山間清澈的小溪,怎比她純潔的心靈?
????這里用DES算法,SUN還供提了別的算法。這里只是其中一種。
????測試代碼:
package com.wf.test;import org.junit.Test;import com.wf.security.EncryptionDecryption;public class EncryptionTest {@Testpublic void test() throws Exception{EncryptionDecryption des = new EncryptionDecryption("wf");String oldStr = "wangfeng";String newStr = "";newStr = des.encrypt(oldStr);System.out.println("加密后: "+newStr);oldStr = "";//楚清老數(shù)據oldStr = des.decrypt(newStr);System.out.println("密解后: "+oldStr);} }????輸出信息:
????加密后: ? d59c46653b72a6248e03aa55a8fdad6c
密解后: ?wangfeng
文章結束給大家分享下程序員的一些笑話語錄: 關于編程語言
如果 C++是一把錘子的話,那么編程就會變成大手指頭。
如果你找了一百萬只猴子來敲打一百萬個鍵盤,那么會有一只猴子會敲出一 段 Java 程序,而其余的只會敲出 Perl 程序。
一陣急促的敲門聲,“誰啊!”,過了 5 分鐘,門外傳來“Java”。
如果說 Java 很不錯是因為它可以運行在所有的操作系統(tǒng)上,那么就可以說 肛交很不錯,因為其可以使用于所有的性別上。
轉載于:https://www.cnblogs.com/xinyuyuanm/archive/2013/04/28/3049893.html
總結
以上是生活随笔為你收集整理的字节数组java加密与解密的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: usb serial port 驱动_t
- 下一篇: macbookpro接口叫什么_【科普】