// 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 " ));
}
}