Import java. Security. messagedigest;
Import java. Security. nosuchalgorithmexception;
Public class sha256test {
Public static final string algorithm = "SHA-256 ";
Public static string sha256encrypt (string orignal ){
Messagedigest MD = NULL;
Try {
MD = messagedigest. getinstance (algorithm );
} Catch (nosuchalgorithmexception e ){
E. printstacktrace ();
}
If (null! = Md ){
Byte [] origbytes = orignal. getbytes ();
Md. Update (origbytes );
Byte [] digestres = md. Digest ();
String digeststr = getdigeststr (digestres );
Return digeststr;
}
Return NULL;
}
Private Static string getdigeststr (byte [] origbytes ){
String tempstr = NULL;
Stringbuilder STB = new stringbuilder ();
For (INT I = 0; I <origbytes. length; I ++ ){
// System. Out. println ("and by bit:" + (origbytes [I] & 0xff ));
// System. Out. println ("No and:" + origbytes [I]);
// System. Out. println ("---------------------------------------------");
// Here, bitwise AND are used to take the correct integer when the bytes are rounded up. An int in Java is 4 bytes.
// If the maximum bit of origbytes [I] is 1, the first three bytes of the int are filled with 1 when it is converted to int.
Tempstr = integer. tohexstring (origbytes [I] & 0xff );
If (tempstr. Length () = 1 ){
STB. append ("0 ");
}
STB. append (tempstr );
}
Return Stb. tostring ();
}
Public static void main (string [] ARGs ){
System. Out. println (sha256encrypt ("aaaa11 "));
}
}