Issues with using .NET MD5

pdf , csharp , md5 United States
  • 11 years ago
    Hello, I am working on implementing some parts of the PDF spec. Currently I am working on getting decryption working Most all of the code I have seen is similiar too pseudocode: Md5 m = new MD5(); m.Init(); m.Update(OwnerPasswordHash); m.Update(Permssions); m.Update(Id[0]); Hash returnHash = m.Finalize(); I have tried doing this with C# in several ways here is the last thing I have tried. MD5 m = new MD5CryptoServiceProvider(); m.Initialize(); int return1 = m.TransformBlock(Padding,0,Padding.Length,Padding,0); byte[] o = Encoding.UTF8.GetBytes(((PdfLiteralString)(encryptionDictionary["O"])).Value); int return2 = m.TransformBlock(o,0, o.Length, o, 0); UInt32 p = (UInt32)((PdfInteger)(encryptionDictionary["P"])).Value; byte[] pBytes = BitConverter.GetBytes(p); int return3 = m.TransformBlock(BitConverter.GetBytes(p),0,BitConverter.GetBytes(p).Length,BitConverter.GetBytes(p),0); byte[] idbytes = Utility.ConvertCharArrayToByteArray(((PdfHexString)id[0]).Value.ToCharArray()); byte[] hash = m.TransformFinalBlock(idbytes, 0, idbytes.Length); but the hash comes back 32 bytes long ... should be 128 and beyond that it may still have other problems as there are still steps remaining to get the key. Any help would be greatly appreciated
  • 11 years ago
    Figured out the key size issue but still having problems with the md5 not being accurate. I would still welcome any pointers from anyone who has implemented this before, but this specific issue is taken care of. J
  • 11 years ago
    Alright could still use some help so heres an update: Ok so here is how the first part of pdf encryption/decryption works in PERL ... (i have verified that this code works); my $md5 = new MD5(); $md5->add($upass); $md5->add($owner_hash); $md5->add($something else...) #after a few more you get my $digest = md5->digest(); so heres the current relevent c# code MD5 m = MD5.Create("MD5"); m.Initialize(); int return1 = m.TransformBlock(Padding,0,Padding.Length,Padding,0); int return2 = m.TransformBlock(o,0, o.Length, o, 0); int return3 = m.TransformBlock(pBytes,0,pBytes.Length,pBytes,0); byte[] hash = m.TransformFinalBlock(idbytes, 0, idbytes.Length); I have check and made sure all the values being used are the same as those in Perl. I even went so far as to do MD5 checksums on each value individually in both perl and c# and they match. Any thoughts ???
  • 11 years ago
    Problem solved. If anyone is interested in seeing the code...just let me know.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The greatest performance improvement of all is when a system goes from not-working to working.” - John Ousterhout