Encrytion causing error

C

Chris Kennedy

I am encryted some data for use in calling an xml function

System.Xml.XmlException:'', hexadecimal value 0x13, is an invalid
character.

Here is my encryption function.

Function Encrypt(ByVal value As String) As String
Dim cryptoProvider As DESCryptoServiceProvider = New
DESCryptoServiceProvider()
Dim ms As MemoryStream = New MemoryStream()
Dim cs As CryptoStream = New CryptoStream(ms,
cryptoProvider.CreateEncryptor(key_64, IV_64), CryptoStreamMode.Write)
Dim sw As StreamWriter = New StreamWriter(cs)
sw.Write(value)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()
Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
End Function

Any ideas of how pass encrypted data as XML without XML illegal character
errors.
 
G

Grant Merwitz

Try enclose the encrypted data in CDATA tags

<SomeNode>
<![CDATA[
encrypted data with invalid characters go here
]]>
</SomeNode>
 
C

Chris Kennedy

The function is a webservice. I am passing the encrypted text as a string. I
am assuming it can't convert it to XML.

Grant Merwitz said:
Try enclose the encrypted data in CDATA tags

<SomeNode>
<![CDATA[
encrypted data with invalid characters go here
]]>
</SomeNode>

Chris Kennedy said:
I am encryted some data for use in calling an xml function

System.Xml.XmlException:'', hexadecimal value 0x13, is an invalid
character.

Here is my encryption function.

Function Encrypt(ByVal value As String) As String
Dim cryptoProvider As DESCryptoServiceProvider = New
DESCryptoServiceProvider()
Dim ms As MemoryStream = New MemoryStream()
Dim cs As CryptoStream = New CryptoStream(ms,
cryptoProvider.CreateEncryptor(key_64, IV_64), CryptoStreamMode.Write)
Dim sw As StreamWriter = New StreamWriter(cs)
sw.Write(value)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()
Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
End Function

Any ideas of how pass encrypted data as XML without XML illegal character
errors.
 
G

Grant Merwitz

sorry, i thought you were trying to place the encrypted string into an XML
file. Didn't realise it was a Web Service.

Not sure if an encrypted over Xml through a web service would break it.
People must pass encrypted values of Web Services all the time.


Just a thought - as web services are http based, maybe the encryption it
creating a character there that could break it.
Maybe give UrlEncoding the string a try. ... Just an idea, and a problem
i've come across passing encrypted values through Http

Chris Kennedy said:
The function is a webservice. I am passing the encrypted text as a string.
I am assuming it can't convert it to XML.

Grant Merwitz said:
Try enclose the encrypted data in CDATA tags

<SomeNode>
<![CDATA[
encrypted data with invalid characters go here
]]>
</SomeNode>

Chris Kennedy said:
I am encryted some data for use in calling an xml function

System.Xml.XmlException:'', hexadecimal value 0x13, is an invalid
character.

Here is my encryption function.

Function Encrypt(ByVal value As String) As String
Dim cryptoProvider As DESCryptoServiceProvider = New
DESCryptoServiceProvider()
Dim ms As MemoryStream = New MemoryStream()
Dim cs As CryptoStream = New CryptoStream(ms,
cryptoProvider.CreateEncryptor(key_64, IV_64), CryptoStreamMode.Write)
Dim sw As StreamWriter = New StreamWriter(cs)
sw.Write(value)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()
Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
End Function

Any ideas of how pass encrypted data as XML without XML illegal
character errors.
 
C

Chris Kennedy

When I send the string encrypted i can return the encrypted string (as a
test) no problem.
When I decrypt and return it 90% I get the error. Occassionally I get a
successful decryption but only on a first build of the webservice. Could
something be cached somewhere.

Function Decrypt(ByVal value As String) As String
Dim cryptoProvider As DESCryptoServiceProvider = New
DESCryptoServiceProvider()
Dim buffer As Byte() = Convert.FromBase64String(value)
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim cs As CryptoStream = New CryptoStream(ms,
cryptoProvider.CreateDecryptor(key_64, IV_64), CryptoStreamMode.Read)
Dim sr As StreamReader = New StreamReader(cs)
Return sr.ReadToEnd
End Function


Grant Merwitz said:
sorry, i thought you were trying to place the encrypted string into an XML
file. Didn't realise it was a Web Service.

Not sure if an encrypted over Xml through a web service would break it.
People must pass encrypted values of Web Services all the time.


Just a thought - as web services are http based, maybe the encryption it
creating a character there that could break it.
Maybe give UrlEncoding the string a try. ... Just an idea, and a problem
i've come across passing encrypted values through Http

Chris Kennedy said:
The function is a webservice. I am passing the encrypted text as a
string. I am assuming it can't convert it to XML.

Grant Merwitz said:
Try enclose the encrypted data in CDATA tags

<SomeNode>
<![CDATA[
encrypted data with invalid characters go here
]]>
</SomeNode>

I am encryted some data for use in calling an xml function

System.Xml.XmlException:'', hexadecimal value 0x13, is an invalid
character.

Here is my encryption function.

Function Encrypt(ByVal value As String) As String
Dim cryptoProvider As DESCryptoServiceProvider = New
DESCryptoServiceProvider()
Dim ms As MemoryStream = New MemoryStream()
Dim cs As CryptoStream = New CryptoStream(ms,
cryptoProvider.CreateEncryptor(key_64, IV_64), CryptoStreamMode.Write)
Dim sw As StreamWriter = New StreamWriter(cs)
sw.Write(value)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()
Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
End Function

Any ideas of how pass encrypted data as XML without XML illegal
character errors.
 
B

Bruce Barker

you need to xml encode the string. if you use the XmlTextWriter it will do
it for you, or you can use the XmlTextEncoder.

-- bruce (sqlwork.com)
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top