Java encryption algorithm
Abstract: Java encryption algorithm
Java encryption algorithm
Author: Java_One Source: http://blog.csdn.net/Java_One
Import java.security .*;
(Public class MessageCheck
Public static void main (String [] args) (
String info1 = "admin";
/ / String info1 = "Hello World!";
String info2 = "Hello World!";
Try (
/ / Generate MessageDigest example, the accounting method
/ / MessageDigest md1 = MessageDigest.getInstance ( "SHA-1");
MessageDigest md1 = MessageDigest.getInstance ( "MD5");
/ / Add a summary of the information to calculate
Md1.update (info1.getBytes ()) / / transform parameter to byte []
/ / Hash completed, a summary of return calculated (for MD5 is 16, SHA is 20)
Byte [] = digA md1.digest ();
/ /
MessageDigest md2 = MessageDigest.getInstance ( "SHA-1");
Md2.update (info2.getBytes ());
Byte [] = digB md2.digest ();
/ / Comparison of the two is the same information
If (md2.isEqual (digA, digB)) (
System.out.println ( "two equal information, inspection normal!");
System.out.println (hasString (digA) + "\ n" + hasString (digB));
)
Else (
System.out.println ( "information does not equal two, Abstract not the same!");
System.out.println (hasString (digA) + "\ n" + hasString (digB));
)
) Catch (NoSuchAlgorithmException e) (
System.out.println ( "illegal Abstract algorithm!");
E.printStackTrace ();
)
)
Public static String hasString (byte [] b) (
String has = "", tmp = "";
For (int i = 0; i <b.length; i + +) (
/ / Hexadecimal form of the unsigned integer return an integer parameters that form the string
Tmp = (java.lang.Integer.toHexString (b [i] & 0XFF));
If (tmp.length () == 1)
Has has + = "0" + tmp;
Else
Tmp = + has has;
If (i <b.length - 1) has has = + ":";
)
Return has.toUpperCase ();
)
)
↑ Back






