PwdUtil.java 密碼加密

來源:互聯網
上載者:User
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位元組 *  * @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 blocke.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);}/** * 將位元組數群組轉換為十六進位字串 *  * @param src *            位元組數組 * @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();}/** * 將十六進位字串轉換為位元組數組 *  * @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();}}}

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.