Cryptography
-
Ok, I've had an Encrypt / Decrypt routine for a year now, but now I'm wondering if I am missing something in it. It seems that anyone with different Languages installed on their PC, the Decrypt cannot decrypt data on a PC with standard English. So, how can I enable my encryption routine to handle Locales?? I know that I need to convert the String into Locale specific data, but I am not sure where in this code. Any help is appreciated.
Public Class ICrypto
'<><><><><><><><><><><><><><><>
#Region "Methods"'Function to Return a Key \_ Private Shared Function GetKey(ByVal Data As String, ByVal KeySize As Integer) As Byte() Dim nSize As Integer = CInt((KeySize / 8) - 1) Dim vKey(nSize) As Byte If (Data.Length >= 1) Then Dim lastBound As Integer = Data.Length If (lastBound > nSize) Then lastBound = nSize For i As Integer = 0 To lastBound - 1 vKey(i) = Convert.ToByte(Data.Chars(i)) Next End If Return vKey End Function 'Function to Get the Initialization Vector \_ Private Shared Function GetIV(ByVal Data As String, ByVal BlockSize As Integer) As Byte() Dim nSize As Integer = CInt((BlockSize / 8) - 1) Dim vVector(nSize) As Byte If (Data.Length >= 1) Then Dim lastBound As Integer = Data.Length If (lastBound > nSize) Then lastBound = nSize For i As Integer = 0 To lastBound - 1 vVector(i) = Convert.ToByte(Data.Chars(i)) Next End If Return vVector End Function 'Function to Encrypt Data \_ Public Shared Function Encrypt(ByVal Data As String, ByVal Password As String, Optional ByVal ConvertToBase64 As Boolean = True) As String Dim provCrypto As Cryptography.SymmetricAlgorithm Dim mEncryptor As ICryptoTransform Dim stCrypto As Cryptography.CryptoStream Dim stMemory As IO.MemoryStream, stWriter As IO.StreamWriter Dim vData() As Byte Dim sData As String 'Exit if there is no Data If (IsNothing(Data)) Then Return String.Empty If (Data.Equals(String.Empty)) Then Return String.Empty Try 'Initialize the Crypto Provider (3DES) provCrypto = Cryptography.TripleDES.Create() provCrypto.KeySize = 128 provCrypto.BlockSize = 64 provCrypto.Mode = CipherMode.CBC provCrypto.Padding = PaddingMode.PKCS7 'Initialize the Memory Stream and Encrypt the Data vData = ConvertStringToBytes(Data) stMemory = New IO.MemoryStream mEncryptor = provCry
-
Ok, I've had an Encrypt / Decrypt routine for a year now, but now I'm wondering if I am missing something in it. It seems that anyone with different Languages installed on their PC, the Decrypt cannot decrypt data on a PC with standard English. So, how can I enable my encryption routine to handle Locales?? I know that I need to convert the String into Locale specific data, but I am not sure where in this code. Any help is appreciated.
Public Class ICrypto
'<><><><><><><><><><><><><><><>
#Region "Methods"'Function to Return a Key \_ Private Shared Function GetKey(ByVal Data As String, ByVal KeySize As Integer) As Byte() Dim nSize As Integer = CInt((KeySize / 8) - 1) Dim vKey(nSize) As Byte If (Data.Length >= 1) Then Dim lastBound As Integer = Data.Length If (lastBound > nSize) Then lastBound = nSize For i As Integer = 0 To lastBound - 1 vKey(i) = Convert.ToByte(Data.Chars(i)) Next End If Return vKey End Function 'Function to Get the Initialization Vector \_ Private Shared Function GetIV(ByVal Data As String, ByVal BlockSize As Integer) As Byte() Dim nSize As Integer = CInt((BlockSize / 8) - 1) Dim vVector(nSize) As Byte If (Data.Length >= 1) Then Dim lastBound As Integer = Data.Length If (lastBound > nSize) Then lastBound = nSize For i As Integer = 0 To lastBound - 1 vVector(i) = Convert.ToByte(Data.Chars(i)) Next End If Return vVector End Function 'Function to Encrypt Data \_ Public Shared Function Encrypt(ByVal Data As String, ByVal Password As String, Optional ByVal ConvertToBase64 As Boolean = True) As String Dim provCrypto As Cryptography.SymmetricAlgorithm Dim mEncryptor As ICryptoTransform Dim stCrypto As Cryptography.CryptoStream Dim stMemory As IO.MemoryStream, stWriter As IO.StreamWriter Dim vData() As Byte Dim sData As String 'Exit if there is no Data If (IsNothing(Data)) Then Return String.Empty If (Data.Equals(String.Empty)) Then Return String.Empty Try 'Initialize the Crypto Provider (3DES) provCrypto = Cryptography.TripleDES.Create() provCrypto.KeySize = 128 provCrypto.BlockSize = 64 provCrypto.Mode = CipherMode.CBC provCrypto.Padding = PaddingMode.PKCS7 'Initialize the Memory Stream and Encrypt the Data vData = ConvertStringToBytes(Data) stMemory = New IO.MemoryStream mEncryptor = provCry
System.Globalization.CultureInfo http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationcultureinfoclasstopic.asp
-
System.Globalization.CultureInfo http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationcultureinfoclasstopic.asp
Ok, well I knew I needed the CultureInfo Class...but I have no idea how to go about using it! I guess I will try to create an InvariantCulture and set it to the Current Culture of the Thread. I will save the existing Culture first so I can restore it later. Does anyone have an example of this? :wtf:
-
Ok, well I knew I needed the CultureInfo Class...but I have no idea how to go about using it! I guess I will try to create an InvariantCulture and set it to the Current Culture of the Thread. I will save the existing Culture first so I can restore it later. Does anyone have an example of this? :wtf:
Look here in section 3.3 http://www.gotdotnet.com/team/vb/VBSampleGuidelines.htm
-
Ok, well I knew I needed the CultureInfo Class...but I have no idea how to go about using it! I guess I will try to create an InvariantCulture and set it to the Current Culture of the Thread. I will save the existing Culture first so I can restore it later. Does anyone have an example of this? :wtf:
and here is another reference http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcustomcasemappingssortingrules.asp