Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone take a look at this please..... 1

Status
Not open for further replies.

gbaughma

IS-IT--Management
Staff member
Joined
Nov 21, 2003
Messages
4,773
Location
US
I have been working on this code for a couple of days now, and I keep getting an error that "Specified key is not a valid size for this algorithm".

I'm writing this in Visual Studio 2010 .NET 2.0 to integrate with SQL as a CLR function.

Code:
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Text
Imports System.Security
Imports System.Security.Cryptography
Imports Microsoft.SqlServer.Server

Public Class Encryption

    <Microsoft.SqlServer.Server.SqlFunction()> _
    Public Shared Function Decrypt(ByVal strData As String) As SqlString
        ' Add your code here
        Dim m_key() As Byte = New Byte(8) {}
        Dim m_IV() As Byte = New Byte(8) {}

        Dim strResult As String
        Dim strKey As String
        strKey = "12345"

        Try

            Dim bp() As Byte = New Byte(strKey.Length) {}
            Dim aEnc As ASCIIEncoding = New ASCIIEncoding()
            aEnc.GetBytes(strKey, 0, strKey.Length, bp, 0)

            ' Hash key using SHA1
            Dim sha As SHA1CryptoServiceProvider = New SHA1CryptoServiceProvider()
            Dim bpHash() As Byte = sha.ComputeHash(bp)

            Dim i As Integer

            For i = 0 To 7
                m_key(i) = bpHash(i)
            Next

            For i = 8 To 15
                m_IV(i - 8) = bpHash(i)
            Next
        Catch ex As Exception
            strResult = "Error: Failed to generate key for decryption."
            Return New SqlString(strResult)
        End Try

        ' Initialize the service provider
        Dim nReturn As Integer = 0
        Dim descsp As DESCryptoServiceProvider = New DESCryptoServiceProvider()
        Dim desDecrypt As ICryptoTransform = descsp.CreateDecryptor(m_Key, m_IV)

        ' Prepare the streams
        Dim mOut As MemoryStream = New MemoryStream()
        Dim cs As CryptoStream = New CryptoStream(mOut, desDecrypt, CryptoStreamMode.Write)

        ' Remember to revert to the Base64 encoding into a byte array to restore the original encrypted stream
        Dim bPlain As Byte() = New Byte(strData.Length) {}

        Try
            bPlain = Convert.FromBase64CharArray(strData.ToCharArray(), 0, strData.Length)

        Catch ex As Exception
            strResult = "Error.  Input data is not base64 encoded."
            Return New SqlString(strResult)

        End Try

        Dim lRead As Long = 0
        Dim lTotal As Long = strData.Length

        Try
            ' Perform the actual decryption
            While (lTotal >= lRead)
                cs.Write(bPlain, 0, Int(bPlain.Length))
                lRead = mOut.Length + Convert.ToUInt32(((bPlain.Length / descsp.BlockSize) * descsp.BlockSize))
            End While

            Dim aEnc As ASCIIEncoding = New ASCIIEncoding()
            strResult = aEnc.GetString(mOut.GetBuffer(), 0, Int(mOut.Length))

            ' Trim the string to return only the meaningful data
            Dim strLen As String = strResult.Substring(0, 5)
            Dim nLen As Integer = Convert.ToInt32(strLen)
            strResult = strResult.Substring(5, nLen)
            nReturn = Int(mOut.Length)

            Return New SqlString(strResult)


        Catch ex As Exception
            strResult = "Error.  Decryption failed."
            Return New SqlString(strResult)
        End Try


    End Function

End Class

It builds all right, it deploys all right, but it doesn't run.

I have to admit, I'm new to .NET.... and the "Byte" variable types have me a little confused on syntax.... any help would be appreciated. :)

TIA!


Just my 2¢

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
Run it in debug without any Try Catch and see what line it fails on.

I don't have 2010, but you do some things you normally don't have to do. I would suggest some of these changes based off of what you have there unless 2010 starts yelling at you for them. None of them may be a problem as I can't test them all right now, but they are not really the proper way to do it unless something has changed. Add green delete red.
Code:
Dim m_key([green]8[/green]) As Byte[red] = New Byte(8) {}[/red]
Dim m_IV([green]8[/green]) As Byte[red] = New Byte(8) {}[/red]
Code:
Dim bp() As Byte = New Byte(strKey.Length) [red]{}[/red]
Code:
Dim bPlain As Byte() = New Byte(strData.Length) [red]{}[/red]

Nothing just sticks out as totally wrong that I know would cause an issue, but then I haven't done all of the things you are trying before. Generally the only time you really want {brackets} with an array is when you are trying to hardcode that array.
Code:
Dim Dogs As String() = {"Malamute", "Akita", "Saint Bernard"}

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I got it all working, finally.

I had to convert the original code from C# (which I am not real familiar with) to VB.NET as a CLR function.

Thank you for your time though. :)



Just my 2¢

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top