Avatar billede super_g Nybegynder
01. september 2003 - 20:45 Der er 2 kommentarer og
1 løsning

Convert.ToBase64String(byte(15)).length = 24 og ikke 22 ?

Jeg har følgende funktion:

Public Function MD5(ByVal value As String) As String
  Dim md5Hasher As New MD5CryptoServiceProvider()
  Dim hashedBytes As Byte()
  Dim encoder As New System.Text.UTF8Encoding()
  hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(value))
  Return Convert.ToBase64String(hashedBytes)
End Function

Hvis jeg kalder MD5("Test") får jeg LxmEfVUC9CAmjiNyVphWw== (length = 24), men burde jeg teoretisk ikke få en length på 22??  ((16byte*8bit pr. byte)/6bit pr. tegn) = 21,33 ~ 22 tegn..

Er min MD5 funktion forkert ? Eller hvad går der galt (hvis noget)?
Avatar billede arne_v Ekspert
01. september 2003 - 21:39 #1
De 24 tegn er OK.

http://rfc.sunsite.dk/rfc/rfc1864.html

The output of the MD5 algorithm is a 128 bit digest.  When viewed in
  network byte order (big-endian order), this yields a sequence of 16
  octets of binary data.  These 16 octets are then encoded according to
  the base64 algorithm in order to obtain the value that is placed in
  the Content-MD5 field.  Thus, if the application of the MD5 algorithm
  over the raw data of a MIME entity results in a digest having the
  (unlikely) value of "Check Integrity!", then that MIME entity's
  header could contain the field

        Content-MD5:  Q2hlY2sgSW50ZWdyaXR5IQ==

(tæl selv)
Avatar billede arne_v Ekspert
01. september 2003 - 21:43 #2
Og forklaringen er her:

http://rfc.sunsite.dk/rfc/rfc2045.html

  Special processing is performed if fewer than 24 bits are available
  at the end of the data being encoded.  A full encoding quantum is
  always completed at the end of a body.  When fewer than 24 input bits
  are available in an input group, zero bits are added (on the right)
  to form an integral number of 6-bit groups.  Padding at the end of
  the data is performed using the "=" character.  Since all base64
  input is an integral number of octets, only the following cases can
  arise: (1) the final quantum of encoding input is an integral
  multiple of 24 bits; here, the final unit of encoded output will be
  an integral multiple of 4 characters with no "=" padding, (2) the
  final quantum of encoding input is exactly 8 bits; here, the final
  unit of encoded output will be two characters followed by two "="
  padding characters, or (3) the final quantum of encoding input is
  exactly 16 bits; here, the final unit of encoded output will be three
  characters followed by one "=" padding character.

Det skal altid være et multipla af 3 bytes. Og da 21 ikek er nok så må
det jo blive 24 med 2 padding bytes (=).
Avatar billede super_g Nybegynder
01. september 2003 - 21:56 #3
Det kan man da kalde et brugbart svar :) Takker
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester