utf 8 control characters

Posted on 2011-05-05
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
    LVL 23

    Expert Comment

    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

    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

    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.
    LVL 23

    Expert Comment

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

    Author Comment

    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

    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

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    The canonical version of this article is on my web site here: A companion presentation is available here:
    Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
    The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
    The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now