Sie sind auf Seite 1von 7

MD5 Algorithm Implementation Using Java:

Description: This program represents the implementation of MD5 algorithm. MD5 algorithm can be used as a digital signature mechanism. Takes as input a message of arbitrary length and produces as output a 128 bit fingerprint or message digest of the input. MD5 algorithm generates the output as 128 bit hashcode for the input !tring of any length. The steps in"ol"ed in MD5 algorithm are 1. #ad message so its length is $$8 mod 512 2. %ppend a &$'bit original length "alue to message (. )nitialise $'*ord +128'bit, MD buffer +%-.-/-D, $. #rocess message in 1&'*ord +512'bit, blocks: 1. 0sing $ rounds of 1& bit operations on message block 1 buffer 2. %dd output to buffer input to form ne* buffer "alue 5. 2utput hash "alue is the final buffer "alue This program uses get.ytes+, method to encode the !tring into a se3uence of bytes using the platform4s default charset- storing the result into a ne* byte array. These byte array data is con"erted into hashcode by in"oking the follo*ing methods update +byte56,- md5final +byte56,dump.ytes +byte56,. MD5 %lgorithm )mplementation 0sing 7a"a:
//MD5.java import java.io.*; import java.net.*; import java.util.*; import java.math.*; public class MD5 { int buf[]; long bits; byte in[]; int inint[]; public MD5 ! { buf " ne# int[$]; // fill the hash accumulator #ith a see% value buf[&] " &'()$5*+&,;

buf[,] " &'efc%ab-.; buf[*] " &'.-ba%cfe; buf[+] " &',&+*5$)(; // initially/ #e0ve hashe% 1ero bits bits " &2; in " ne# byte[($]; inint " ne# int[,(]; 3 public voi% up%ate byte[] ne#buf! { up%ate ne#buf/ &/ ne#buf.length!; 3 public voi% up%ate byte[] ne#buf/ int length! { up%ate ne#buf/ &/ length!; 3 public voi% up%ate byte[] ne#buf/ int bufstart/ int buflen! { int t; int len " buflen; // shash ol% bits value for the 45ytes alrea%y in4 computation // just belo#. t " int! bits; // int! cast shoul% just %rop high bits/ 6 hope /* up%ate bitcount */ /* the 7 co%e use% t#o +*8bit ints separately/ an% carefully * ensure% that the carry carrie%. * 9ava has a ($8bit long/ #hich is just #hat the co%e really #ants. */ bits :" long! len;;+!; t " t <<< +! = &'+f; /* 5ytes alrea%y in this8<in */

/* >an%le any lea%ing o%%8si1e% chun?s */ /* that is/ any left8over chun? left by last up%ate ! */ if t@"&! { int p " t; t " ($ 8 t; if len ; t! { Aystem.arraycopy ne#buf/ bufstart/ in/ p/ len!; return; 3 Aystem.arraycopy ne#buf/ bufstart/ in/ p/ t!; transform !; bufstart :" t; len 8" t; 3

/* Brocess %ata in ($8byte chun?s */ #hile len <" ($! { Aystem.arraycopy ne#buf/ bufstart/ in/ &/ ($!; transform !; bufstart :" ($; len 8" ($; 3 /* >an%le any remaining bytes of %ata. */ /* that is/ stash them for the ne't up%ate !. */ Aystem.arraycopy ne#buf/ bufstart/ in/ &/ len!; 3 /* * Cinal #rapup 8 pa% to ($8byte boun%ary #ith the bit pattern * , &* ($8bit count of bits processe%/ MA58first! */ public voi% m%5final byte[] %igest! { /* 4final4 is a poor metho% name in 9ava. Dv! */ int count; int p; /* 7ompute number of bytes mo% ($ */ count " int! bits <<< +! = &'+C!; /* Aet the first char of pa%%ing to &'-&. Ehis is safe since there is al#ays at least one byte free */ p " count; in[p::] " byte! &'-&; /* 5ytes of pa%%ing nee%e% to ma?e ($ bytes */ count " ($ 8 , 8 count; /* Ba% out to 5( mo% ($ */ if count ; -! { /* E#o lots of pa%%ingD Ba% the first bloc? to ($ bytes */ 1ero5yteFrray in/ p/ count!; transform !; /* Go# fill the ne't bloc? #ith 5( bytes */ 1ero5yteFrray in/ &/ 5(!; 3 else { /* Ba% bloc? to 5( bytes */ 1ero5yteFrray in/ p/ count 8 -!; 3 /* Fppen% length in bits an% transform */ // 7oul% use a BHEI($56E... func here. Ehis is a fairly // %irect translation from the 7 co%e/ #here bits #as an array // of t#o +*8bit ints. int lo#bits " int! bits;

int highbits " int! bits <<< +*!; BHEI+*56EI2A5IC6JAE in/ 5(/ lo#bits!; BHEI+*56EI2A5IC6JAE in/ (&/ highbits!; transform !; BHEI+*56EI2A5IC6JAE BHEI+*56EI2A5IC6JAE BHEI+*56EI2A5IC6JAE BHEI+*56EI2A5IC6JAE %igest/ &/ buf[&]!; %igest/ $/ buf[,]!; %igest/ -/ buf[*]!; %igest/ ,*/ buf[+]!;

/* 1ero sensitive %ata */ /* notice this misses any snea?ing out on the stac?. Ehe 7 * version uses registers in some spots/ perhaps because * they care about this. */ 1ero5yteFrray in!; 1ero6ntFrray buf!; bits " &; 1ero6ntFrray inint!; 3 public static voi% main Atring args[]! { // Ehis main ! metho% #as create% to easily test // this class. 6t hashes #hatever0s on Aystem.in. byte buf[] " ne# byte[+.)]; // arbitrary buffer length %esigne% to irritate up%ate ! int rc; MD5 m% " ne# MD5 !; byte out[] " ne# byte[,(]; int i; int len " &; try { #hile rc " Aystem.in.rea% buf/ &/ +.)!! < &! { m%.up%ate buf/ rc!; len :" rc; 3 3 catch 6KL'ception e'! { e'.printAtac?Erace !; return; 3 m%.m%5final out!; Aystem.out.println 4file lengthD 4:len!; Aystem.out.println 4hashD 4:%ump5ytes out!!; 3

private voi% 1ero5yteFrray byte[] a! {

1ero5yteFrray a/ &/ a.length!; 3 private voi% 1ero5yteFrray byte[] a/ int start/ int length! { set5yteFrray a/ byte! &/ start/ length!; 3 private voi% set5yteFrray byte[] a/ byte val/ int start/ int length! { int i; int en% " start:length; for i"start; i;;s M #<<< +*8s!; # :" '; return #; 3 private voi% transform ! { /* loa% in[] byte array into an internal int array */ int i; int[] inint " ne# int[,(]; for i"&; i;,(; i::! { inint[i] " NLEI+*56EI2A5IC6JAE in/ $*i!; 3 int a/ b/ c/ %; a " buf[&]; b " buf[,]; c " buf[*]; % " buf[+]; a " MD5AELB C,/ a/ b/ c/ %/ inint[&] % " MD5AELB C,/ %/ a/ b/ c/ inint[,] c " MD5AELB C,/ c/ %/ a/ b/ inint[*] b " MD5AELB C,/ b/ c/ %/ a/ inint[+] a " MD5AELB C,/ a/ b/ c/ %/ inint[$] % " MD5AELB C,/ %/ a/ b/ c/ inint[5] c " MD5AELB C,/ c/ %/ a/ b/ inint[(] b " MD5AELB C,/ b/ c/ %/ a/ inint[)] a " MD5AELB C,/ a/ b/ c/ %/ inint[-] % " MD5AELB C,/ %/ a/ b/ c/ inint[.] c " MD5AELB C,/ c/ %/ a/ b/ inint[,&] b " MD5AELB C,/ b/ c/ %/ a/ inint[,,] a " MD5AELB C,/ a/ b/ c/ %/ inint[,*] % " MD5AELB C,/ %/ a/ b/ c/ inint[,+] c " MD5AELB C,/ c/ %/ a/ b/ inint[,$] b " MD5AELB C,/ b/ c/ %/ a/ inint[,5] a " MD5AELB C*/ a/ b/ c/ %/ inint[,] % " MD5AELB C*/ %/ a/ b/ c/ inint[(] c " MD5AELB C*/ c/ %/ a/ b/ inint[,,] b " MD5AELB C*/ b/ c/ %/ a/ inint[&] : &'%)(aa$)-/ )!; : &'e-c)b)5(/ ,*!; : &'*$*&)&%b/ ,)!; : &'c,b%ceee/ **!; : &'f5)c&faf/ )!; : &'$)-)c(*a/ ,*!; : &'a-+&$(,+/ ,)!; : &'f%$(.5&,/ **!; : &'(.-&.-%-/ )!; : &'-b$$f)af/ ,*!; : &'ffff5bb,/ ,)!; : &'-.5c%)be/ **!; : &'(b.&,,**/ )!; : &'f%.-),.+/ ,*!; : &'a().$+-e/ ,)!; : &'$.b$&-*,/ **!; : &'f(,e*5(*/ 5!; : &'c&$&b+$&/ .!; : &'*(5e5a5,/ ,$!; : &'e.b(c)aa/ *&!;

a " MD5AELB C*/ a/ b/ c/ %/ inint[5] % " MD5AELB C*/ %/ a/ b/ c/ inint[,&] c " MD5AELB C*/ c/ %/ a/ b/ inint[,5] b " MD5AELB C*/ b/ c/ %/ a/ inint[$] a " MD5AELB C*/ a/ b/ c/ %/ inint[.] % " MD5AELB C*/ %/ a/ b/ c/ inint[,$] c " MD5AELB C*/ c/ %/ a/ b/ inint[+] b " MD5AELB C*/ b/ c/ %/ a/ inint[-] a " MD5AELB C*/ a/ b/ c/ %/ inint[,+] % " MD5AELB C*/ %/ a/ b/ c/ inint[*] c " MD5AELB C*/ c/ %/ a/ b/ inint[)] b " MD5AELB C*/ b/ c/ %/ a/ inint[,*] a " MD5AELB C+/ a/ b/ c/ %/ inint[5] % " MD5AELB C+/ %/ a/ b/ c/ inint[-] c " MD5AELB C+/ c/ %/ a/ b/ inint[,,] b " MD5AELB C+/ b/ c/ %/ a/ inint[,$] a " MD5AELB C+/ a/ b/ c/ %/ inint[,] % " MD5AELB C+/ %/ a/ b/ c/ inint[$] c " MD5AELB C+/ c/ %/ a/ b/ inint[)] b " MD5AELB C+/ b/ c/ %/ a/ inint[,&] a " MD5AELB C+/ a/ b/ c/ %/ inint[,+] % " MD5AELB C+/ %/ a/ b/ c/ inint[&] c " MD5AELB C+/ c/ %/ a/ b/ inint[+] b " MD5AELB C+/ b/ c/ %/ a/ inint[(] a " MD5AELB C+/ a/ b/ c/ %/ inint[.] % " MD5AELB C+/ %/ a/ b/ c/ inint[,*] c " MD5AELB C+/ c/ %/ a/ b/ inint[,5] b " MD5AELB C+/ b/ c/ %/ a/ inint[*] a " MD5AELB C$/ a/ b/ c/ %/ inint[&] % " MD5AELB C$/ %/ a/ b/ c/ inint[)] c " MD5AELB C$/ c/ %/ a/ b/ inint[,$] b " MD5AELB C$/ b/ c/ %/ a/ inint[5] a " MD5AELB C$/ a/ b/ c/ %/ inint[,*] % " MD5AELB C$/ %/ a/ b/ c/ inint[+] c " MD5AELB C$/ c/ %/ a/ b/ inint[,&] b " MD5AELB C$/ b/ c/ %/ a/ inint[,] a " MD5AELB C$/ a/ b/ c/ %/ inint[-] % " MD5AELB C$/ %/ a/ b/ c/ inint[,5] c " MD5AELB C$/ c/ %/ a/ b/ inint[(] b " MD5AELB C$/ b/ c/ %/ a/ inint[,+] a " MD5AELB C$/ a/ b/ c/ %/ inint[$] % " MD5AELB C$/ %/ a/ b/ c/ inint[,,] c " MD5AELB C$/ c/ %/ a/ b/ inint[*] b " MD5AELB C$/ b/ c/ %/ a/ inint[.] buf[&] :" a; buf[,] :" b; buf[*] :" c; buf[+] :" %;

: &'%(*f,&5%/ 5!; : &'&*$$,$5+/ .!; : &'%-a,e(-,/ ,$!; : &'e)%+fbc-/ *&!; : &'*,e,c%e(/ 5!; : &'c++)&)%(/ .!; : &'f$%5&%-)/ ,$!; : &'$55a,$e%/ *&!; : &'a.e+e.&5/ 5!; : &'fcefa+f-/ .!; : &'()(f&*%./ ,$!; : &'-%*a$c-a/ *&!; : &'fffa+.$*/ $!; : &'-)),f(-,/ ,,!; : &'(%.%(,**/ ,(!; : &'f%e5+-&c/ *+!; : &'a$beea$$/ $!; : &'$b%ecfa./ ,,!; : &'f(bb$b(&/ ,(!; : &'bebfbc)&/ *+!; : &'*-.b)ec(/ $!; : &'eaa,*)fa/ ,,!; : &'%$ef+&-5/ ,(!; : &'&$--,%&5/ *+!; : &'%.%$%&+./ $!; : &'e(%b..e5/ ,,!; : &',fa*)cf-/ ,(!; : &'c$ac5((5/ *+!; : &'f$*.**$$/ (!; : &'$+*aff.)/ ,&!; : &'ab.$*+a)/ ,5!; : &'fc.+a&+./ *,!; : &'(55b5.c+/ (!; : &'-f&ccc.*/ ,&!; : &'ffeff$)%/ ,5!; : &'-5-$5%%,/ *,!; : &'(fa-)e$f/ (!; : &'fe*ce(e&/ ,&!; : &'a+&,$+,$/ ,5!; : &'$e&-,,a,/ *,!; : &'f)5+)e-*/ (!; : &'b%+af*+5/ ,&!; : &'*a%)%*bb/ ,5!; : &'eb-(%+.,/ *,!;

3 private int NLEI+*56EI2A5IC6JAE byte[] b/ int off! { return int! b[off:&]=&'ff!! M int! b[off:,]=&'ff! ;; -! M int! b[off:*]=&'ff! ;; ,(! M int! b[off:+]=&'ff! ;; *$!; 3 private voi% BHEI+*56EI2A5IC6JAE byte[] b/ int off/ int value! { b[off:&] " byte! value = &'ff!; b[off:,] " byte! value << -!= &'ff!; b[off:*] " byte! value << ,(!= &'ff!; b[off:+] " byte! value << *$!= &'ff!; 3

public static Atring %ump5ytes byte[] bytes! { int i; Atring5uffer sb " ne# Atring5uffer !; for i"&; i; *! { s " 4&4:s; 3 if s.length ! < *! { s " s.substring s.length !8*!; 3 sb.appen% s!; 3 return sb.toAtring !; 3 3 //MD56mplementation.java public class MD56mplementation{ public static voi% main Atring args[]!{ Atring message"47omputer4; Aystem.out.println 46nput Atring D 4:message!; byte buf[] " message.get5ytes !; MD5 m% " ne# MD5 !; byte out[] " ne# byte[,(]; m%.up%ate buf!; m%.m%5final out!; Atring nhash " m%.%ump5ytes out!; Aystem.out.println 4MD5 Kutput >ashco%e! D 4:nhash!; 3 3

Das könnte Ihnen auch gefallen