we are using ASCIIEncoding for saving data we have the new requirement for support an Unicode value (regional language like (Hindi, Gujarati) so we modify the code as below we have not done a big change just change over ASCIIEncoding to UnicodeEncoding . the issue is we are not able to decrypt old records using UnicodeEncoding
is there any way to identify the encryption method from an encrypted string which is stored in DB
we are using Below Code
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
You should use UTF8Encoding instead of UnicodeEncoding . The latter is encoded with UTF16, so not bytes compatible with ASCII. Instead UTF8 is byte compatible with ASCII. Both support all Unicode codes, so there is no loss of information.
But often one is not so lucky to have a superset encoder. So in general you should keep track of the encoding (eg in a new field), or just use one encoding (so you convert all your databases).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.