解密后连接字符串失败 [英] Connection String Fails After Decryption

查看:78
本文介绍了解密后连接字符串失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,该程序读取INI文件以获取(除其他外)连接字符串,然后该字符串用于基于从INI文件读取的其他参数来查询Oracle数据库.

I have created a program that reads an INI file to get (among other things) a connection string that it then uses to query an Oracle db based on other parameters read from the INI file.

它可以正常工作,但是新的要求是对连接字符串进行加密.我使用一个简单的函数完成了此任务,该函数与类似的Encryption函数相反:

It works fine, but a new requirement was to encrypt the connection string. I did this with a simple function that is the inverse of a similar Encryption function:

    Public Shared Function DecryptString(Message As String) As String
        Dim Results As Byte()
        Dim UTF8 As New System.Text.UTF8Encoding()
        Dim HashProvider As New MD5CryptoServiceProvider()
        Dim TDESKey As Byte() = HashProvider.ComputeHash(UTF8.GetBytes(strCryptoKey))
        Dim TDESAlgorithm As New TripleDESCryptoServiceProvider()
        TDESAlgorithm.Key = TDESKey
        TDESAlgorithm.Mode = CipherMode.ECB
        TDESAlgorithm.Padding = PaddingMode.PKCS7
        Dim DataToDecrypt As Byte() = Convert.FromBase64String(Message)
        Try
            Dim Decryptor As ICryptoTransform = TDESAlgorithm.CreateDecryptor()
            Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length)
        Finally
            TDESAlgorithm.Clear()
            HashProvider.Clear()
        End Try
        Return UTF8.GetString(Results)
    End Function

在代码的断点以及之后的当我写入日志文件时,我可以看到解密后的连接字符串显然与原始的明文连接字符串相同,但是当程序访问数据库时,它失败并返回 此错误:

At the breakpoint in the code and afterwards when I write to a log file, I can see that the decrypted connection string is apparently identical to the original clear text connection string, but when the program goes to access the db, it fails and returns this error:

Error: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
   at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)
   at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)
   at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)
   at System.Data.OracleClient.OracleConnectionString..ctor(String connectionString)
   at System.Data.OracleClient.OracleConnectionFactory.CreateConnectionOptions(String connectionOptions, DbConnectionOptions previous)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
   at System.Data.OracleClient.OracleConnection.ConnectionString_Set(String value)
   at DBBase.GetConnection()
   at DBUtil.dbSelect(String aQuery)
   at XXXXXX.Fetch(Export& aRec, Int32 intJobCount, DateTime dDateTimeStamp) in C:\DataFiles\SourceCode\XXXXXX.vb:line 44
   at XXXXXX.DoDocSearch(Export& oExport, Int32 intJobNumber, DateTime dDateTimeStamp) in C:\DataFiles\SourceCode\XXXXXX.vb:line 360

中的XXXXXX.DoDocSearch(Export& oExport,Int32 intJobNumber,DateTime dDateTimeStamp)

我尝试使用正则表达式删除所有不可见的字符,但是无论如何,它总是返回相同的错误.当我将解密后的文本粘贴到.INI文件中并再次运行该程序而没有解密功能时,它再次正常运行.

I've tried using a Regex to strip any non-visible characters, but no matter what, it keeps coming back with the same error. When I paste the decrypted text into the .INI file and run the program again without the Decryption function, it works fine again.

任何帮助将不胜感激...

Any help would be appreciated...

谢谢

约翰

推荐答案

可能尝试的方法:

Things to maybe try:

将纯文本连接字符串替换为测试

substitute the plain text connection string as a test

比较明文和解密后的长度

compare the length of the plaintext and the decrypted

将纯文本和解密后的数据进行哈希处理,比较哈希值

Hash the plaintext and the decrypted, compare the hashes

尝试使用这对加密/解密功能对(从代码项目上的C ++示例转换而来,经过测试并确认能正常工作)

try this encrypt/decrypt pair of functions (converted from a C++ example on codeproject, tested and confirmed working)

    Public Shared Function EncryptData(ByVal Message As String, passphrase As String) As String
        Dim Results() As Byte
        Dim UTF8 As New System.Text.UTF8Encoding()
        Dim HashProvider As New MD5CryptoServiceProvider()
        Dim TDESKey() As Byte = HashProvider.ComputeHash(UTF8.GetBytes(passphrase))
        Dim TDESAlgorithm As New TripleDESCryptoServiceProvider()
        TDESAlgorithm.Key = TDESKey
        TDESAlgorithm.Mode = CipherMode.ECB
        TDESAlgorithm.Padding = PaddingMode.PKCS7
        Dim DataToEncrypt() As Byte = UTF8.GetBytes(Message)
        Try
            Dim Encryptor As ICryptoTransform = TDESAlgorithm.CreateEncryptor()
            Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length)
        Finally
            TDESAlgorithm.Clear()
            HashProvider.Clear()
        End Try
        Return Convert.ToBase64String(Results)
    End Function


    Public Shared Function DecryptString(ByVal Message As String, passphrase As String) As String
        Dim Results() As Byte
        Dim UTF8 As New System.Text.UTF8Encoding()
        Dim HashProvider As New MD5CryptoServiceProvider()
        Dim TDESKey() As Byte = HashProvider.ComputeHash(UTF8.GetBytes(passphrase))
        Dim TDESAlgorithm As New TripleDESCryptoServiceProvider()
        TDESAlgorithm.Key = TDESKey
        TDESAlgorithm.Mode = CipherMode.ECB
        TDESAlgorithm.Padding = PaddingMode.PKCS7
        Dim DataToDecrypt() As Byte = Convert.FromBase64String(Message)
        Try
            Dim Decryptor As ICryptoTransform = TDESAlgorithm.CreateDecryptor()
            Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length)
        Finally
            TDESAlgorithm.Clear()
            HashProvider.Clear()
        End Try
        Return UTF8.GetString(Results)
    End Function


这篇关于解密后连接字符串失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆