MD5 encryption and validation of strings using Java

Source: Internet
Author: User
Tags md5 md5 encryption validation examples

A few days ago, a Java proxy was used in Domino to write a simple function of encrypting and validating strings using MD5.

Reference articles:

1. Java encryption algorithm Implementation use case: Http://www-128.ibm.com/developerworks/cn/java/l-security

The implementation process is as follows:

1. Encrypt the password string:

First, the generation of a MessageDigest class: MessageDigest alg= messagedigest.getinstance ("MD5");

Add information to calculate the summary: Alg.update (Password.getbytes ()); Where password is the password string

Calculate the summary: byte[] Digest=alga.digest ();

Then convert the digest into 16: Byte2hex (Digest)

/*

* Binary conversion to hexadecimal string

*

* @param b-binary array @return hexadecimal string

*/

private static String Byte2hex (byte[] bytes) {

String hs = "";

String stmp = "";

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

Stmp = (java.lang.Integer.toHexString (bytes[i] & 0XFF));

if (stmp.length () = = 1)

HS = HS + "0" + stmp;

Else

HS = HS + stmp;

}

return Hs.touppercase ();

Saves the generated 16 feed summary to the database.

2. Verification:

Gets the authentication password entered: password=doc.getitemvaluestring ("f_passwordvalidate");

Get the relevant summary in the database and convert the digest:

Byte[] Digest = tobytes (curdoc.getitemvaluestring ("F_password"));

/**

* Convert string to byte[]

* @param hex the string to be converted

* @return converted byte[]

*/

public static byte[] Tobytes (String hex) {

byte[] bytes = new BYTE[16];

for (int i = 0,j=0; I

BYTES[J] = integer.valueof (hex.substring (i,i+2), Bytevalue ();

}

return bytes;

}

Finally, verify that the comparison summary is the same:

result = Isvalidate (digest, password);

if (result = = True)

Retmsg = "Success". ";

Else

Retmsg = "failed." ";

public boolean isvalidate (byte[] Digest, String password) {

try {

MessageDigest alga = messagedigest.getinstance ("MD5");

Alga.update (Password.getbytes ());

if (Messagedigest.isequal (Digest, Alga.digest ()))

return true;

Else

return false;

catch (Exception ex) {

SYSTEM.OUT.PRINTLN ("algorithm error.");

return false;

}

}

Full Java code:

/*

* Created on 2005-9-23

*

*/

Package security;

Import Java.security.MessageDigest;

Import java.security.NoSuchAlgorithmException;

/**

* @author stunet

*

* MD5 encryption and validation examples using Java for strings

*

*/

public class Md5digestdemo {

public static void Main (string[] args) {

try {

Make string encryption

String password = "NET";

System.out.println ("Encrypted string:" + md5digestdemo.md5digest (password));

Perform string validation

String validate = md5digestdemo.md5digest (password);

String passwordvalidate = "NET";

Boolean issuccess = Md5digestdemo.isvalidate (tobytes (Validate),

Passwordvalidate);

if (issuccess)

SYSTEM.OUT.PRINTLN ("Validation successful!");

Else

System.out.println ("Validation failed!");

catch (NoSuchAlgorithmException e) {

E.printstacktrace ();

}

}

/**

*

* @param password

* Strings that need to be encrypted

* @return The encrypted string

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.