java,cipher
java cipher是什麼,讓我們一起了解一下?
cipher是在javax.crypto包下,構成了Java Cryptographic Extension (JCE) 框架的核心,Java的Cipher類提供了加密和解密的功能。
我們都知道,Cipher類是一個引擎類,它需要通過getInstance()工廠方法來實例化對象。那麼該如何操作?
1、我們可以通過指定轉換模式的方式獲得實例化對象,方法如下所示:
// 返回實現指定轉換的 Cipher對象
public static Cipher getInstance(String transformation)
2、也可以在制定轉換模式的同時制定該轉換模式的提供者,方法如下所示:
// 返回實現指定轉換的 Cipher對象
public static Cipher getInstance(String transformation, Provider provider)
// 返回實現指定轉換的 Cipher對象
public static Cipher getInstance(String transformation, String provider)
注意這裏的參數String transformation,通過如下代碼示例:
Cipher c = Cipher.getInstance("DES");
上述實例化操作是一種最爲簡單的實現,並沒有考慮DES分組算法的工作模式和填充模式,可通過以下方式對其設定:
Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
參數String transformation的格式是“算法/工作模式/填充模式”,不同的算法支持不同的工作模式以及填充模式。
另外,Java的Cipher類還提供了加密和解密的功能,那麼JAVA是如何通過Cipher實現加密與解密的?
實戰操作:具體代碼如下
package com.bsd.yx;import java.security.Key;import java.security.Security;import java.text.SimpleDateFormat;import java.util.Date;import javax.crypto.Cipher;import com.ibm.model.cxf.Safety;/** * 加密與解密 * @author tanf * @date 2013-11-08 */public class EncryptionDecryption {/*** 默認密鑰*/private static String strDefaultKey = "tandaly201124335";/** 加密工具 */private static Cipher encryptCipher = null;/** 解密工具 */private static Cipher decryptCipher = null;/*** 將byte數組轉換爲表示16進制值的字符串, 如:byte[]{8,18}轉換爲:0813, 和public static byte[]* hexStr2ByteArr(String strIn) 互爲可逆的轉換過程* * @param arrB* 需要轉換的byte數組* @return 轉換後的字符串* @throws Exception* */public static String byteArr2HexStr(byte[] arrB) throws Exception {int iLen = arrB.length;// 每個byte用兩個字符才能表示,所以字符串的長度是數組長度的兩倍StringBuffer sb = new StringBuffer(iLen * 2);for (int i = 0; i < iLen; i++) {int intTmp = arrB[i];// 把負數轉換爲正數while (intTmp < 0) {intTmp = intTmp + 256;}// 小於0F的數需要在前面補0if (intTmp < 16) {sb.append("0");}sb.append(Integer.toString(intTmp, 16));}return sb.toString();}/*** 將表示16進制值的字符串轉換爲byte數組, 和public static String byteArr2HexStr(byte[] arrB)* 互爲可逆的轉換過程* * @param strIn* 需要轉換的字符串* @return 轉換後的byte數組* @throws Exception* */public static byte[] hexStr2ByteArr(String strIn) throws Exception {byte[] arrB = strIn.getBytes();int iLen = arrB.length;// 兩個字符表示一個字節,所以字節數組長度是字符串長度除以2byte[] arrOut = new byte[iLen / 2];for (int i = 0; i < iLen; 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*/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 static byte[] encrypt(byte[] arrB) throws Exception {return encryptCipher.doFinal(arrB);}/*** 加密字符串* * @param strIn* 需加密的字符串* @return 加密後的字符串* @throws Exception*/public static String encrypt(String strIn) throws Exception {return byteArr2HexStr(encrypt(strIn.getBytes()));}/*** 解密字節數組* * @param arrB* 需解密的字節數組* @return 解密後的字節數組* @throws Exception*/public static byte[] decrypt(byte[] arrB) throws Exception {return decryptCipher.doFinal(arrB);}/*** 解密字符串* * @param strIn* 需解密的字符串* @return 解密後的字符串* @throws Exception*/public static 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 java.lang.Exception*/private Key getKey(byte[] arrBTmp) throws Exception {// 創建一個空的8位字節數組(默認值爲0)byte[] arrB = new byte[8];// 將原始字節數組轉換爲8位for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {arrB[i] = arrBTmp[i];}// 生成密鑰Key key = new javax.crypto.spec.SecretKeySpec(arrB, "ABC");return key;}/*** 自定義加密* * @param arrBTmp* * @return 生成的密鑰* @throws java.lang.Exception*/public static String BPMMD5(String str){try {EncryptionDecryption des = new EncryptionDecryption();// 自定義密鑰str= EncryptionDecryption.encrypt(str);System.out.println(str); } catch (Exception e) { e.printStackTrace(); }return str;}}
-
新筆記本電腦第一次充電要多久,筆記本電腦充電時間
1、第一次先把筆記本電池中的電完全用完,然後關機充電12個小時以上,最好14個小時左右。2、只有第一次充電需要衝的時間長,以後每次都是充滿即可,現在的筆記本都是有完善的電源管理電路和充電管理體系,充電時間過長,這樣會導致充電時熱量過高,會引起爆炸等現象。3、如...
-
吃菠蘿什麼好處
菠蘿是一種熱帶水果,從十六世紀末傳入中國以來,因爲吃起來酸甜可口,成爲備受人們喜愛的熱帶水果之一,也因此在中國南方,氣候適宜的地方得到普遍的栽培,中國也是世界十大菠蘿產國之一。在生活中有人可能分不清菠蘿和鳳梨的區別,從生物學上來說,這倆是同一種水果,但是在商...
-
樂視視頻開通會員怎麼開
開通樂視視頻會員的途徑很簡單,在官網註冊賬號選擇開通的會員類型,點擊支付即可。也可以通過手機裏面的樂視視頻APP開通會員,先註冊或者登錄已有賬號,選擇右下角的未登錄,點進去可以選擇開通會員。首先,打開樂視視頻軟件,進入主頁面後,選擇右下角的“未登錄”,再“點擊...
-
微博如何移動好友分組
1、進入個人微博,點擊主頁裏的“關注”。或者從首頁裏邊的“關注”進入也是可以的。2、進入關注列表後自動顯示爲查看其他分組,這個時候你可以看到“批量管理”和“排序”兩個選項,你可以進行初步排序管理。3、單擊“查看其他分組”,裏面有你微博裏所有的分組,我們...