Current location - Quotes Website - Signature design - How to implement md5 encryption and decryption in java
How to implement md5 encryption and decryption in java

package endecrypt;

02.

03.import java.io.UnsupportedEncodingException;

04.import java.security.MessageDigest ;

05.import java.security.NoSuchAlgorithmException;

06.

07./**

08. * Use MD5 Encryption and decryption

09. * @author tfq

10. * @datetime 2011-10-13

11. */

12. public class MD5Util {

13.

14. /***

15. * MD5 encoding generates 32-bit md5 code

< p>16. */

17. public static String string2MD5(String inStr){

18. MessageDigest md5 = null;

19. try{ < /p>

20. md5 = MessageDigest.getInstance("MD5");

21. }catch (Exception e){

22. System.out.println( e.toString());

23. e.printStackTrace();

24. return "";

25. }

< p>26. char[] charArray = inStr.toCharArray();

27. byte[] byteArray = new byte[charArray.length];

28.

29. for (int i = 0; i < charArray.length; i++)

30. byteArray[i] = (byte) charArray[i];

31 . byte[] md5Bytes = md5.digest(byteArray);

32. StringBuffer hexValue = new StringBuffer();

33. for (int i = 0; i < md5Bytes. length; i++){

34. int val = ((int) md5Bytes[i]) & 0xff;

35. if (val < 16)

36. hexValue.append("0");

37. hexValue.append(Integer.toHexString(val));

38. }

39. return hexValue.toString();

40.

41. }

42.

43. /** < /p>

44. * The encryption and decryption algorithm performs encryption once and decryption twice

45. */

46. public static String convertMD5(String inStr){

45. p>

47.

48. char[] a = inStr.toCharArray();

49. for (int i = 0; i < a.length; i++ ){

50. a[i] = (char) (a[i] ^ 't');

51. }

52. String s = new String(a);

53. return s;

54.

55. }