Common Java functions 6

Source: Internet
Author: User
// Replacement character
Public   Static String Replace (string instring, string oldpattern, string newpattern)
{
If (Instring =   Null )
Return   Null ;
If (Oldpattern =   Null   | Newpattern =   Null )
Return Instring;
Stringbuffer sbuf =   New Stringbuffer ();
Int Pos =   0 ;
Int Index = Instring. indexof (oldpattern );
Int Patlen = Oldpattern. Length ();
For (; Index > =   0 ; Index = Instring. indexof (oldpattern, POS ))
{
Sbuf. append (instring. substring (Pos, index ));
Sbuf. append (newpattern );
Pos = Index + Patlen;
}

Sbuf. append (instring. substring (POS ));
ReturnSbuf. tostring ();
}

/**
* The class MD5.
  */
Public   Class MD5 {

/**
* 32-bit encryptionAlgorithm.
*
* @ Param Plaintext the plain text
*
* @ Return The string
*/
Public   Static String make32 (string plaintext ){
Try {
Messagedigest MD = Messagedigest. getinstance ( " MD5 " );
Md. Update (plaintext. getbytes ());
Byte B [] = Md. Digest ();
Int I;
Stringbuffer Buf =   New Stringbuffer ( "" );
For ( Int Offset =   0 ; Offset < B. length; offset ++ ){
I = B [offset];
If (I <   0 )
I + =   256 ;
If (I <   16 )
Buf. append ( " 0 " );
Buf. append (integer. tohexstring (I ));
}
Return Buf. tostring ();
} Catch (Nosuchalgorithmexception e ){
E. printstacktrace ();
}
Return   Null ;
}

/**
* 16-bit encryption algorithm.
*
* @ Param Plaintext the plain text
*
* @ Return The string
*/
Public   Static String make16 (string plaintext ){
Try {
Messagedigest MD = Messagedigest. getinstance ( " MD5 " );
Md. Update (plaintext. getbytes ());
Byte B [] = Md. Digest ();
Int I;
Stringbuffer Buf =   New Stringbuffer ( "" );
For ( Int Offset =   0 ; Offset < B. length; offset ++ ){
I = B [offset];
If (I <   0 )
I + =   256 ;
If (I <   16 )
Buf. append ( " 0 " );
Buf. append (integer. tohexstring (I ));
}
Return Buf. tostring (). substring ( 8 , 24 );
} Catch (Nosuchalgorithmexception e ){
E. printstacktrace ();
}
Return   Null ;
}

/**
* The main method.
*
* @ Param AGRs the arguments
*/
Public   Static   Void Main (string AGRs []) {
System. Out. println (md5.make32 ( " 1 " )); // Encryption 4
System. Out. println (md5.make16 ( " 1 " ));

}

}

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.