Encrypt data using the MD5 function in JSP

Source: Internet
Author: User

Package beans;
Import java. Io .*;
Import java. Security .*;
Public class changemd5
{
Private messagedigest currentalgorithm;

Public String computedigest (byte [] B)
{
Try
{
Currentalgorithm = messagedigest. getinstance ("MD5 ");
} Catch (nosuchalgorithmexception e ){}
Currentalgorithm. Reset ();
Currentalgorithm. Update (B );
Byte [] hash = currentalgorithm. Digest ();
String d = "";
For (INT I = 0; I {
Int v = hash [I] & 0xff;
If (v <16) D + = "0 ";
D + = integer. tostring (v, 16). tolowercase ();
}
Return D;
}
}

This sectionCodeYou can use it directly.ProgramIt is actually a bean, and its method name is computedigest (

Byte [] B). The parameter is a byte type variable. If you want to encrypt the string "xiapeng" with MD5 encryption, first set a string-type variable, such as string result = "xiapeng "; in addition, you can use the getbytes () method to convert the type, and then call the beans in JSP to get it OK.

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.