日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

字节数组java加密与解密

發布時間:2023/12/9 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字节数组java加密与解密 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這兩天一直在查找字節數組之類的問題,今天正好有機會和大家共享一下.

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數組轉換為表現16進制的字符串* @param arrB 須要轉換的byte數組* @return 16進制表現的字符串* @throws Exception*/public static String byteArr2HexStr(byte[] arrB) throws Exception{int bLen = arrB.length;//每一個字符占用兩個字節,所以字符串的度長需是數組度長的2倍StringBuffer strBuffer = new StringBuffer(bLen*2);for(int i=0; i != bLen; ++i){int intTmp = arrB[i];//把正數轉化為正數while(intTmp < 0){intTmp = intTmp + 256;//因為字一個字節是8位,從低往高數,第9位為符號為,加256,相當于在第九位加1}//小于0F的數據須要在后面補0,(因為原來是一個字節,在現成變String是兩個字節,如果小于0F的話,明說大最也盛不滿第一個字節。第二個需彌補0)if(intTmp < 16){strBuffer.append("0");}strBuffer.append(Integer.toString(intTmp,16));}return strBuffer.toString();}/*** 將表現16進制的字符串轉化為byte數組* @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);}/*** 加密字節數組* @param arrB 需加密的字節數組* @return 加密后的字節數組* @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()));}/*** 密解字節數組* @param arrB 需密解的字節數組* @return 密解后的字節數組* @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 "";}}/*** 從指定字符串生成密匙,密匙所需的字節數組度長為8位,缺乏8位時,面后補0,超越8位時,只取后面8位* @param arrBTmp 成構字符串的字節數組* @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 = "";//楚清老數據oldStr = des.decrypt(newStr);System.out.println("密解后: "+oldStr);} }

????輸出信息:

????加密后: ? d59c46653b72a6248e03aa55a8fdad6c
密解后: ?wangfeng

文章結束給大家分享下程序員的一些笑話語錄: 關于編程語言
如果 C++是一把錘子的話,那么編程就會變成大手指頭。
如果你找了一百萬只猴子來敲打一百萬個鍵盤,那么會有一只猴子會敲出一 段 Java 程序,而其余的只會敲出 Perl 程序。
一陣急促的敲門聲,“誰啊!”,過了 5 分鐘,門外傳來“Java”。
如果說 Java 很不錯是因為它可以運行在所有的操作系統上,那么就可以說 肛交很不錯,因為其可以使用于所有的性別上。

轉載于:https://www.cnblogs.com/xinyuyuanm/archive/2013/04/28/3049893.html

總結

以上是生活随笔為你收集整理的字节数组java加密与解密的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。