Encryption

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    Encryption

    Hope someone can help or have a free control. I am going to be passing
    data from a client computer to a server via a winsock control. I want
    to encrypt/decript the contents of the string that is passed back and
    forth. Anyone have any ideas?

    David
  • Eddie B

    #2
    Re: Encryption

    Are you using VB6 or .Net? .Net has an encryption class you could use
    - System.Security .Cryptography

    Here is the code for a DLL I use for that same reason. The Sub Main
    routine is commented out, it will generate a key, but I use the same
    one, so I don't need this part.

    =============== =============== ==========

    Imports System
    Imports System.IO
    Imports System.Security
    Imports System.Security .Cryptography
    Imports System.Runtime. InteropServices
    Imports System.Text


    Public Class Class1


    ' Call this function to remove the key from memory after it is
    used for security.
    <DllImport("ker nel32.dll")> _
    Public Sub ZeroMemory(ByVa l addr As IntPtr, ByVal size As Integer)
    End Sub

    ' Function to generate a 64-bit key.
    Function GenerateKey() As String
    ' Create an instance of a symmetric algorithm. The key and the
    IV are generated automatically.
    Dim desCrypto As DESCryptoServic eProvider =
    DESCryptoServic eProvider.Creat e()

    ' Use the automatically generated key for encryption.
    Return ASCIIEncoding.A SCII.GetString( desCrypto.Key)

    End Function

    Public Shared Function EncryptFile(ByV al sInputFilename As String,
    _
    ByVal sOutputFilename As String, _
    ByVal sKey As String) As String

    On Error GoTo errhandle

    Dim fsInput As New FileStream(sInp utFilename, _
    FileMode.Open, FileAccess.Read )
    Dim fsEncrypted As New FileStream(sOut putFilename, _
    FileMode.Create , FileAccess.Writ e)

    Dim DES As New DESCryptoServic eProvider

    'Set secret key for DES algorithm.
    'A 64-bit key and an IV are required for this provider.
    DES.Key = ASCIIEncoding.A SCII.GetBytes(s Key)

    'Set the initialization vector.
    DES.IV = ASCIIEncoding.A SCII.GetBytes(s Key)

    'Create the DES encryptor from this instance.
    Dim desencrypt As ICryptoTransfor m = DES.CreateEncry ptor()
    'Create the crypto stream that transforms the file stream by
    using DES encryption.
    Dim cryptostream As New CryptoStream(fs Encrypted, _
    desencrypt, _
    CryptoStreamMod e.Write)

    'Read the file text to the byte array.
    Dim bytearrayinput( fsInput.Length - 1) As Byte
    fsInput.Read(by tearrayinput, 0, bytearrayinput. Length)
    'Write out the DES encrypted file.
    cryptostream.Wr ite(bytearrayin put, 0, bytearrayinput. Length)
    cryptostream.Cl ose()

    Return sOutputFilename

    Exit Function

    errhandle:
    Return "Error Encrypting File!" & vbCrLf & Err.Description

    End Function

    Public Shared Function DecryptFile(ByV al sInputFilename As String,
    _
    ByVal sOutputFilename As String, _
    ByVal sKey As String) As String

    On Error GoTo errhandle

    Dim DES As New DESCryptoServic eProvider

    'A 64-bit key and an IV are required for this provider.

    'Set the secret key for the DES algorithm.
    DES.Key() = ASCIIEncoding.A SCII.GetBytes(s Key)

    'Set the initialization vector.
    DES.IV = ASCIIEncoding.A SCII.GetBytes(s Key)

    'Create the file stream to read the encrypted file back.
    Dim fsread As New FileStream(sInp utFilename, FileMode.Open,
    FileAccess.Read )

    'Create the DES decryptor from the DES instance.
    Dim desdecrypt As ICryptoTransfor m = DES.CreateDecry ptor()

    'Create the crypto stream set to read and to do a DES
    decryption transform on incoming bytes.
    Dim cryptostreamDec r As New CryptoStream(fs read, desdecrypt,
    CryptoStreamMod e.Read)

    'Print out the contents of the decrypted file.
    Dim fsDecrypted As New StreamWriter(sO utputFilename)

    fsDecrypted.Wri te(New
    StreamReader(cr yptostreamDecr) .ReadToEnd)
    fsDecrypted.Flu sh()
    fsDecrypted.Clo se()

    Return sOutputFilename

    Exit Function

    errhandle:
    Return "Error Encrypting File!" & vbCrLf & Err.Description

    End Function

    Public Sub Main()
    'Must be 64 bits, 8 bytes.
    'Dim sSecretKey As String

    ' Get the key for the file to encrypt.
    ' You can distribute this key to the user who will decrypt the
    file.
    'sSecretKey = GenerateKey()

    ' For additional security, pin the key.
    'Dim gch As GCHandle = GCHandle.Alloc( sSecreyKey,
    GCHandleType.Pi nned)


    ' Encrypt the file.
    'EncryptFile(Ap plication.Start upPath & "\settings.ini" , _
    ' Application.Sta rtupPath & "\Encrypted.txt ", _
    ' sSecretKey)

    ' Decrypt the file.
    'DecryptFile(ap plication.Start upPath & "\Encrypted.txt ", _
    ' application.Sta rtupPath & "\Decrypted.txt ", _
    ' sSecretKey)

    ' Remove the key from memory.
    'ZeroMemory(gch .AddrOfPinnedOb ject(), sSecretKey.Leng th * 2)
    'gch.Free()
    End Sub
    End Class

    Hope this helps!

    On 4 Aug 2004 10:24:58 -0700, dhodgkins@chest nut.org (David) wrote:
    [color=blue]
    >Hope someone can help or have a free control. I am going to be passing
    >data from a client computer to a server via a winsock control. I want
    >to encrypt/decript the contents of the string that is passed back and
    >forth. Anyone have any ideas?
    >
    >David[/color]

    Comment

    • David

      #3
      Re: Encryption

      dhodgkins@chest nut.org (David) wrote in message news:<83b47ff1. 0408040924.3742 358f@posting.go ogle.com>...[color=blue]
      > Hope someone can help or have a free control. I am going to be passing
      > data from a client computer to a server via a winsock control. I want
      > to encrypt/decript the contents of the string that is passed back and
      > forth. Anyone have any ideas?
      >
      > David[/color]

      Thanks, but never mind. I found it.

      David

      Comment

      Working...