Pwdutil. Java password encryption

Source: Internet
Author: 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;}}

 

 

 

 

Another encryption tool

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 supports any of the following algorithms: pbewithmd5anddes algorithm */public static final string algorithm = "pbewithmd5anddes"; // encryption algorithm public static final string salt = "63293188 "; // key/*** defines the number of iterations as 1000 */Private Static final int iterationcount = 1000;/*** gets the salt value used in the encryption algorithm, the salt value used in decryption must be the same as that used in encryption to complete the operation. the length of the salt must be 8 bytes ** @ return byte [] salt value **/public static byte [] getsalt () throws exception {// instantiate the Security random number securerandom random = new securerandom (); // output the salt return random. generateseed (8);} public static byte [] getstaticsalt () {// generate the salt return salt. getbytes ();} /*** generate a key based on the PBE password ** @ Param password * The password used to generate the key * @ return key PBE algorithm key **/Private Static key getpbekey (string Password) {// secretkeyfactory keyfactory; secretkey = NULL; try {keyfactory = secretkeyfactory. getinstance (algorithm); // set the PBE key parameter pbekeyspec keyspec = new pbekeyspec (password. tochararray (); // generate the secret key secretkey = keyfactory. generatesecret (keyspec);} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();} return secretkey ;} /*** encrypt the plaintext string ** @ Param plaintext * the plaintext string to be encrypted * @ Param password * The password used to generate the key * @ Param salt * salt value * @ return encrypted ciphertext string * @ throws exception */public static string encrypt (string plaintext, string password, byte [] salt) {key = getpbekey (password); byte [] enciphereddata = NULL; pbeparameterspec parameterspec = new pbeparameterspec (salt, iterationcount ); try {cipher = cipher. getinstance (algorithm); cipher. init (cipher. encrypt_mode, key, parameterspec); enciphereddata = cipher. dofinal (plaintext. getbytes ();} catch (exception e) {} return bytestohexstring (enciphereddata );} /*** decrypt the ciphertext string ** @ Param ciphertext * the ciphertext string to be decrypted * @ Param password * The password used to generate the key (if decryption is required, this parameter must be consistent with the value used for encryption. * @ Param salt * salt value (if decryption is required, this parameter must be consistent with the value used for encryption) * @ return the decrypted plaintext string * @ throws exception */public static string decrypt (string ciphertext, string password, byte [] salt) {key = getpbekey (password ); byte [] passdec = NULL; pbeparameterspec parameterspec = new pbeparameterspec (getstaticsalt (), iterationcount); try {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 );} /*** convert a byte array to a hexadecimal string ** @ Param SRC * byte array * @ return */public static string bytestohexstring (byte [] SRC) {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 ();} /*** convert the hexadecimal string to a byte array ** @ Param hexstring * The hexadecimal string * @ 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) "0123456789 abcdef ". 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 ("plaintext:" + Str); system. out. println ("Password:" + password); try {byte [] salt = passwordutil. getstaticsalt (); string ciphertext = passwordutil. encrypt (STR, password, salt); system. out. println ("ciphertext:" + ciphertext); string plaintext = passwordutil. decrypt (ciphertext, password, salt); system. out. println ("plaintext:" + plaintext);} catch (exception e) {e. printstacktrace ();}}}

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.