I created a repository on github to write some of the computer security algorithms, and it's time to write MD5 Algorithm, i searched about papers/videos explain the algorithm with examples alongside steps, but i didn't.
I wrote this for step 1 and i don't know if this is correct or not?
//step1
var textP = ToBinaryString(Encoding.UTF8, text);
textP = textP.Length < 448 ? textP + '1' : textP;
while (textP.Length <448)
{
textP += '0';
}
Console.WriteLine(textP);
Second: to step 2, append length
A 64 bit representation of b is appended to the result of the previous step The resulting message has a length that is an exact multiple of 512 bits
it means to append to the 448bits the origin bits of the string?
No, the first step is not correct, as it doesn't pad correctly in case there are fewer than 64 bits left in the block. In that case the padding will have to span two blocks - first put in a 1 and fill the rest with zero's, then create a 448 bit block.
The second sentence is unclear to me. The 64 bits encoding of the input size in bits needs to be added after the padding has taken place.
Note that you're trying to recreate the algorithm description literally. That's not a good idea. You need to process blocks of plaintext, keeping count of the number of bits or bytes and then perform the padding and length encoding when the end of the stream is indicated. You need a 512 bit buffer, an update
and final
method.
Creating the hash by using a string representing binary is not a good idea. You should process bytes and possibly words on the inside. You only need to encode anything for debugging purposes.