473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

String missing ending quote

>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(K ey, 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(K ey, 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

Feb 5 '07 #1
4 2162
we******@gmail.com wrote:
>>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
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.

--
Göran Andersson
_____
http://www.guffa.com
Feb 6 '07 #2
On Feb 5, 6:04 pm, Göran Andersson <g...@guffa.comwrote:
wesbl...@gmail.com wrote:
>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

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.

--
Göran Andersson
_____http://www.guffa.com
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

Feb 6 '07 #3
Wesley wrote:
>
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.

--
Göran Andersson
_____
http://www.guffa.com
Feb 6 '07 #4
On Feb 6, 1:28 pm, Göran Andersson <g...@guffa.comwrote:
Wesley wrote:
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.

--
Göran Andersson
_____http://www.guffa.com

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.

Feb 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bengt Richter | last post by:
Why wouldn't quote-stuffing solve the problem, and let you treat \ as an ordinary character? In a raw string, it's no good for preventing end-of-quoting anyway, unless you want the literal \ in...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
34
by: Umesh | last post by:
I want to extract a string abc*xyz from a text file. * indicates arbitrary no. of characters. I'm only able to do it when the string has definite no. of characters or the string length is...
8
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
5
by: Curious | last post by:
I have: if (temp.Contains("Account") == true) { temp.Replace("Account", "Client"); } The "Account" is not replaced by "Client" after this operation. I used
23
by: KIRAN | last post by:
Hi all, can i split a C string like this? char * p = "Hello \ World\n\r"; is this according to standard? Any help or link to standard regarding the above doubt is aprreciated... Regards, Kiran
3
by: eBob.com | last post by:
Is there a regex pattern which will match a VB.Net string? I.E. a regex which matches ... "this is a ""vb.net"" string" (I don't want three matches in this case, I want one.) I've come up...
5
by: EricW | last post by:
Hi, I have the following line in my code: strConnect = String.Format("Data Source={0},{1};Network Library=DBMSSOCN;Initial Catalog={2};Integrated Security=False;Trusted...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.