JAVA Password Encryption

Source: Internet
Author: User

How to implement simple encryption and decryption of strings in Java? In order to ensure the security of user information, the system when saving user information, it is important to keep its password encryption to the database.

When you need to use a password, take out the data and decrypt the processing.

Avoid saving plaintext passwords.


Programme one:

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 byte array to 16 binary string

*

* @param b

* Byte array

* @return 16 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 = + 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"));

}

}


Programme II

Package com.shangyu.core.utils;


public class MD5 {

public static String getMD5 (byte[] source) {

String s = null;

Char hexdigits[] = {//character used to convert bytes to 16 binary 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 (); The MD5 evaluates to a 128-bit long integer,

In bytes, that's 16 bytes.

Char str[] = new char[16 * 2]; Each byte is represented by a 16 binary, using two characters,

So 32 characters are required to represent 16 binary

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

for (int i = 0; i < i++) {//Starting with the first byte, each byte of MD5

Convert to 16-character conversion

byte byte0 = tmp[i]; Take the first I byte

str[k++] = hexdigits[byte0 >>> 4 & 0xf]; Takes a 4-bit numeric conversion in bytes,

>>>

Move right to logical right, move symbol bit

str[k++] = hexdigits[byte0 & 0xf]; The number conversion of the lower 4 bits in the fetch byte

}

s = new String (str); Converted result to 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"));

}

}


This article from the "extraordinary life-seekers of knowledge" blog, declined to reprint!

JAVA Password Encryption

Related Article

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.