生活随笔
收集整理的這篇文章主要介紹了
字符串加密解密处理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? 使用場景中需要對字符串加密、解密,且加解密可以指定密鑰時,可以使用javax.crypto.Cipher類作為加解密工具,java.security.Key作為密鑰。
方法一:
加密時先將字符串轉(zhuǎn)換成byte數(shù)組,用Cipher.doFinal加密后獲取加密byte數(shù)組,并轉(zhuǎn)換成16進(jìn)制字符串作為加密結(jié)果解密時將需要解密的16進(jìn)制字符串轉(zhuǎn)換成加密后的byte數(shù)組,再用Cipher.doFinal解密,用解密后的byte數(shù)組創(chuàng)建String對象獲得解密后字符串。
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;public class EncryptUtils {private static String defaultKey = "youngsun";//加密工具private Cipher encryptCipher;//解密工具private Cipher decryptCipher;/*** 指定密鑰構(gòu)造方法* @param keyStr 傳入的密鑰* @throws Exception*/public EncryptUtils(String keyStr) throws Exception {Key key = getKey(keyStr.getBytes());encryptCipher = Cipher.getInstance("DES");encryptCipher.init(Cipher.ENCRYPT_MODE, key);decryptCipher = Cipher.getInstance("DES");decryptCipher.init(Cipher.DECRYPT_MODE, key);}/** 默認(rèn)密鑰構(gòu)造方法 */public EncryptUtils() throws Exception {this(defaultKey);}/*** byte數(shù)組轉(zhuǎn)化成16進(jìn)制字符串(與hexStr2ByteArr互為轉(zhuǎn)換)* @param byteArr* @return hexStr*/public static String byteArr2HexStr(byte[] byteArr) throws Exception {int arrLen = byteArr.length;//byte轉(zhuǎn)16進(jìn)制需要預(yù)留兩位,不到兩位的用0補充StringBuffer sb = new StringBuffer(arrLen*2);for (int i = 0; i < arrLen; i++) {int b = byteArr[i];while (b < 0) {b = b + 256;}if(b < 16){sb.append("0");}String s = Integer.toString(b, 16);sb.append(s);}return sb.toString();}/*** 16進(jìn)制字符串轉(zhuǎn)換成byte數(shù)組(與byteArr2HexStr互為轉(zhuǎn)換)* @param hexStr* @return byte[]*/public static byte[] hexStr2ByteArr(String hexStr){int strLen = hexStr.length();byte[] bytes = new byte[strLen / 2];for (int i = 0; i < strLen; i = i + 2) {String byteStr = hexStr.substring(i, i + 2);bytes[i/2] = (byte)Integer.parseInt(byteStr, 16);}return bytes;}/*** byte數(shù)組加密* @param bytes 待加密byte數(shù)組* @return encryptBytes 加密后數(shù)組* @throws Exception*/public byte[] encryptBytes(byte[] bytes) throws Exception{byte[] encryptBytes = encryptCipher.doFinal(bytes);return encryptBytes;}/*** 字符串加密* @param str 待加密字符串* @return* @throws Exception*/public String encryptStr(String str) throws Exception{byte[] bytes = str.getBytes();byte[] encryptBytes = encryptCipher.doFinal(bytes);String encryptStr = byteArr2HexStr(encryptBytes);return encryptStr;}public byte[] decryptBytes(byte[] bytes) throws Exception{return decryptCipher.doFinal(bytes);}public String decryptString(String str) throws Exception{byte[] bytes = decryptBytes(hexStr2ByteArr(str));return new String(bytes);}/*** 創(chuàng)建密鑰需要數(shù)組長度為8位,不足8位的用0替代,超出8位取前八位* @param keyBytes 密鑰字符串轉(zhuǎn)數(shù)組,獲取Key* @return Key*/private Key getKey(byte[] keyBytes){byte[] entBytes = new byte[8];for (int i = 0; i < keyBytes.length && i < entBytes.length; i++) {entBytes[i] = keyBytes[i];//keyBytes不足8位時,余下}Key key = new SecretKeySpec(entBytes, "DES");return key;}}
方法二:
? ? 基于方法一,將上面代碼中的byte數(shù)組與16進(jìn)制字符串互轉(zhuǎn)部分改為由com.thoughtworks.xstream.core.util.Base64Encoder類加密解密處理。
因為如果加密后的byte數(shù)組,直接用于創(chuàng)建String對象作為加密字符串的轉(zhuǎn)換不可逆(再將字符串轉(zhuǎn)換成byte數(shù)組后的結(jié)果與加密結(jié)果byte數(shù)組不一致)。
@Testpublic void testEncryptUtils(){String testStr = "123456";String keyStr = "youngsun";try {Key key = new SecretKeySpec(keyStr.getBytes(), "DES");Cipher encryptCipher = Cipher.getInstance("DES");encryptCipher.init(Cipher.ENCRYPT_MODE, key);Cipher decryptCipher = Cipher.getInstance("DES");decryptCipher.init(Cipher.DECRYPT_MODE, key);System.out.println("加密前:" + testStr);byte[] bytes = encryptCipher.doFinal(testStr.getBytes());System.out.println("加密后數(shù)組bytes:" + Arrays.toString(bytes));String byteStr = new String(bytes);System.out.println("bytes轉(zhuǎn)換成字符串 byteStr:" + byteStr);System.out.println("byteStr轉(zhuǎn)換byte數(shù)組" + Arrays.toString(byteStr.getBytes()));Base64Encoder base64Encoder = new Base64Encoder();String s = base64Encoder.encode(bytes);System.out.println("加密后:" + s);byte[] bytes2 = base64Encoder.decode(s);byte[] bytes1 = decryptCipher.doFinal(bytes2);System.out.println("解密后:" + new String(bytes1) );} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (NoSuchPaddingException e) {e.printStackTrace();}catch (InvalidKeyException e) {e.printStackTrace();}catch (IllegalBlockSizeException e) {e.printStackTrace();} catch (BadPaddingException e) {e.printStackTrace();}
}
運行結(jié)果:
加密前:123456
加密后數(shù)組bytes:[58, -67, 118, 96, 3, -74, -109, 5]
bytes轉(zhuǎn)換成字符串 byteStr::�v`��
byteStr轉(zhuǎn)換byte數(shù)組[58, -17, -65, -67, 118, 96, 3, -17, -65, -67, -17, -65, -67, 5]
加密后:Or12YAO2kwU=
解密后:123456
?
總結(jié)
以上是生活随笔為你收集整理的字符串加密解密处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。