Re: How do I get this to work ?
From: Per Vestergaard-Laustsen (pvl_at_capto.dk)
Date: 04/30/03
- Next message: Per Vestergaard-Laustsen: "Re: How do I get this to work ?"
- Previous message: Joe Kaplan: "Re: Retrieving User Name?"
- In reply to: Ivan Medvedev [MS]: "Re: How do I get this to work ?"
- Next in thread: Per Vestergaard-Laustsen: "Re: How do I get this to work ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Wed, 30 Apr 2003 13:02:11 +0200
Thanks, but unfortunately it didn't work. I tried to just change the method from CreateEncryptor to CreateDecryptot in the encrypt function, and got the exact same error. What does 'Bad data' really mean, what is wrong when I get that kind of message ? Is it the length of the argument to be decrypted that is wrong, or is it the bytes in the argument that can only assume certain values ? I do not understand how data can be 'bad' since anything should go, because the user can choose Key, IV and value to en-/decrypt quite freely. I feel that I need more information about the error, in order to shoot it down.
Best regards
Per Vestergaard-Laustsen, Denmark
"Ivan Medvedev [MS]" <ivanmed@online.microsoft.com> skrev i en meddelelse news:evABrqmDDHA.2288@TK2MSFTNGP12.phx.gbl...
Per -
I would recommend using the same method in your Decrypt function as you used in the Encrypt - that is create a CryptoStream with CryptoStreamMode.Write and a decrytor object and write encrypted bytes to it. Then in the same manner you can grab the decrypted bytes from the MemoryStream.ToArray().
If for some reason you have to stay with the current model, don't use BinaryReader - you can read bytes directly off the CryptoStream.
Hope this helps.
--Ivan
This posting is provided "AS IS" with no warranties, and confers no rights.
"Per Vestergaard-Laustsen" <pvl@capto.dk> wrote in message news:uRxqzUYDDHA.1604@TK2MSFTNGP10.phx.gbl...
Well it gets worse.
I have done some tidying up in the code and tried to implement your suggestions, so now I at least get another errormessage, but I am not sure wether it is a step forward or back.
The errormessage now reads 'Stream does not support seeking', when I issue the statement :
"decryptStreamReader.ReadBytes(CInt(decryptStream.Length))".
Can you please help me out.
This is what the code looks like now :
Public Function encrypt(ByRef textPIN As String, ByRef textKey As String, ByRef textInitialValue As String) As String
On Error GoTo ErrorHandler
errPosition = "In encrypt function"
Dim cryptPIN As New MemoryStream()
Dim cryptPINReadBinary As New BinaryReader(cryptPIN)
Dim PIN() As Byte
Dim Key() As Byte
Dim InitialValue() As Byte
PIN = GetEncryptKeyByteArray(textPIN)
Key = GetEncryptKeyByteArray(textKey)
InitialValue = GetEncryptKeyByteArray(textInitialValue)
Dim buffer(128) As Byte
Dim transformation As ICryptoTransform
Dim cryptoService As New System.Security.Cryptography.DESCryptoServiceProvider()
transformation = cryptoService.CreateEncryptor(Key, InitialValue)
Dim encryptStream As New System.Security.Cryptography.CryptoStream(cryptPIN, _
transformation, _
CryptoStreamMode.Write)
encryptStream.Write(PIN, 0, CInt(PIN.Length)) 'encrypt writing to the encryptionstream
encryptStream.FlushFinalBlock() 'empty the temporary stream-buffer by force
encrypt = byte2str(cryptPIN.ToArray())
'close the streams that has been opened
encryptStream.Close()
cryptPIN.Close()
'destroy the objects that has been created and used
cryptPIN = Nothing
cryptPINReadBinary = Nothing
transformation = Nothing
cryptoService = Nothing
encryptStream = Nothing
Return encrypt
ErrorHandler:
textPIN = Err.Description
textKey = errPosition
End Function
Public Function decrypt(ByRef textPIN As String, ByRef textKey As String, ByRef textInitialValue As String) As String
'On Error GoTo ErrorHandler
errPosition = "In decrypt function"
Dim globalException As CryptographicException
Dim i As Integer
Dim cryptPIN As New MemoryStream()
Dim cryptPINWriteBinary As New BinaryWriter(cryptPIN)
Dim PIN(15) As Byte
Dim Key(7) As Byte
Dim InitialValue(7) As Byte
PIN = GetDecryptKeyByteArray(textPIN)
Key = GetEncryptKeyByteArray(textKey)
InitialValue = GetEncryptKeyByteArray(textInitialValue)
Dim buffer(1024) As Byte
Dim decryption As ICryptoTransform
Dim cryptoService As New DESCryptoServiceProvider()
cryptPINWriteBinary.Write(PIN, 0, CInt(PIN.Length))
cryptPINWriteBinary.Flush() 'empty the temporary stream-buffer by force
decryption = cryptoService.CreateDecryptor(Key, InitialValue)
cryptPIN.Seek(0, SeekOrigin.Begin)
'the decryption takes place here
Dim decryptStream As New CryptoStream(cryptPIN, _
decryption, _
CryptoStreamMode.Read)
'definition of a reader in order to be able to read the decrypted PIN from the stream
Dim decryptStreamReader As New BinaryReader(decryptStream)
'reading the decrypted PIN into a textstring
Try
buffer = decryptStreamReader.ReadBytes(CInt(decryptStream.Length))
decryptStream.FlushFinalBlock()
Catch exception As CryptographicException
globalException = exception
GoTo ErrorHandler
End Try
'close the streams that has been opened
cryptPIN.Close()
decryptStream.Close()
'destroy all objects used
cryptPINWriteBinary = Nothing
decryption = Nothing
globalException = Nothing
cryptoService = Nothing
Return decrypt
ErrorHandler:
If globalException.Message = "" Then
textPIN = Err.Description
textKey = errPosition
Else
Dim inner As Exception
textPIN = globalException.Message
inner = globalException.InnerException()
If Not inner Is Nothing Then
textKey = inner.Message
End If
textInitialValue = globalException.Source
End If
End Function
Private Function GetEncryptKeyByteArray(ByVal sPassword As String) As Byte()
Dim byteTemp(7) As Byte
sPassword = sPassword.PadRight(8) ' make sure we have 8 chars
Dim iCharIndex As Integer
For iCharIndex = 0 To sPassword.Length - 1
byteTemp(iCharIndex) = Asc(Mid$(sPassword, iCharIndex + 1, 1))
Next
Return byteTemp
End Function
Private Function GetDecryptKeyByteArray(ByVal sPassword As String) As Byte()
Dim byteTemp(15) As Byte
Dim iCharIndex As Integer
For iCharIndex = 0 To sPassword.Length - 1
byteTemp(iCharIndex) = Asc(Mid$(sPassword, iCharIndex + 1, 1))
Next
Return byteTemp
End Function
Private Function byte2str(ByRef bytes() As Byte) As String
errPosition = "In byte2str"
Dim i As Integer
Dim tmpString As String
Dim tmpStringOneChar As String
tmpString = ""
errPosition = "Before for loop in byte2str"
For i = 1 To bytes.Length - 1
tmpStringOneChar = Chr(bytes(i))
tmpString = tmpString + tmpStringOneChar
errPosition = "In for loop in byte2str"
Next
errPosition = "After for loop in byte2str"
byte2str = tmpString
errPosition = "Before return from byte2str"
Return byte2str
End Function
"Ivan Medvedev [MS]" <ivanmed@online.microsoft.com> skrev i en meddelelse news:OEPI523CDHA.3040@TK2MSFTNGP11.phx.gbl...
> Per -
> I think the problem is that your are not accounting for the changed length
> of the encrypted data. When you encrypt 8 bytes if data the resulting
> cyphertext is usually 16 bytes because of the padding added. In your code
> however you use a 8 byte long buffer to store enciphered bytes, that is why
> you are having problems decrypting. I would recommend using
> MemoryStream.ToArray() method, it is simpler then reading from the stream in
> a conventional way, and it gives you an array of the right size.
> Hope this helps.
> --Ivan
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> "Per Vestergaard-Laustsen" <pvl@capto.dk> wrote in message
> news:etI1N8vCDHA.28568@TK2MSFTNGP10.phx.gbl...
> > When trying to decrypt I get a bad data error.
> > Public Function encrypt(ByRef textPIN As String, ByRef textKey As String,
> > ByRef textInitialValue As String) As String
> >
> > On Error GoTo ErrorHandler
> >
> > errPosition = "In encrypt function"
> >
> > Dim cryptPIN As New MemoryStream()
> >
> > Dim cryptPINReadBinary As New BinaryReader(cryptPIN)
> >
> > Dim PIN() As Byte
> >
> > Dim Key() As Byte
> >
> > Dim InitialValue() As Byte
> >
> > PIN = GetKeyByteArray(textPIN)
> >
> > Key = GetKeyByteArray(textKey)
> >
> > InitialValue = GetKeyByteArray(textInitialValue)
> >
> > Dim buffer(8) As Byte
> >
> > Dim transformation As ICryptoTransform
> >
> > Dim cryptoService As New
> > System.Security.Cryptography.DESCryptoServiceProvider()
> >
> > transformation = cryptoService.CreateEncryptor(Key, InitialValue)
> >
> > Dim encryptStream As New
> System.Security.Cryptography.CryptoStream(cryptPIN,
> > _
> >
> > transformation, _
> >
> > CryptoStreamMode.Write)
> >
> > encryptStream.Write(PIN, 0, CInt(PIN.Length)) 'encrypt writing to the
> > encryptionstream
> >
> > encryptStream.FlushFinalBlock() 'empty the temporary stream-buffer by
> force
> >
> > cryptPIN.Seek(0, SeekOrigin.Begin)
> >
> > cryptPINReadBinary.Read(buffer, 0, cryptPIN.Length) 'read back into the
> > buffer the encrypted PIN from the memorystream
> >
> > errPosition = "Before call to byte2str"
> >
> > encrypt = byte2str(buffer)
> >
> > errPosition = "After call to byte2str"
> >
> > 'close the streams that has been opened
> >
> > encryptStream.Close()
> >
> > cryptPIN.Close()
> >
> > 'destroy the objects that has been created and used
> >
> > cryptPIN = Nothing
> >
> > cryptPINReadBinary = Nothing
> >
> > transformation = Nothing
> >
> > cryptoService = Nothing
> >
> > encryptStream = Nothing
> >
> > Return encrypt
> >
> > ErrorHandler:
> >
> > textPIN = Err.Description
> >
> > textKey = errPosition
> >
> > End Function
> >
> > Public Function decrypt(ByRef textPIN As String, ByRef textKey As String,
> > ByRef textInitialValue As String) As String
> >
> > 'On Error GoTo ErrorHandler
> >
> > errPosition = "In decrypt function"
> >
> > Dim globalException As CryptographicException
> >
> > Dim i As Integer
> >
> > Dim cryptPIN As New MemoryStream()
> >
> > Dim cryptPINWriteBinary As New BinaryWriter(cryptPIN)
> >
> > Dim PIN() As Byte
> >
> > Dim Key() As Byte
> >
> > Dim InitialValue() As Byte
> >
> > PIN = GetKeyByteArray(textPIN)
> >
> > Key = GetKeyByteArray(textKey)
> >
> > InitialValue = GetKeyByteArray(textInitialValue)
> >
> > Dim buffer(1024) As Byte
> >
> > Dim decryption As ICryptoTransform
> >
> > Dim cryptoService As New DESCryptoServiceProvider()
> >
> > cryptPINWriteBinary.Write(PIN, 0, CInt(PIN.Length))
> >
> > cryptPINWriteBinary.Flush() 'empty the temporary stream-buffer by force
> >
> > decryption = cryptoService.CreateDecryptor(Key, InitialValue)
> >
> > cryptPIN.Seek(0, SeekOrigin.Begin)
> >
> > 'the decryption takes place here
> >
> > Dim decryptStream As New CryptoStream(cryptPIN, _
> >
> > decryption, _
> >
> > CryptoStreamMode.Read)
> >
> > 'definition of a reader in order to be able to read the decrypted PIN from
> > the stream
> >
> > Dim decryptStreamReader As New BinaryReader(decryptStream)
> >
> > 'readtoend delivers a string, so we need a temporary string to store it in
> >
> > Dim txtString As String
> >
> > 'reading the decrypted PIN into a textstring
> >
> > Try
> >
> > decryptStream.Read(buffer, 0, PIN.Length)
> >
> > buffer = decryptStreamReader.ReadBytes(CInt(PIN.Length))
> >
> > Catch exception As CryptographicException
> >
> > globalException = exception
> >
> > GoTo ErrorHandler
> >
> > End Try
> >
> > 'close the streams that has been opened
> >
> > cryptPIN.Close()
> >
> > decryptStream.Close()
> >
> > 'destroy all objects used
> >
> > cryptPIN = Nothing
> >
> > cryptPINWriteBinary = Nothing
> >
> > decryption = Nothing
> >
> > decryptStream = Nothing
> >
> > globalException = Nothing
> >
> > cryptoService = Nothing
> >
> > Return decrypt
> >
> > ErrorHandler:
> >
> > If globalException.Message = "" Then
> >
> > textPIN = Err.Description
> >
> > textKey = errPosition
> >
> > Else
> >
> > Dim inner As Exception
> >
> > textPIN = globalException.Message
> >
> > inner = globalException.InnerException()
> >
> > If Not inner Is Nothing Then
> >
> > textKey = inner.Message
> >
> > End If
> >
> > textInitialValue = globalException.Source
> >
> > End If
> >
> > End Function
> >
> > How do I avoid that ?
> >
> > Best regards Per Vestergaard-Laustsen, Denmark
> >
> >
>
>
- Next message: Per Vestergaard-Laustsen: "Re: How do I get this to work ?"
- Previous message: Joe Kaplan: "Re: Retrieving User Name?"
- In reply to: Ivan Medvedev [MS]: "Re: How do I get this to work ?"
- Next in thread: Per Vestergaard-Laustsen: "Re: How do I get this to work ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]