By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,966 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,966 IT Pros & Developers. It's quick & easy.

How To Encrypt non-ASCII string ?

P: n/a
Hi,

I have a class that I modify from a sample program, like below

==========================================
Imports System
Imports System.Web.UI
Imports System.Security.Cryptography

public Class CSoccerichCommonFunc

private DESKey as String = "ABCDEFGH"
private DESIV as String = "HRHDDKGF"

private Function Convert2ByteArray( strInput As String ) As Byte()
Dim intCounter As Integer
Dim arrChar As Char()

arrChar = strInput.ToCharArray()
Dim arrByte( arrChar.Length - 1 ) As Byte
For intCounter = 0 To arrByte.Length - 1
arrByte( intCounter ) = Convert.ToByte( arrChar( intCounter ) )
Next
Return arrByte
End Function

Function EncryptString (strInput as String, byref strOutput as String) as
Integer
EncryptString = 0
Dim arrDesKey As Byte()
Dim arrDesIV As Byte()
Dim arrInput As Byte()
Dim arrResult As Byte()
Dim objDes as DESCryptoServiceProvider
Dim objEncryptor As ICryptoTransform

arrDESKey = Convert2ByteArray(DESKey)
arrDESIV = Convert2ByteArray (DESIV)
arrInput = Convert2ByteArray (strInput)
objDES = New DESCryptoServiceProvider()
objEncryptor = objDES.CreateEncryptor( arrDESKey, arrDESIV )
arrResult = objEncryptor.TransformFinalBlock (arrInput, 0,
arrInput.Length())
strOutput = Convert.ToBase64String (arrResult)
EncryptString = 1
End Function
End Class

==========================================
here is my questions
a) The encryption will work only when user imput string that are all ASCII
character, if they input eg. Chinese character it will fail.
How to solve that ?
b) How to do a decyption ?

I try like this,

Function DecryptString (strInput as String, byref strOutput as String) as
Integer
DecryptString = 0
Dim arrDESKey As Byte()
Dim arrDESIV As Byte()
Dim arrResult As Byte()
Dim arrInput As Byte()
Dim objDES As DESCryptoServiceProvider
Dim objDecryptor As ICryptoTransform
arrDESKey = Convert2ByteArray( DESKey )
arrDESIV = Convert2ByteArray( DESIV )
arrInput = convert.FromBase64String(strInput)
objDES = New DESCryptoServiceProvider
objDecryptor = objDES.CreateDecryptor( arrDESKey, arrDESIV )
arrResult = objDecryptor.TransformFinalBlock (arrInput, 0,
arrInput.Length())
DecryptString = 1
End Function

but I don't know how to change array of byte back to string.
Please help, Thanks in advance for any help offered.

Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Cor
Hi Sia Jai Sung

Have a look at the text.encoder class

http://msdn.microsoft.com/library/de...classtopic.asp

I think there is a lot about both questions of you?

Cor
Nov 20 '05 #2

P: n/a

"Cor" <no*@non.com> wrote in message
news:Oy**************@TK2MSFTNGP09.phx.gbl...
Hi Sia Jai Sung

Have a look at the text.encoder class

http://msdn.microsoft.com/library/de...classtopic.asp

Just to follow up.

The Convert2ByteArray function you posted did a manual ASCII encoding of the
string.

Just replace it with

dim b() as byte = System.Text.Encoding.Unicode.GetBytes(s)

David
Nov 20 '05 #3

P: n/a
The problem is the Convert2ByteArray function, which is a manual way of
doing what can be done with .NET classes. For example, for Unicode byte
arrays, you can use:

Dim MyByteArray As byte() =
System.Text.Encoding.Unicode.GetBytes(InputString)

The Convert2ByteArray function is essentially the same as:

Dim MyByteArray As byte() = System.Text.Encoding.ASCII.GetBytes(InputString)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Sia Jai Sung" <@thunder@stormex@yahoo@com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi,

I have a class that I modify from a sample program, like below

==========================================
Imports System
Imports System.Web.UI
Imports System.Security.Cryptography

public Class CSoccerichCommonFunc

private DESKey as String = "ABCDEFGH"
private DESIV as String = "HRHDDKGF"

private Function Convert2ByteArray( strInput As String ) As Byte()
Dim intCounter As Integer
Dim arrChar As Char()

arrChar = strInput.ToCharArray()
Dim arrByte( arrChar.Length - 1 ) As Byte
For intCounter = 0 To arrByte.Length - 1
arrByte( intCounter ) = Convert.ToByte( arrChar( intCounter ) ) Next
Return arrByte
End Function

Function EncryptString (strInput as String, byref strOutput as String) as
Integer
EncryptString = 0
Dim arrDesKey As Byte()
Dim arrDesIV As Byte()
Dim arrInput As Byte()
Dim arrResult As Byte()
Dim objDes as DESCryptoServiceProvider
Dim objEncryptor As ICryptoTransform

arrDESKey = Convert2ByteArray(DESKey)
arrDESIV = Convert2ByteArray (DESIV)
arrInput = Convert2ByteArray (strInput)
objDES = New DESCryptoServiceProvider()
objEncryptor = objDES.CreateEncryptor( arrDESKey, arrDESIV )
arrResult = objEncryptor.TransformFinalBlock (arrInput, 0,
arrInput.Length())
strOutput = Convert.ToBase64String (arrResult)
EncryptString = 1
End Function
End Class

==========================================
here is my questions
a) The encryption will work only when user imput string that are all ASCII
character, if they input eg. Chinese character it will fail.
How to solve that ?
b) How to do a decyption ?

I try like this,

Function DecryptString (strInput as String, byref strOutput as String) as
Integer
DecryptString = 0
Dim arrDESKey As Byte()
Dim arrDESIV As Byte()
Dim arrResult As Byte()
Dim arrInput As Byte()
Dim objDES As DESCryptoServiceProvider
Dim objDecryptor As ICryptoTransform
arrDESKey = Convert2ByteArray( DESKey )
arrDESIV = Convert2ByteArray( DESIV )
arrInput = convert.FromBase64String(strInput)
objDES = New DESCryptoServiceProvider
objDecryptor = objDES.CreateDecryptor( arrDESKey, arrDESIV )
arrResult = objDecryptor.TransformFinalBlock (arrInput, 0,
arrInput.Length())
DecryptString = 1
End Function

but I don't know how to change array of byte back to string.
Please help, Thanks in advance for any help offered.

Nov 20 '05 #4

P: n/a
Thanks for the help, I change the encrypt function to this,
Function EncryptString (strInput as String, byref strOutput as String) as
Integer
EncryptString = 0
Dim arrDesKey As Byte()
Dim arrDesIV As Byte()
Dim arrInput As Byte()
Dim arrResult As Byte()
Dim objDes as DESCryptoServiceProvider
Dim objEncryptor As ICryptoTransform

arrDESKey = Convert2ByteArray(DESKey)
arrDESIV = Convert2ByteArray (DESIV)
arrInput = System.Text.Encoding.Unicode.GetBytes(strInput)
objDES = New DESCryptoServiceProvider()
objEncryptor = objDES.CreateEncryptor( arrDESKey, arrDESIV )
arrResult = objEncryptor.TransformFinalBlock (arrInput, 0,
arrInput.Length())
strOutput = Convert.ToBase64String (arrResult)
EncryptString = 1
End Function

now it can accept anything even chinese character, but for decryption
function,
how do I convert an array of byte back to string?



"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:Ou**************@TK2MSFTNGP12.phx.gbl...
The problem is the Convert2ByteArray function, which is a manual way of
doing what can be done with .NET classes. For example, for Unicode byte
arrays, you can use:

Dim MyByteArray As byte() =
System.Text.Encoding.Unicode.GetBytes(InputString)

The Convert2ByteArray function is essentially the same as:

Dim MyByteArray As byte() = System.Text.Encoding.ASCII.GetBytes(InputString)
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Sia Jai Sung" <@thunder@stormex@yahoo@com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi,

I have a class that I modify from a sample program, like below

==========================================
Imports System
Imports System.Web.UI
Imports System.Security.Cryptography

public Class CSoccerichCommonFunc

private DESKey as String = "ABCDEFGH"
private DESIV as String = "HRHDDKGF"

private Function Convert2ByteArray( strInput As String ) As Byte()
Dim intCounter As Integer
Dim arrChar As Char()

arrChar = strInput.ToCharArray()
Dim arrByte( arrChar.Length - 1 ) As Byte
For intCounter = 0 To arrByte.Length - 1
arrByte( intCounter ) = Convert.ToByte( arrChar(

intCounter ) )
Next
Return arrByte
End Function

Function EncryptString (strInput as String, byref strOutput as String) as Integer
EncryptString = 0
Dim arrDesKey As Byte()
Dim arrDesIV As Byte()
Dim arrInput As Byte()
Dim arrResult As Byte()
Dim objDes as DESCryptoServiceProvider
Dim objEncryptor As ICryptoTransform

arrDESKey = Convert2ByteArray(DESKey)
arrDESIV = Convert2ByteArray (DESIV)
arrInput = Convert2ByteArray (strInput)
objDES = New DESCryptoServiceProvider()
objEncryptor = objDES.CreateEncryptor( arrDESKey, arrDESIV )
arrResult = objEncryptor.TransformFinalBlock (arrInput, 0,
arrInput.Length())
strOutput = Convert.ToBase64String (arrResult)
EncryptString = 1
End Function
End Class

==========================================
here is my questions
a) The encryption will work only when user imput string that are all ASCII character, if they input eg. Chinese character it will fail.
How to solve that ?
b) How to do a decyption ?

I try like this,

Function DecryptString (strInput as String, byref strOutput as String) as Integer
DecryptString = 0
Dim arrDESKey As Byte()
Dim arrDESIV As Byte()
Dim arrResult As Byte()
Dim arrInput As Byte()
Dim objDES As DESCryptoServiceProvider
Dim objDecryptor As ICryptoTransform
arrDESKey = Convert2ByteArray( DESKey )
arrDESIV = Convert2ByteArray( DESIV )
arrInput = convert.FromBase64String(strInput)
objDES = New DESCryptoServiceProvider
objDecryptor = objDES.CreateDecryptor( arrDESKey, arrDESIV )
arrResult = objDecryptor.TransformFinalBlock (arrInput, 0,
arrInput.Length())
DecryptString = 1
End Function

but I don't know how to change array of byte back to string.
Please help, Thanks in advance for any help offered.


Nov 20 '05 #5

P: n/a
Here is my decrypt dunction , looks like its working, Thanks for all the
help offerd.

Function DecryptString (strInput as String, byref strOutput as String) as
Integer
DecryptString = 0
Dim arrDESKey As Byte()
Dim arrDESIV As Byte()
Dim arrResult As Byte()
Dim arrInput As Byte()
Dim objDES As DESCryptoServiceProvider
Dim objDecryptor As ICryptoTransform

arrDESKey = Convert2ByteArray( DESKey )
arrDESIV = Convert2ByteArray( DESIV )
arrInput = convert.FromBase64String(strInput)
objDES = New DESCryptoServiceProvider
objDecryptor = objDES.CreateDecryptor( arrDESKey, arrDESIV )
arrResult = objDecryptor.TransformFinalBlock (arrInput, 0,
arrInput.Length())

Dim uniDecoder As Decoder = Encoding.Unicode.GetDecoder()
Dim charCount As Integer = uniDecoder.GetCharCount(arrResult, 0,
arrResult.Length)
Dim chars() As Char, i as Integer
chars = New Char(charCount - 1) {}

Dim charsDecodedCount As Integer = uniDecoder.GetChars(arrResult,
0, arrResult.Length, chars, 0)

for i = 0 to chars.Length()-1
strOutput &= chars(i)
next

DecryptString = 1
End Function

"Sia Jai Sung" <@thunder@stormex@yahoo@com> wrote in message
news:u1*************@TK2MSFTNGP11.phx.gbl...
Thanks for the help, I change the encrypt function to this,
Function EncryptString (strInput as String, byref strOutput as String) as
Integer
EncryptString = 0
Dim arrDesKey As Byte()
Dim arrDesIV As Byte()
Dim arrInput As Byte()
Dim arrResult As Byte()
Dim objDes as DESCryptoServiceProvider
Dim objEncryptor As ICryptoTransform

arrDESKey = Convert2ByteArray(DESKey)
arrDESIV = Convert2ByteArray (DESIV)
arrInput = System.Text.Encoding.Unicode.GetBytes(strInput)
objDES = New DESCryptoServiceProvider()
objEncryptor = objDES.CreateEncryptor( arrDESKey, arrDESIV )
arrResult = objEncryptor.TransformFinalBlock (arrInput, 0,
arrInput.Length())
strOutput = Convert.ToBase64String (arrResult)
EncryptString = 1
End Function

now it can accept anything even chinese character, but for decryption
function,
how do I convert an array of byte back to string?



"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:Ou**************@TK2MSFTNGP12.phx.gbl...
The problem is the Convert2ByteArray function, which is a manual way of
doing what can be done with .NET classes. For example, for Unicode byte
arrays, you can use:

Dim MyByteArray As byte() =
System.Text.Encoding.Unicode.GetBytes(InputString)

The Convert2ByteArray function is essentially the same as:

Dim MyByteArray As byte() =

System.Text.Encoding.ASCII.GetBytes(InputString)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Sia Jai Sung" <@thunder@stormex@yahoo@com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi,

I have a class that I modify from a sample program, like below

==========================================
Imports System
Imports System.Web.UI
Imports System.Security.Cryptography

public Class CSoccerichCommonFunc

private DESKey as String = "ABCDEFGH"
private DESIV as String = "HRHDDKGF"

private Function Convert2ByteArray( strInput As String ) As Byte()
Dim intCounter As Integer
Dim arrChar As Char()

arrChar = strInput.ToCharArray()
Dim arrByte( arrChar.Length - 1 ) As Byte
For intCounter = 0 To arrByte.Length - 1
arrByte( intCounter ) = Convert.ToByte( arrChar(

intCounter ) )
Next
Return arrByte
End Function

Function EncryptString (strInput as String, byref strOutput as String) as Integer
EncryptString = 0
Dim arrDesKey As Byte()
Dim arrDesIV As Byte()
Dim arrInput As Byte()
Dim arrResult As Byte()
Dim objDes as DESCryptoServiceProvider
Dim objEncryptor As ICryptoTransform

arrDESKey = Convert2ByteArray(DESKey)
arrDESIV = Convert2ByteArray (DESIV)
arrInput = Convert2ByteArray (strInput)
objDES = New DESCryptoServiceProvider()
objEncryptor = objDES.CreateEncryptor( arrDESKey, arrDESIV )
arrResult = objEncryptor.TransformFinalBlock (arrInput, 0,
arrInput.Length())
strOutput = Convert.ToBase64String (arrResult)
EncryptString = 1
End Function
End Class

==========================================
here is my questions
a) The encryption will work only when user imput string that are all ASCII character, if they input eg. Chinese character it will fail.
How to solve that ?
b) How to do a decyption ?

I try like this,

Function DecryptString (strInput as String, byref strOutput as String) as Integer
DecryptString = 0
Dim arrDESKey As Byte()
Dim arrDESIV As Byte()
Dim arrResult As Byte()
Dim arrInput As Byte()
Dim objDES As DESCryptoServiceProvider
Dim objDecryptor As ICryptoTransform
arrDESKey = Convert2ByteArray( DESKey )
arrDESIV = Convert2ByteArray( DESIV )
arrInput = convert.FromBase64String(strInput)
objDES = New DESCryptoServiceProvider
objDecryptor = objDES.CreateDecryptor( arrDESKey, arrDESIV )
arrResult = objDecryptor.TransformFinalBlock (arrInput, 0,
arrInput.Length())
DecryptString = 1
End Function

but I don't know how to change array of byte back to string.
Please help, Thanks in advance for any help offered.



Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.