- import java.security.MessageDigest;
-
- public class PwdUtil {
- public static String MD5(String inStr) {
- MessageDigest md5 = null;
- try {
- md5 = MessageDigest.getInstance("MD5");
- } catch (Exception e) {
- System.out.println(e.toString());
- e.printStackTrace();
- return "";
- }
- char[] charArray = inStr.toCharArray();
- byte[] byteArray = new byte[charArray.length];
-
- for (int i = 0; i < charArray.length; ++i) {
- byteArray[i] = (byte) charArray[i];
- }
- byte[] md5Bytes = md5.digest(byteArray);
-
- StringBuffer hexValue = new StringBuffer();
-
- for (int i = 0; i < md5Bytes.length; ++i) {
- int val = md5Bytes[i] & 0xFF;
- if (val < 16)
- hexValue.append("0");
- hexValue.append(Integer.toHexString(val));
- }
-
- return hexValue.toString();
- }
-
- public static String encrypt(String inStr) {
- char[] a = inStr.toCharArray();
- for (int i = 0; i < a.length; ++i) {
- a[i] = (char) (a[i] ^ 0x74);
- }
- String s = new String(a);
- return s;
- }
-
- public static String decrypt(String inStr) {
- char[] a = inStr.toCharArray();
- for (int i = 0; i < a.length; ++i) {
- a[i] = (char) (a[i] ^ 0x74);
- }
- String k = new String(a);
- return k;
- }
- }
另一個加密工具
- import java.security.Key;
- import java.security.SecureRandom;
- import javax.crypto.Cipher;
- import javax.crypto.SecretKey;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.PBEKeySpec;
- import javax.crypto.spec.PBEParameterSpec;
- public class PasswordUtil {
-
- /**
- * JAVA6支持以下任意一種算法 PBEWITHMD5ANDDES PBEWITHMD5ANDTRIPLEDES
- * PBEWITHSHAANDDESEDE PBEWITHSHA1ANDRC2_40 PBKDF2WITHHMACSHA1
- * */
-
- /**
- * 定義使用的算法為:PBEWITHMD5andDES算法
- */
- public static final String ALGORITHM = "PBEWithMD5AndDES";//加密算法
- public static final String Salt = "63293188";//密鑰
-
- /**
- * 定義迭代次數為1000次
- */
- private static final int ITERATIONCOUNT = 1000;
-
- /**
- * 獲取加密算法中使用的鹽值,解密中使用的鹽值必須與加密中使用的相同才能完成操作. 鹽長度必須為8字節(jié)
- *
- * @return byte[] 鹽值
- * */
- public static byte[] getSalt() throws Exception {
- // 實例化安全隨機數
- SecureRandom random = new SecureRandom();
- // 產出鹽
- return random.generateSeed(8);
- }
-
- public static byte[] getStaticSalt() {
- // 產出鹽
- return Salt.getBytes();
- }
-
- /**
- * 根據PBE密碼生成一把密鑰
- *
- * @param password
- * 生成密鑰時所使用的密碼
- * @return Key PBE算法密鑰
- * */
- private static Key getPBEKey(String password) {
- // 實例化使用的算法
- SecretKeyFactory keyFactory;
- SecretKey secretKey = null;
- try {
- keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
- // 設置PBE密鑰參數
- PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
- // 生成密鑰
- secretKey = keyFactory.generateSecret(keySpec);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- return secretKey;
- }
-
- /**
- * 加密明文字符串
- *
- * @param plaintext
- * 待加密的明文字符串
- * @param password
- * 生成密鑰時所使用的密碼
- * @param salt
- * 鹽值
- * @return 加密后的密文字符串
- * @throws Exception
- */
- public static String encrypt(String plaintext, String password, byte[] salt) {
-
- Key key = getPBEKey(password);
- byte[] encipheredData = null;
- PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, ITERATIONCOUNT);
- try {
- Cipher cipher = Cipher.getInstance(ALGORITHM);
-
- cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
-
- encipheredData = cipher.doFinal(plaintext.getBytes());
- } catch (Exception e) {
- }
- return bytesToHexString(encipheredData);
- }
-
- /**
- * 解密密文字符串
- *
- * @param ciphertext
- * 待解密的密文字符串
- * @param password
- * 生成密鑰時所使用的密碼(如需解密,該參數需要與加密時使用的一致)
- * @param salt
- * 鹽值(如需解密,該參數需要與加密時使用的一致)
- * @return 解密后的明文字符串
- * @throws Exception
- */
- public static String decrypt(String ciphertext, String password, byte[] salt) {
-
- Key key = getPBEKey(password);
- byte[] passDec = null;
- PBEParameterSpec parameterSpec = new PBEParameterSpec(getStaticSalt(), ITERATIONCOUNT);
- try {
- Cipher cipher = Cipher.getInstance(ALGORITHM);
-
- cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
-
- passDec = cipher.doFinal(hexStringToBytes(ciphertext));
- }
-
- catch (Exception e) {
- // TODO: handle exception
- }
- return new String(passDec);
- }
-
- /**
- * 將字節(jié)數組轉換為十六進制字符串
- *
- * @param src
- * 字節(jié)數組
- * @return
- */
- public static String bytesToHexString(byte[] src) {
- StringBuilder stringBuilder = new StringBuilder("");
- if (src == null || src.length <= 0) {
- return null;
- }
- for (int i = 0; i < src.length; i++) {
- int v = src[i] & 0xFF;
- String hv = Integer.toHexString(v);
- if (hv.length() < 2) {
- stringBuilder.append(0);
- }
- stringBuilder.append(hv);
- }
- return stringBuilder.toString();
- }
-
- /**
- * 將十六進制字符串轉換為字節(jié)數組
- *
- * @param hexString
- * 十六進制字符串
- * @return
- */
- public static byte[] hexStringToBytes(String hexString) {
- if (hexString == null || hexString.equals("")) {
- return null;
- }
- hexString = hexString.toUpperCase();
- int length = hexString.length() / 2;
- char[] hexChars = hexString.toCharArray();
- byte[] d = new byte[length];
- for (int i = 0; i < length; i++) {
- int pos = i * 2;
- d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
- }
- return d;
- }
-
- private static byte charToByte(char c) {
- return (byte) "0123456789ABCDEF".indexOf(c);
- }
-
- public static void main(String[] args) {
- int i=10;
- for (int j = 0; j < i; j++) {
- if((j)%3==0)
- {
- System.out.print("<br>");
- }
- else {
- System.out.print(j);
- }
- }
- System.out.print(-1%2==0);
- String str = "admin";
- String password = "123456";
-
- System.out.println("明文:" + str);
- System.out.println("密碼:" + password);
-
- try {
- byte[] salt = PasswordUtil.getStaticSalt();
- String ciphertext = PasswordUtil.encrypt(str, password, salt);
- System.out.println("密文:" + ciphertext);
- String plaintext = PasswordUtil.decrypt(ciphertext, password, salt);
- System.out.println("明文:" + plaintext);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
MD5 加密
- import java.security.MessageDigest;
-
- import org.apache.commons.lang.StringUtils;
-
- public class MethodUtil{
- /**
- * 初始化
- * @return
- */
- public static MethodUtil getInit(){
- return new MethodUtil();
- }
- private static String byteArrayToHexString(byte b[]) {
- StringBuffer resultSb = new StringBuffer();
- for (int i = 0; i < b.length; i++)
- resultSb.append(byteToHexString(b[i]));
-
- return resultSb.toString();
- }
-
- private static String byteToHexString(byte b) {
- int n = b;
- if (n < 0)
- n += 256;
- int d1 = n / 16;
- int d2 = n % 16;
- return hexDigits[d1] + hexDigits[d2];
- }
-
- /**
- * MD5 加密
- *
- */
- public static String MD5(String origin) {
- String resultString = MD5Encode(origin,"UTF-8");
- if(StringUtils.isNotBlank(resultString)){
- return resultString.toUpperCase();
- }
- return resultString;
- }
-
-
- /**
- * MD5 比較 匹配origin 加密后是否等于md5
- * @param origin 密碼, 未加密
- * @param md5 已加密字符串
- * @return
- */
- public static boolean ecompareMD5(String origin,String md5) {
- String result =MD5(origin);
- return md5.equals(result);
- }
-
-
- /*
- * MD5 加密
- * */
- public static String MD5Encode(String origin, String charsetname) {
- String resultString = null;
- try {
- resultString = new String(origin);
- MessageDigest md = MessageDigest.getInstance("MD5");
- if (charsetname == null || "".equals(charsetname))
- resultString = byteArrayToHexString(md.digest(resultString
- .getBytes()));
- else
- resultString = byteArrayToHexString(md.digest(resultString
- .getBytes(charsetname)));
- } catch (Exception exception) {
-
- }
- return resultString;
- }
-
- private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
- "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
-
- /**
- * 將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的數需要在前面補0
- if (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
- * 本方法不處理任何異常,所有異常全部拋出
- * @author <a href="mailto:[email protected]">LiGuoQing</a >
- */
- public static byte[] hexStr2ByteArr(String strIn) throws Exception {
- byte[] arrB = strIn.getBytes();
- int iLen = arrB.length;
-
- // 兩個字符表示一個字節(jié),,所以字節(jié)數組長度是字符串長度除以2
- byte[] 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;
- }
-
-
- }
|