Click here to Skip to main content
15,904,153 members
Home / Discussions / Visual Basic
   

Visual Basic

 
General$ To.String Pin
Member 11683248-Oct-04 19:54
Member 11683248-Oct-04 19:54 
GeneralRe: $ To.String Pin
MohammadAmiry9-Oct-04 6:48
MohammadAmiry9-Oct-04 6:48 
GeneralRe: $ To.String Pin
Member 116832413-Oct-04 13:01
Member 116832413-Oct-04 13:01 
Generalhelp , i need to make a tic tac toe game using control arrays Pin
Tow Jam8-Oct-04 15:41
Tow Jam8-Oct-04 15:41 
GeneralRe: help , i need to make a tic tac toe game using control arrays Pin
Mekong River8-Oct-04 17:32
Mekong River8-Oct-04 17:32 
GeneralSetting the selected value in a listbox Pin
David M J8-Oct-04 11:39
David M J8-Oct-04 11:39 
GeneralRe: Setting the selected value in a listbox Pin
HarryBo9-Oct-04 10:17
HarryBo9-Oct-04 10:17 
GeneralCryptography Pin
mikasa8-Oct-04 10:49
mikasa8-Oct-04 10:49 
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
	'<<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>>
#Region "Methods"

	'Function to Return a Key
	<DebuggerStepThrough()> _
	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
	<DebuggerStepThrough()> _
	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
	<DebuggerStepThrough()> _
	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 = provCrypto.CreateEncryptor(GetKey(Password, provCrypto.KeySize), GetIV("Initialization Vector", provCrypto.BlockSize))
			stCrypto = New CryptoStream(stMemory, mEncryptor, CryptoStreamMode.Write)

			'Write the Encrypted Data
			stCrypto.Write(vData, 0, vData.Length)
			stCrypto.FlushFinalBlock()

			If (ConvertToBase64) Then
				'Base64 Encoding enables Saving to XML
				sData = System.Convert.ToBase64String(stMemory.ToArray())
			Else
				sData = ConvertBytesToString(stMemory.ToArray())				 'Encode the Byte Array to UTF8
			End If

		Catch ex As Exception		   ': MsgBox(ex.ToString)
		Finally
			Erase vData
			If (Not IsNothing(stMemory)) Then stMemory.Close() : stMemory = Nothing
			If (Not IsNothing(stCrypto)) Then stCrypto.Clear() : stCrypto.Close() : stCrypto = Nothing
			If (Not IsNothing(mEncryptor)) Then mEncryptor.Dispose() : mEncryptor = Nothing
			If (Not IsNothing(provCrypto)) Then provCrypto.Clear() : provCrypto = Nothing

		End Try

		'Return the Data
		Return sData
	End Function

	'Function to Decrypt Data
	<DebuggerStepThrough()> _
	Public Shared Function Decrypt(ByVal Data As String, ByVal Password As String, Optional ByVal ConvertFromBase64 As Boolean = True) As String
		Dim provCrypto As Cryptography.SymmetricAlgorithm
		Dim mDecryptor As ICryptoTransform
		Dim stCrypto As Cryptography.CryptoStream
		Dim stMemory As IO.MemoryStream
		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

			'Decrypt the Data
			If (ConvertFromBase64) Then
				ReDim vData(Data.Length)
				vData = System.Convert.FromBase64String(Data)
			Else
				vData = ConvertStringToBytes(Data)
			End If
			stMemory = New IO.MemoryStream
			mDecryptor = provCrypto.CreateDecryptor(GetKey(Password, provCrypto.KeySize), GetIV("Initialization Vector", provCrypto.BlockSize))
			stCrypto = New CryptoStream(stMemory, mDecryptor, CryptoStreamMode.Write)
			stCrypto.Write(vData, 0, vData.Length)
			stCrypto.FlushFinalBlock()

			'Read the Encrypted Data
			sData = ConvertBytesToString(stMemory.ToArray())

		Catch ex As Exception		   ': MsgBox(ex.ToString)
		Finally
			Erase vData
			If (Not IsNothing(stMemory)) Then stMemory.Close() : stMemory = Nothing
			If (Not IsNothing(stCrypto)) Then stCrypto.Clear() : stCrypto = Nothing
			If (Not IsNothing(mDecryptor)) Then mDecryptor.Dispose() : mDecryptor = Nothing
			If (Not IsNothing(provCrypto)) Then provCrypto.Clear() : provCrypto = Nothing

		End Try

		'Return the Data
		Return sData
	End Function

	'Function to Convert a String to a Byte Array
	<DebuggerStepThrough()> _
	Public Shared Function ConvertStringToBytes(ByVal Data As String) As Byte()
		Return System.Text.UTF8Encoding.UTF8.GetBytes(Data)
	End Function

	'Function to Convert Bytes to a String
	<DebuggerStepThrough()> _
	Public Shared Function ConvertBytesToString(ByVal Bytes() As Byte) As String
		Return System.Text.UTF8Encoding.UTF8.GetString(Bytes)
	End Function

#End Region
	'<<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>>

	'<<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>>
#Region "Class Constructors / Destructors"

	'Initialize the Class
	Private Sub New()
	End Sub

#End Region
	'<<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>><<o>>
End Class

GeneralRe: Cryptography Pin
Anonymous10-Oct-04 21:13
Anonymous10-Oct-04 21:13 
GeneralRe: Cryptography Pin
mikasa11-Oct-04 3:37
mikasa11-Oct-04 3:37 
GeneralRe: Cryptography Pin
Anonymous11-Oct-04 8:12
Anonymous11-Oct-04 8:12 
GeneralRe: Cryptography Pin
Anonymous11-Oct-04 8:27
Anonymous11-Oct-04 8:27 
GeneralMAP Pin
marcoscavaleiro8-Oct-04 7:44
marcoscavaleiro8-Oct-04 7:44 
GeneralRe: MAP Pin
Jim Matthews8-Oct-04 8:17
Jim Matthews8-Oct-04 8:17 
GeneralRe: MAP Pin
Colin Angus Mackay8-Oct-04 10:13
Colin Angus Mackay8-Oct-04 10:13 
GeneralRe: MAP Pin
marcoscavaleiro8-Oct-04 11:40
marcoscavaleiro8-Oct-04 11:40 
QuestionHow to prevent auto conversion of date format Using RunSQL Pin
Umair Khalid8-Oct-04 5:45
Umair Khalid8-Oct-04 5:45 
AnswerRe: How to prevent auto conversion of date format Using RunSQL Pin
Dave Kreskowiak8-Oct-04 7:16
mveDave Kreskowiak8-Oct-04 7:16 
GeneralDateTimePicker Pin
Member 11683248-Oct-04 5:26
Member 11683248-Oct-04 5:26 
GeneralRe: DateTimePicker Pin
Dave Kreskowiak8-Oct-04 5:59
mveDave Kreskowiak8-Oct-04 5:59 
GeneralRe: DateTimePicker Pin
Member 11683248-Oct-04 9:50
Member 11683248-Oct-04 9:50 
GeneralAbout Namespace Pin
iamalik8-Oct-04 4:58
professionaliamalik8-Oct-04 4:58 
GeneralRe: About Namespace Pin
Dave Kreskowiak8-Oct-04 5:56
mveDave Kreskowiak8-Oct-04 5:56 
QuestionDatagrid edit mode to edit template columns? Pin
moody868-Oct-04 4:38
moody868-Oct-04 4:38 
Generalcontrols Pin
fo0lish8-Oct-04 4:31
fo0lish8-Oct-04 4:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.