我们正在使用 ASCIIEncoding 来保存数据,我们对支持Unicode值(区域语言(如(印地语,古吉拉特语)等)有新的要求,因此我们对代码进行了如下修改,我们并没有做大的改动只需将 ASCIIEncoding 更改为 UnicodeEncoding 。 问题是我们无法使用 UnicodeEncoding
解密旧记录有什么方法可以从存储在数据库中的加密字符串中识别加密方法
我们正在使用以下代码
Public Function DESEncrypt(ByVal PlainText As String) As String
Dim KeyBArray As Byte()
Dim IVBArray As Byte()
Dim Encryptor As ICryptoTransform
Dim PlainTextBArray As Byte()
'Dim Ascii As New ASCIIEncoding
Dim Ascii As New UnicodeEncoding
Dim I As Integer
Dim Result As String
PlainTextBArray = Ascii.GetBytes(PlainText)
KeyBArray = Convert.FromBase64String(desKey)
IVBArray = Convert.FromBase64String(desIV)
Dim TDES As New TripleDESCryptoServiceProvider
Encryptor = TDES.CreateEncryptor(KeyBArray, IVBArray)
Dim CypherTextBArray(Encryptor.OutputBlockSize - 1) As Byte
CypherTextBArray = Encryptor.TransformFinalBlock(PlainTextBArray, I, PlainTextBArray.Length)
Dim TempBArray(CypherTextBArray.Length - 1) As Byte
For I = 0 To CypherTextBArray.Length - 1
TempBArray(I) = CypherTextBArray(I)
Next
Result = Convert.ToBase64String(TempBArray)
KeyBArray = Nothing
IVBArray = Nothing
Encryptor = Nothing
PlainTextBArray = Nothing
Ascii = Nothing
Return Result
End Function
Public Function DESDecrypt(ByVal CypherText As String) As String
Dim KeyBArray As Byte()
Dim IVBArray As Byte()
Dim CypherTextBArray As Byte()
Dim Decryptor As ICryptoTransform
Dim PlainText As New StringBuilder("")
Dim I As Integer
Dim Result As String = String.Empty
If CypherText <> "" Then
CypherTextBArray = Convert.FromBase64String(CypherText)
KeyBArray = Convert.FromBase64String(desKey)
IVBArray = Convert.FromBase64String(desIV)
If cryptoPrivider = "TripleDES" Or cryptoPrivider = "CITripleDES" Then
Dim TDES As New TripleDESCryptoServiceProvider
Decryptor = TDES.CreateDecryptor(KeyBArray, IVBArray)
Else
Dim DES As New DESCryptoServiceProvider
Decryptor = DES.CreateDecryptor(KeyBArray, IVBArray)
End If
Dim PlainTextBArray(Decryptor.OutputBlockSize - 1) As Byte
PlainTextBArray = Decryptor.TransformFinalBlock(CypherTextBArray, I, CypherTextBArray.Length)
'Result = System.Text.Encoding.ASCII.GetString(PlainTextBArray)
Result = System.Text.Encoding.Unicode.GetString(PlainTextBArray)
End If
KeyBArray = Nothing
IVBArray = Nothing
CypherTextBArray = Nothing
Decryptor = Nothing
PlainText = Nothing
Return Result
End Function
答案 0 :(得分:0)
您应该使用 UTF8Encoding 而不是 UnicodeEncoding 。后者使用UTF16编码,因此不兼容ASCII的字节。相反,UTF8与ASCII字节兼容。两者都支持所有Unicode代码,因此不会丢失任何信息。
但是通常没有一个超级集编码器很不幸。因此,通常您应该跟踪编码(例如,在新字段中),或者只使用一种编码(以便转换所有数据库)。