JAVA string encryption password encryption implementation method

  • 2020-05-10 18:15:45
  • OfStack

In our program design, we often want to encrypt 1 some special content, today summed up a few simple encryption methods, to share with you!

How to use JAVA to achieve simple string encryption and decryption? In order to ensure the security of user information, when the system saves user information, be sure to save its password to the database.  

Need to use the password, take out the data, decryption processing can be.  

Avoid saving plaintext passwords.

Plan 1:


package com.tnt.util; 

 

import java.security.MessageDigest; 

public class StringUtil { 

  private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", 

      "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; 

 

  /** 

   *  Convert the byte array to 16 A binary string  

   * 

   * @param b 

   *       An array of bytes  

   * @return 16 A binary string  

   */ 

  public 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 + n; 

    int d1 = n / 16; 

    int d2 = n % 16; 

    return hexDigits[d1] + hexDigits[d2]; 

  } 

 

  public static String MD5Encode(String origin) { 

    String resultString = null; 

    try { 

      resultString = new String(origin); 

      MessageDigest md = MessageDigest.getInstance("MD5"); 

      resultString = byteArrayToHexString(md.digest(resultString 

          .getBytes())); 

    } catch (Exception ex) { 

    } 

    return resultString; 

  } 

 

  public static void main(String[] args) { 

    System.err.println(MD5Encode("123456")); 

  } 

} 

Scheme 2


package com.shangyu.core.utils;



public class MD5 {


public static String getMD5(byte[] source) {

String s = null;

char hexDigits[] = { //  Used to convert bytes to  16  Character in base notation 

 ' 0 ' ,  ' 1 ' ,  ' 2 ' ,  ' 3 ' ,  ' 4 ' ,  ' 5 ' ,  ' 6 ' ,  ' 7 ' ,  ' 8 ' ,  ' 9 ' ,  ' a ' ,  ' b ' ,  ' c ' ,  ' d ' ,

 ' e ' ,  ' f '  };

try {

java.security.MessageDigest md = java.security.MessageDigest

.getInstance("MD5");

md.update(source);

byte tmp[] = md.digest(); // MD5  The calculated result is 1 a  128  A long integer of bits, 

//  In bytes  16  bytes 

char str[] = new char[16 * 2]; //  Each byte USES  16  For the base representation, use two characters, 

//  So it's expressed as  16  Into the system need to be  32  A character 

int k = 0; //  Represents the corresponding character position in the conversion result 

for (int i = 0; i < 16; i++) { //  From the first 1 We start with bytes, right  MD5  Each of the 1 bytes 

//  Converted to  16  Conversion of base characters 

byte byte0 = tmp[i]; //  Take the first  i  bytes 

str[k++] = hexDigits[byte0 >>> 4 & 0xf]; //  Take the height in bytes  4  Bit number conversion ,

// >>>

//  Moves the symbol bit for the logical right 1 The moves to the right 

str[k++] = hexDigits[byte0 & 0xf]; //  Take the low in the byte  4  Bit number conversion 

}

s = new String(str); //  The result is converted to a string 



} catch (Exception e) {

e.printStackTrace();

}

return s;

}



public static String getMD5(String str) {

return getMD5(str.getBytes());

}


public static void main(String[] args){

System.out.println(MD5.getMD5("123456"));

}

}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: