- Moderator
- #1
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.
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
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