Intractive
SF Bay Area Computer Consulting
SF Bay Area Computer Consulting
Mar 17th
Welcome to the new Intractive web site. The site is actually 2 sites in one… it just makes it easier for us to maintain.
The main purpose of the site is to facilitate our computer consulting business. If you are in the San Francisco Bay Area, and have any computer consulting needs, please feel free to contact me.
A little about myself. I am self taught. I do NOT have a Computer Science degree. But, if it were offered, I would have a Google search degree. If it weren’t for Google, I wouldn’t know half of the things I do today.
As it is, I have never paid for training, but yet, I charge well for my services. I feel it is my duty to give back to the community that teach me, and thus the other half of this site will provide resources and support to individuals who are new to programming, networking, and what ever else people need help with.
Mar 17th
Purpose : Return the contents of a URL as a string
Imports System.Net Shared Function getFileContentsFromURL(ByVal myUrl As String) As String Dim myReturn As String = "" Try Dim myWC As New WebClient Dim myDownloadedData() As Byte = myWC.DownloadData(myUrl) Dim myObjUTF8 As New System.Text.UTF8Encoding myReturn = myObjUTF8.GetString(myDownloadedData) myDownloadedData = Nothing myObjUTF8 = Nothing myWC = Nothing Catch ex As Exception 'Handle Error End Try Return myReturn End Function
Mar 17th
Purpose : When designing Windows form applications, you can use the code below to minimize the amount of memory Task Manager shows your application is using.
Private Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal hProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Int32, ByVal dwMaximumWorkingSetSize As Int32) As Int32 Shared Sub ReleaseMemory() Try GC.Collect() GC.WaitForPendingFinalizers() If Environment.OSVersion.Platform = PlatformID.Win32NT Then SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1) End If Catch End Try End Sub
Mar 17th
Purpose : This function will allow you to encrypt data.
Special Note : The mySecurity Class is NOT my code, but is VERY useful and VERY difficult to find, so I figured I would re-post it. If the author of the code has any objections, please message me and it will be removed. The shared variables would be setup in your own class, and then passed to the mySecurity class.
Imports System.IO Imports System.Security.Cryptography Imports System.Text
Shared passPhrase As String = "12345gcwert6789012dg" Shared saltValue As String = "s@1tValue" Shared hashAlgorithm As String = "SHA1" Shared passwordIterations As Integer = 2 Shared initVector As String = "@1B2c3D4e5F6g7H8" ' must be 16 bytes Shared keySize As Integer = 256 ' can be 192 or 128
Public Class mySecurity Public Shared Function Encrypt(ByVal plainText As String, _ ByVal passPhrase As String, _ ByVal saltValue As String, _ ByVal hashAlgorithm As String, _ ByVal passwordIterations As Integer, _ ByVal initVector As String, _ ByVal keySize As Integer) _ As String ' Convert strings into byte arrays. ' Let us assume that strings only contain ASCII codes. ' If strings include Unicode characters, use Unicode, UTF7, or UTF8 ' encoding. Dim initVectorBytes As Byte() initVectorBytes = Encoding.ASCII.GetBytes(initVector) Dim saltValueBytes As Byte() saltValueBytes = Encoding.ASCII.GetBytes(saltValue) ' Convert our plaintext into a byte array. ' Let us assume that plaintext contains UTF8-encoded characters. Dim plainTextBytes As Byte() plainTextBytes = Encoding.UTF8.GetBytes(plainText) ' First, we must create a password, from which the key will be derived. ' This password will be generated from the specified passphrase and ' salt value. The password will be created using the specified hash ' algorithm. Password creation can be done in several iterations. Dim password As PasswordDeriveBytes password = New PasswordDeriveBytes(passPhrase, _ saltValueBytes, _ hashAlgorithm, _ passwordIterations) ' Use the password to generate pseudo-random bytes for the encryption ' key. Specify the size of the key in bytes (instead of bits). Dim keyBytes As Byte() keyBytes = password.GetBytes(keySize /' Create uninitialized Rijndael encryption object. Dim symmetricKey As RijndaelManaged symmetricKey = New RijndaelManaged() ' It is reasonable to set encryption mode to Cipher Block Chaining ' (CBC). Use default options for other symmetric key parameters. symmetricKey.Mode = CipherMode.CBC ' Generate encryptor from the existing key bytes and initialization ' vector. Key size will be defined based on the number of the key ' bytes. Dim encryptor As ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes) ' Define memory stream which will be used to hold encrypted data. Dim memoryStream As MemoryStream memoryStream = New MemoryStream() ' Define cryptographic stream (always use Write mode for encryption). Dim cryptoStream As CryptoStream cryptoStream = New CryptoStream(memoryStream, _ encryptor, _ CryptoStreamMode.Write) ' Start encrypting. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length) ' Finish encrypting. cryptoStream.FlushFinalBlock() ' Convert our encrypted data from a memory stream into a byte array. Dim cipherTextBytes As Byte() cipherTextBytes = memoryStream.ToArray() ' Close both streams. memoryStream.Close() cryptoStream.Close() ' Convert encrypted data into a base64-encoded string. Dim cipherText As String cipherText = Convert.ToBase64String(cipherTextBytes) ' Return encrypted string. Encrypt = cipherText End Function ' <summary> ' Decrypts specified ciphertext using Rijndael symmetric key algorithm. ' </summary> ' <param name="cipherText"> ' Base64-formatted ciphertext value. ' </param> ' <param name="passPhrase"> ' Passphrase from which a pseudo-random password will be derived. The ' derived password will be used to generate the encryption key. ' Passphrase can be any string. In this example we assume that this ' passphrase is an ASCII string. ' </param> ' <param name="saltValue"> ' Salt value used along with passphrase to generate password. Salt can ' be any string. In this example we assume that salt is an ASCII string. ' </param> ' <param name="hashAlgorithm"> ' Hash algorithm used to generate password. Allowed values are: "MD5" and ' "SHA1". SHA1 hashes are a bit slower, but more secure than MD5 hashes. ' </param> ' <param name="passwordIterations"> ' Number of iterations used to generate password. One or two iterations ' should be enough. ' </param> ' <param name="initVector"> ' Initialization vector (or IV). This value is required to encrypt the ' first block of plaintext data. For RijndaelManaged class IV must be ' exactly 16 ASCII characters long. ' </param> ' <param name="keySize"> ' Size of encryption key in bits. Allowed values are: 128, 192, and 256. ' Longer keys are more secure than shorter keys. ' </param> ' <returns> ' Decrypted string value. ' </returns> ' <remarks> ' Most of the logic in this function is similar to the Encrypt ' logic. In order for decryption to work, all parameters of this function ' - except cipherText value - must match the corresponding parameters of ' the Encrypt function which was called to generate the ' ciphertext. ' </remarks> Public Shared Function Decrypt(ByVal cipherText As String, _ ByVal passPhrase As String, _ ByVal saltValue As String, _ ByVal hashAlgorithm As String, _ ByVal passwordIterations As Integer, _ ByVal initVector As String, _ ByVal keySize As Integer) _ As String Dim myReturn As String = Nothing Try Dim initVectorBytes As Byte() initVectorBytes = Encoding.ASCII.GetBytes(initVector) Dim saltValueBytes As Byte() saltValueBytes = Encoding.ASCII.GetBytes(saltValue) ' Convert our ciphertext into a byte array. Dim cipherTextBytes As Byte() cipherTextBytes = Convert.FromBase64String(cipherText) Dim password As PasswordDeriveBytes password = New PasswordDeriveBytes(passPhrase, _ saltValueBytes, _ hashAlgorithm, _ passwordIterations) Dim keyBytes As Byte() keyBytes = password.GetBytes(keySize /
Dim symmetricKey As RijndaelManaged symmetricKey = New RijndaelManaged() symmetricKey.Mode = CipherMode.CBC Dim decryptor As ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes) Dim memoryStream As MemoryStream memoryStream = New MemoryStream(cipherTextBytes) Dim cryptoStream As CryptoStream cryptoStream = New CryptoStream(memoryStream, _ decryptor, _ CryptoStreamMode.Read) Dim plainTextBytes As Byte() ReDim plainTextBytes(cipherTextBytes.Length) Dim decryptedByteCount As Integer decryptedByteCount = cryptoStream.Read(plainTextBytes, _ 0, _ plainTextBytes.Length) memoryStream.Close() cryptoStream.Close() Dim plainText As String plainText = Encoding.UTF8.GetString(plainTextBytes, _ 0, _ decryptedByteCount) myReturn = plainText Catch ex As Exception End Try Return myReturn End Function End Class
Mar 17th
Purpose : To allow webpage to send an email.
Special Note : Requires additional code in web.config
Public Function sendEmail(ByVal myTo As String, ByVal mySubject As String, Optional ByVal myBody As String = "") As Boolean Dim myReturn As Boolean = False Try Dim myEmail As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage() Dim smtpClient As New System.Net.Mail.SmtpClient myEmail.Body = myBody myEmail.Subject = mySubject myEmail.To.Add(myTo) 'myEmail.Bcc.Add("userToBCC@yourdomain.com") smtpClient.Send(myEmail) myReturn = True ' The function has completed with NO errors Catch ex As Exception 'Handle Error Return False End Try Return myReturn End Function
CODE FOR WEB.CONFIG FILE
<system.net>
<mailSettings>
<smtp>
<network host="relay-hosting.secureserver.net" />
</smtp>
</mailSettings>
</system.net>
Mar 17th
Purpose : Allow user to download data in XML format.
Private Function upload_DataTable_AsXML(ByVal myDataTable As Data.DataTable, ByVal myFileNameWithoutExtension As String, Optional ByVal writeEmptyTable As Boolean = True) As Boolean Try If myDataTable Is Nothing Then Return False ' The table sent was NOTHING If myDataTable.Rows.Count < 1 And writeEmptyTable = False Then Return False ' The table sent has no rows, and the user does NOT want an XML file with no rows. Response.ContentType = "application/download" Response.AppendHeader("Content-Disposition", "attachment; filename=" & myFileNameWithoutExtension & ".XML") myDataTable.WriteXml(Response.OutputStream) Response.End() Return True ' The function has completed with NO errors Catch ex As Exception 'Handle Error Return False End Try Return False End Function