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;}}