How To Encrypt non-ASCII string ?

C

Cowboy \(Gregory A. Beamer\)

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!
**********************************************************************
 
S

Sia Jai Sung

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.
 
S

Sia Jai Sung

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?
 
S

Sia Jai Sung

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
 

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