utf 8 control characters

Posted on 2011-05-05
Medium Priority
Last Modified: 2012-05-11
I've inherited a 1.1 net app and just converted to 4.0
to cut a long story short I have converted encoding.ascii to encoding.utf8
only issue is how does one represent utf8 control characters in ascii

essentially these control characters form part of a decryption routine and all was well with ascii but now with utf control characters are being returned as square boxes.

thanks - any help will be much appreciated.
Question by:sanagarwl
  • 4
  • 2
LVL 23

Expert Comment

ID: 35701138
Can you provide more information or code on the encryption/decryption routine?  Is it custom written or does it leverage the .NET encryption classes?

Also what are the control characters wrongly handled?  Where are they being displayed (where do you see the square boxes)?

Author Comment

ID: 35701251
It is a custom routine that leverages the .net encryption class - but the version is 1.1

Public Sub GetEncryptedString(ByVal sInputString As String, ByVal sEncKey As String, _
                ByRef sOutVector As String, ByRef sOutEncString As String)
        Dim enc As Encryptor
        Dim plainText As Byte()
        Dim key As Byte()
        Dim cipherText As Byte()

            enc = New Encryptor(EncryptionAlgorithm.TripleDes)

            'plainText = Encoding.ASCII.GetBytes(sInputString)
            plainText = Encoding.UTF8.GetBytes(sInputString)

            'key = Encoding.ASCII.GetBytes(sEncKey)
            key = Encoding.UTF8.GetBytes(sEncKey)

            cipherText = enc.Encrypt(plainText, key)

            'sOutVector = Encoding.ASCII.GetString(enc.IV)
            sOutVector = Encoding.UTF8.GetString(enc.IV)

            sOutEncString = Convert.ToBase64String(cipherText)

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

so with sinputstring="server=###"


would return squarebracket/control characters for enc.IV values that are less than 33 and enc.IV values that are greater than 126.

Earlier, with ASCII encoding all worked well under .net 1.1 - those line have been commented out above.

### obfuscated by
EE Moderator
LVL 23

Expert Comment

ID: 35701439
BTW, if you use this encrypt/decrypt methods just to protect information on your Web.Config file, ASP.NET provides that out-of-the-box.

Encrypting Configuration Information Using Protected Configuration

Basically, you just need to run the aspnet_regiis utility against your Web.Config file to encrypt sections of it and no coding changes are needed.

Anyway, I'll review your code.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 23

Expert Comment

ID: 35701530
Where does Encryptor class comes from (Assembly / Namespace)?

Author Comment

ID: 35706864
thanks for the quick response.

I will go with your suggestion and was aware of an out of the box solution but would like to do a 1 to 1 upgrade {more for political/business reason at the client site} first.

I then plan on recommending a series of architectural changes too...

Essentially, the Encoding.UTF8.GetString(enc.IV) returns control characters from the UTF-8 charachter set, and the string returned by the Encoding.UTF8.GetString(enc.IV) is used during the decryption.

I could keep calling the above routine again and again till I get a series of "legible" charachters but that is cumbersome at best and highly improbable at worst - we need to generate those for each of the environments namely dev, devtest, uat, and prod.

The idea for this I believe comes from this msdn link

Imports System.Security.Cryptography
Imports System.IO

Public Class Encryptor

    Private transformer As EncryptTransformer
    Private initVec As Byte()
    Private encKey As Byte()

    Public Property IV() As Byte()
            Return initVec
        End Get
        Set(ByVal Value As Byte())
            initVec = Value
        End Set
    End Property

    Public Property Key() As Byte()
            Return encKey
        End Get
        Set(ByVal Value As Byte())

        End Set
    End Property

    Public Sub New(ByVal algId As EncryptionAlgorithm)
        transformer = New EncryptTransformer(algId)
    End Sub

    Public Function Encrypt(ByVal bytesData As Byte(), ByVal bytesKey As Byte()) As Byte()
        Dim memStreamEncryptedData As MemoryStream

        memStreamEncryptedData = New MemoryStream

        transformer.IV = initVec
        Dim transform As ICryptoTransform
        transform = transformer.GetCryptoServiceProvider(bytesKey)

        Dim encStream As CryptoStream

        encStream = New CryptoStream(memStreamEncryptedData, transform, CryptoStreamMode.Write)

            encStream.Write(bytesData, 0, bytesData.Length)
        Catch ex As Exception
            Throw New Exception("Error while writing encrypted data to the stream: \n" & ex.Message)
        End Try

        encKey = transformer.key
        initVec = transformer.IV

        Return memStreamEncryptedData.ToArray

    End Function
End Class

Open in new window

LVL 23

Accepted Solution

wdosanjos earned 2000 total points
ID: 35707427
I believe the solution is to handle sOutVector in the same way you handle sOutEncString, that is by converting it to/from a Base 64 String instead of using Encoding.UTF8.GetString.  As follows:

sOutVector = Convert.ToBase64String(enc.IV) ' when encrypting

enc.IV = Convert.FromBase64String(sOutVector) ' when decrypting

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question