473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jul 17 '05 #1
2 6482
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("kernel32.dll")> _
Public Sub ZeroMemory(ByVal 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 DESCryptoServiceProvider =
DESCryptoServiceProvider.Create()

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

End Function

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

On Error GoTo errhandle

Dim fsInput As New FileStream(sInputFilename, _
FileMode.Open, FileAccess.Read)
Dim fsEncrypted As New FileStream(sOutputFilename, _
FileMode.Create, FileAccess.Write)

Dim DES As New DESCryptoServiceProvider

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

'Set the initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

'Create the DES encryptor from this instance.
Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
'Create the crypto stream that transforms the file stream by
using DES encryption.
Dim cryptostream As New CryptoStream(fsEncrypted, _
desencrypt, _
CryptoStreamMode.Write)

'Read the file text to the byte array.
Dim bytearrayinput(fsInput.Length - 1) As Byte
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
'Write out the DES encrypted file.
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Close()

Return sOutputFilename

Exit Function

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

End Function

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

On Error GoTo errhandle

Dim DES As New DESCryptoServiceProvider

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

'Set the secret key for the DES algorithm.
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)

'Set the initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

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

'Create the DES decryptor from the DES instance.
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()

'Create the crypto stream set to read and to do a DES
decryption transform on incoming bytes.
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt,
CryptoStreamMode.Read)

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

fsDecrypted.Write(New
StreamReader(cryptostreamDecr).ReadToEnd)
fsDecrypted.Flush()
fsDecrypted.Close()

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.Pinned)
' Encrypt the file.
'EncryptFile(Application.StartupPath & "\settings.ini", _
' Application.StartupPath & "\Encrypted.txt", _
' sSecretKey)

' Decrypt the file.
'DecryptFile(application.StartupPath & "\Encrypted.txt", _
' application.StartupPath & "\Decrypted.txt", _
' sSecretKey)

' Remove the key from memory.
'ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
'gch.Free()
End Sub
End Class

Hope this helps!

On 4 Aug 2004 10:24:58 -0700, dh*******@chestnut.org (David) wrote:
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


Jul 17 '05 #2
dh*******@chestnut.org (David) wrote in message news:<83**************************@posting.google. com>...
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


Thanks, but never mind. I found it.

David
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Cliff | last post by:
We are trying to connect to 3 different Oracle databases using MS Access as the front-end and ODBC as the connection. The problem that we are having is that 1 of the databases requires a...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
7
by: Alan Silver | last post by:
Hello, I am writing a page where sensitive data is collected (over SSL) and stored in a database. I have been looking at the .NET encryption classes, but am a bit confused as to which is best...
2
by: Sumit Gupta | last post by:
Can anyone please tell me how to encrpt string or any kind of Data. Also the Algorithm of Compression. Any Link tutorial etc. Like : Zip or RAR Formats etc.
9
by: sweety | last post by:
Dear All, How to encrypt a C data file and make binary file and then have to read a bin file at run time and decrypt the file and have to read the data. Any help to achive this pls. Would be...
4
by: pintu | last post by:
Hello everybody.. I hav some confusion regarding asymmetric encryption.As asymmetric encryption it there is one private key and one public key.So any data is encrypted using private key and the...
1
by: =?Utf-8?B?bWljcm9ob2Y=?= | last post by:
Short version: Is there a way to configure (preferably programmatically) the max encryption strength that will be used by the framework when connecting to a particular SSL-protected web service? ...
11
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to...
22
by: j1mb0jay | last post by:
I have had to create a simple string encryption program for coursework, I have completed the task and now have to do a write up on how it could be improved at a later date. If you could look...
19
by: klenwell | last post by:
Another request for comments here. I'd like to accomplish something like the scheme outlined at this page here: http://tinyurl.com/3dtcdr In a nutshell, the form uses javascript to hash...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.