まず、次の図からMD5暗号化の実装を理解しましょう。
パッケージcom.pb; import java.io.unsupportedencodingexception; Import java.security.messagegest; Import java.security.nosuchalgorithmexception; Import java.util.scanner;/** md5*1。情報の概要*4。32ビット検索でバイト[]をヘキサデシマル文字列に変換*/public class md5 {/**パラメーター文字列に渡されてmd5を生成*/public void generatemd5(string input){try {// 1。 MESSAGED GIGEST情報サマリーオブジェクトを初期化し、ケースに依存しないMESSAGEDGEST MD = MESSAGEDGEST.GETINSTANCE( "MD5")なしでMD5として指定します。 // 2。計算する文字列に渡されて要約情報を更新すると、渡されたバイト配列バイト[]、// getBytes()メソッド//を使用して文字列をバイト配列に変換します。 // 3。情報概要digest()method // return return値はバイト配列バイト[] hashcode = md.digest(); // 4。バイトを変換します[] 32ビット検索で16進数に変換します// stringbufferオブジェクトを宣言して、最後の値stringbuffer sb = new StringBuffer()を保存します。 //(バイトB:ハッシュコード)のバイト配列を転送{//配列コンテンツをhexadecimal、sb.append(character.fordigit(b >> 4&0xf、16)); // 32ビット16進数Sb.Append(character.fordigit(b&0xf、16))に転送します。 } system.out.println( "暗号化後の結果は"+sb.tostring()); } catch(nosuchalgorithmexception e){// todo auto-fenated catch block e.printstacktrace(); } catch(unsupportedencodingexception e){// todo auto-enerated catch block e.printstacktrace(); }} public static void main(string [] args){// md5 class md5 md5 = new md5()を暗号化するオブジェクトを宣言します。 //スキャナーを使用して、キャラクタースキャナースキャナー= new Scanner(system.in)を入力します。 System.out.println( "暗号化されるコンテンツを入力してください:"); string input = scanner.nextline(); //暗号化方法md5.generatemd5(input)を呼び出す; }}結果:
暗号化するコンテンツを入力してください:
MD5の暗号化プロセスを学習した後の結果は、B826CDAC46F01DCC8CC60A76CEBF858です。
2番目のコード:
パッケージtest.md5; import java.security.messagegest; public class md5util {public final static string md5(string s){char hexdigits [] = {'0'、 '1'、 '2'、 '3'、 '4'、 '5'、 '6'、 '7'、 '8'、 '9'、 ''、 'b'、 'c'、 'd'、 'e'、 'f'}; try {byte [] btinput = s.getBytes(); // MD5ダイジェストアルゴリズムのMESSAGED GIGEST MDINST = MESSAGEDGEST.GETINSTANCE( "MD5")のMESSAGEDGESTオブジェクトを取得します。 // Digest mdinst.update(btinput)を更新します。 // ciphertext byte [] md = mdinst.digest()を取得します。 // ciphertextを16進ストリングフォームint j = md.lengthに変換します。 char str [] = new char [j * 2]; int k = 0; for(int i = 0; i <j; i ++){byte byte0 = md [i]; str [k ++] = hexdigits [byte0 >>> 4&0xf]; str [k ++] = hexdigits [byte0&0xf]; } new String(str)を返します。 } catch(Exception e){e.printstacktrace(); nullを返します。 }} public static void main(string [] args){system.out.println(md5util.md5( "20121221")); System.out.println(md5util.md5( "necryption")); }}3番目のコード: MD5暗号化アルゴリズムのJava実装
その他; Import Java.security.MessageGest; Import Java.security.NosuchalgorithMexception;/** MD5 Algorithm*/Public Array Private final Static String [] Strdigits = {"0"、 "1"、 "2"、 "4" "7" "7" "7" "7" "7" "7" "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7" 7 "7"、 "a"、 "b"、 "c"、 "d"、 "e"、 "f"}; public Md5(){} //フォームを数値として返すprivate private static string bytetoarraystring(byte bbyte){int iret = bbyte; // system.out.println( "iret ="+iret); if(iret <0){iRet += 256; } int id1 = iret / 16; int id2 = iret%16; return strdigits [id1] + strdigits [id2]; } //返されるフォームは、桁のprivate private static string bytetonum(byte bbyte){int iret = bbyte; System.out.println( "IRET1 =" + IRET); if(iret <0){iRet += 256; } return string.valueof(iret); } //バイト配列を16進列に変換するprivate static string bytetostring(byte [] bbyte){stringbuffer sbuffer = new StringBuffer(); for(int i = 0; i <bbyte.length; i ++){sbuffer.append(bytetoarraystring(bbyte [i])); } sbuffer.toString()を返します。 } public static string getMd5Code(string strobj){string resultString = null; try {resultString = new String(strobj); MESSAGEDGEST MD = MESSAGEDGEST.GETINSTANCE( "MD5"); // md.digest()この関数の返品値は、ハッシュresults tring = bytetostring(md.digest(strobj.getbytes())を格納するバイト配列です。 } catch(nosuchalgorithmexception ex){ex.printstacktrace(); } return resultString; } public static void main(string [] args){md5 getMd5 = new Md5(); system.out.println(getmd5.getmd5code( "000000")); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。