Uso de DES en .net

13/08/2004 - 20:09 por Halber | Informe spam
Buen dia. Necesito implementar una rutina que trabaje con
DES pero no he podido usar de manera acertada las clases
de System.Security.Cryptography .. Los datos que deberia
obtener son :

DES (ECB Mode)
Cryptographic Key = 0123456789abcdef

texto "Now is the time for all "

resultado esperado
"3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53"


DES (CBD Mode)
Cryptographic Key = 0123456789abcdef
Initialization Vector = 1234567890abcdef

texto "Now is the time for all "

resultado esperado
"e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6"

Estos datos de prueba fueron tomados del
http://www.nist.gov

Agradezco su ayuda

Halber A.
 

Leer las respuestas

#1 Eduardo A. Morcillo [MS MVP VB]
14/08/2004 - 07:41 | Informe spam
Esta funcion funciona (suponiendo que el largo del texto es siempre multiplo
de 8):

Function EncryptString_DES_CBC( _
ByVal text As String, _
ByVal key() As Byte, _
ByVal iv() As Byte) As String

Dim des As New DESCryptoServiceProvider
Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(text)

des.Mode = CipherMode.CBC
des.Key = key
des.IV = iv

Dim transform As ICryptoTransform = des.CreateEncryptor()

Dim enc(data.Length - 1) As Byte
For i As Integer = 0 To data.Length - 1 Step 8
transform.TransformBlock(data, i, 8, enc, i)
Next

Return ToHex(enc)

End Function

Function ToHex(ByVal data() As Byte) As String
Dim hex As New System.Text.StringBuilder

For Each b As Byte In data
hex.AppendFormat("{0:X2}", b)
Next

Return hex.ToString

End Function

MessageBox.Show(EncryptString_DES_CBC("Now is the time for all ", _
New Byte() {&H1, &H23, &H45, &H67, &H89, &HAB, &HCD, &HEF}, _
New Byte() {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}))

Eduardo A. Morcillo [MS MVP VB]
http://www.mvps.org/emorcillo

Preguntas similares