java加密字符串,可解密
生活随笔
收集整理的這篇文章主要介紹了
java加密字符串,可解密
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
有一種是字符串反轉,反轉兩次即可解密,可加密性不高,這一種是用鹽加密
md5加密是不可反轉的所以誕生了以下工具類
import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Base32; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Hex;import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom;public class CryptoUtil {private static final String DEFAULT_SECRET_KEY1 = "?:P)(OL><KI*&UJMNHY^%TGBVFR$#EDCXSW@!QAZ";private static final String DEFAULT_SECRET_KEY2 = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik,9ol.0p;/";private static final String DEFAULT_SECRET_KEY3 = "!QAZ@WSX#EDC$RFV%TGB^YHN&UJM*IK<(OL>)P:?";private static final String DEFAULT_SECRET_KEY = DEFAULT_SECRET_KEY1;public static final String SALT = "hang";private static final String DES = "DES";private static final Base32 base32 = new Base32();private static Key DEFAULT_KEY = null;static {DEFAULT_KEY = obtainKey(DEFAULT_SECRET_KEY);}/*** 獲得key**/private static Key obtainKey(String key) {if (key == null) {return DEFAULT_KEY;}KeyGenerator generator = null;try {generator = KeyGenerator.getInstance(DES);} catch (NoSuchAlgorithmException e) {e.printStackTrace();}SecureRandom random = null;try {random = SecureRandom.getInstance("SHA1PRNG");random.setSeed(key.getBytes());} catch (NoSuchAlgorithmException e) {e.printStackTrace();}generator.init(random);Key key1 = generator.generateKey();generator = null;return key1;}/*** 加密String明文輸入,String密文輸出*/private static String encode(String str) {return encode64(null, str);}/*** 加密String明文輸入,String密文輸出*/public static String encode64(String key, String str) {return Base64.encodeBase64URLSafeString(obtainEncode(key, str.getBytes()));}/*** 加密String明文輸入,String密文輸出*/private static String encode32(String key, String str) {return base32.encodeAsString(obtainEncode(key, str.getBytes())).replaceAll("=", "");}/*** 加密String明文輸入,String密文輸出*/private static String encode16(String key, String str) {return Hex.encodeHexString(obtainEncode(key, str.getBytes()));}/*** 解密以String密文輸入,String明文輸出*/private static String decode(String str) {return decode64(null, str);}/*** 解密以String密文輸入,String明文輸出*/public static String decode64(String key, String str) {return new String(obtainDecode(key, Base64.decodeBase64(str)));}/*** 解密以String密文輸入,String明文輸出*/private static String decode32(String key, String str) {return new String(obtainDecode(key, base32.decode(str)));}/*** 解密以String密文輸入,String明文輸出*/private static String decode16(String key, String str) {try {return new String(obtainDecode(key, Hex.decodeHex(str.toCharArray())));} catch (DecoderException e) {e.printStackTrace();}return null;}/*** 加密以byte[]明文輸入,byte[]密文輸出*/private static byte[] obtainEncode(String key, byte[] str) {byte[] byteFina = null;Cipher cipher;try {Key key1 = obtainKey(key);cipher = Cipher.getInstance(DES);cipher.init(Cipher.ENCRYPT_MODE, key1);byteFina = cipher.doFinal(str);} catch (Exception e) {e.printStackTrace();} finally {cipher = null;}return byteFina;}/*** 解密以byte[]密文輸入,以byte[]明文輸出*/private static byte[] obtainDecode(String key, byte[] str) {Cipher cipher;byte[] byteFina = null;try {Key key1 = obtainKey(key);cipher = Cipher.getInstance(DES);cipher.init(Cipher.DECRYPT_MODE, key1);byteFina = cipher.doFinal(str);} catch (Exception e) {e.printStackTrace();} finally {cipher = null;}return byteFina;}public static String encodeSrc(String src) {String m = encode64(DEFAULT_SECRET_KEY2 + SALT, src);String saltm = SALT + ";" + m;String result = encode32(DEFAULT_SECRET_KEY3, saltm);return result;}public static String decodeTarget(String target) {String n = decode32(DEFAULT_SECRET_KEY3, target);String key = n.split(";")[0];String m = n.split(";")[1];String result = decode64(DEFAULT_SECRET_KEY2 + key, m);return result;}}總結
以上是生活随笔為你收集整理的java加密字符串,可解密的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米汽车要来了?传已经小批量试生产 或明
- 下一篇: 极星 4 量产版下线:定位纯电高性能 S