String missing ending quote

W

wesbland

From my understanding, when a string is stored in VB.NET and you look
at it in the debugger, it has a quote on both sides to signify that it
is a string as opposed to a char or int or whatever. I've got a
simple program here (I actually found it on the web somewhere, but I'm
looking through it) that doesn't seem to have that ending quote at
when it gets down to the end.

In this program it doesn't make much of a difference, but I've
modified it a little and used it in another one where it needs to
decompress the string after it decrypts it and not having that ending
quote makes it crash.

Can anyone tell me what's going on here that makes that quote go away.

Also, I can't add anything to the end of the string when it comes out
of the last decryptTextFromMemory. These two problems are probably
related.

Thanks ahead of time

-----------------------------------------------------------------

Imports System.Security.Cryptography
Imports System.Text
Imports System.IO

Module TripleDESCSPSample

Sub Main()
Try
' Create a new TripleDESCryptoServiceProvider object
' to generate a key and initialization vector (IV).
Dim tDESalg As New TripleDESCryptoServiceProvider

' Create a string to encrypt.
Dim sData As String = "Here is some data to encrypt."

' Encrypt the string to an in-memory buffer.
Dim Data As Byte() = EncryptTextToMemory(sData,
tDESalg.Key, tDESalg.IV)

' Decrypt the buffer back to a string.
Dim Final As String = DecryptTextFromMemory(Data,
tDESalg.Key, tDESalg.IV)

' Display the decrypted string to the console.
Console.WriteLine(Final)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub


Function EncryptTextToMemory(ByVal Data As String, ByVal Key() As
Byte, ByVal IV() As Byte) As Byte()
Try
' Create a MemoryStream.
Dim mStream As New MemoryStream

' Create a CryptoStream using the MemoryStream
' and the passed key and initialization vector (IV).
Dim cStream As New CryptoStream(mStream, _
New
TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV), _
CryptoStreamMode.Write)

' Convert the passed string to a byte array.
Dim toEncrypt As Byte() = New
ASCIIEncoding().GetBytes(Data)

' Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length)
cStream.FlushFinalBlock()

' Get an array of bytes from the
' MemoryStream that holds the
' encrypted data.
Dim ret As Byte() = mStream.ToArray()

' Close the streams.
cStream.Close()
mStream.Close()

' Return the encrypted buffer.
Return ret
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: {0}",
e.Message)
Return Nothing
End Try
End Function


Function DecryptTextFromMemory(ByVal Data() As Byte, ByVal Key()
As Byte, ByVal IV() As Byte) As String
Try
' Create a new MemoryStream using the passed
' array of encrypted data.
Dim msDecrypt As New MemoryStream(Data)

' Create a CryptoStream using the MemoryStream
' and the passed key and initialization vector (IV).
Dim csDecrypt As New CryptoStream(msDecrypt, _
New
TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV), _
CryptoStreamMode.Read)

' Create buffer to hold the decrypted data.
Dim fromEncrypt(Data.Length) As Byte

' Read the decrypted data out of the crypto stream
' and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

'Convert the buffer into a string and return it.
Return New ASCIIEncoding().GetString(fromEncrypt)
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: {0}",
e.Message)
Return Nothing
End Try
End Function
End Module
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

at it in the debugger, it has a quote on both sides to signify that it
is a string as opposed to a char or int or whatever. I've got a
simple program here (I actually found it on the web somewhere, but I'm
looking through it) that doesn't seem to have that ending quote at
when it gets down to the end.

In this program it doesn't make much of a difference, but I've
modified it a little and used it in another one where it needs to
decompress the string after it decrypts it and not having that ending
quote makes it crash.

Can anyone tell me what's going on here that makes that quote go away.

Also, I can't add anything to the end of the string when it comes out
of the last decryptTextFromMemory. These two problems are probably
related.

Thanks ahead of time

What you are saying doesn't really make sense. The quotes that are
displayed by the debugger is not part of the string in any way, it's
just how the debugger displays the string.

As the quotes are not part of the string in the first place, they can't
be missing from the string either.
 
W

Wesley

What you are saying doesn't really make sense. The quotes that are
displayed by the debugger is not part of the string in any way, it's
just how the debugger displays the string.

As the quotes are not part of the string in the first place, they can't
be missing from the string either.

Yeah, I realize it doesn't make sense. That's why I'm so confused.
Maybe someone with more of a knowledge of VB could tell me what's
going on there, but even so, if I add another quote to the end of the
string in the debugger, it works fine.

I think the second part of my problem is probably related and might
make more sense to to someone. After I make the final call to
decryptTextFromMemory, I can't concatenate any more text to the end of
the string. I can add to the beginning though.

Wesley
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Wesley said:
Yeah, I realize it doesn't make sense. That's why I'm so confused.
Maybe someone with more of a knowledge of VB could tell me what's
going on there, but even so, if I add another quote to the end of the
string in the debugger, it works fine.

If it makes any difference, it's most likely not that you add a quote,
it's that you edit the string in the debugger. There is nothing special
about the quote character if you put it inside a string, you will
probably get the same effect if you add any other character.
I think the second part of my problem is probably related and might
make more sense to to someone. After I make the final call to
decryptTextFromMemory, I can't concatenate any more text to the end of
the string. I can add to the beginning though.

What do you mean when you say that "you can't"? What did you try
exactly, and what happens when you try? Do you get an error message?

Strictly speaking, it's not possible to add anything to a string,
neither at the end nor at the beginning. If you concatenate strings, you
are copying the contents of the strings into a new instance of a string.
 
W

Wesley

If it makes any difference, it's most likely not that you add a quote,
it's that you edit the string in the debugger. There is nothing special
about the quote character if you put it inside a string, you will
probably get the same effect if you add any other character.


What do you mean when you say that "you can't"? What did you try
exactly, and what happens when you try? Do you get an error message?

Strictly speaking, it's not possible to add anything to a string,
neither at the end nor at the beginning. If you concatenate strings, you
are copying the contents of the strings into a new instance of a string.


I solved the problem. The issue was that the byte array that was
being converted to a string has some nulls at the end that were not
being taken out. Once I did that, the issue went away.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top