Solved

Encrypt dan Decrypt Data

Posted on 2000-02-27
7
442 Views
Last Modified: 2010-05-02
What to Encrypt and Decrypt data using VB6.  Somebody can help me?  Thanks Very much!
0
Comment
Question by:tonyckx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Expert Comment

by:paulstamp
ID: 2562432
Thats a very open question... do you mean encrypting individual strings, or files? Need more info.

If you're looking for a custom control to encrypt data try www.componentsource.com.
0
 
LVL 1

Expert Comment

by:holgrave
ID: 2562440
A basic encryption/decryption routine can be found at www.softcircuits.com.
0
 
LVL 18

Expert Comment

by:mdougan
ID: 2562538
This function will render a string unreadable/readable, but it's not much of an encryption algorithm.  It simply shifts the ASCII code by a constant amount (in this case 23 positions).  You can change this number to something different if you wish.  

Anyone who knows anything about cryptology could break this in a few minutes, but for the vast majority of people browsing around in a database, they're not going to be able to figure it out.

Function EncryptPassword(dPassword As String, encrypt As Integer) As String
'*************************************************************************************************************************
'   Encrypts or Decrypts a string if "encrypt" = True or False respectively
'   Example call:
'       SQL = SQL + "AND PASSWORD = '" & EncryptPassword(txtPassword.Text, True) & "'"
'   Result of call: "AND PASSWORD = '@^$34#$$*'"
'*************************************************************************************************************************
Dim sErrors As String
Dim lError As Variant

Dim sChar               As String
Dim iChar               As Integer
Dim iPos                As Integer
Dim iLen                As Integer
Dim strPassword         As String

    On Error GoTo EncryptPasswordErr
   
    strPassword = ""

    iLen = Len(dPassword)
    For iPos = 1 To iLen

        sChar = Mid(dPassword, iPos, 1)
        iChar = Asc(sChar)
        If encrypt Then
            iChar = iChar - 23
        Else
            iChar = iChar + 23
        End If
        sChar = Chr(iChar)
        strPassword = strPassword + sChar

    Next iPos

    EncryptPassword = strPassword

EncryptPasswordExit:
    Exit Function
EncryptPasswordErr:
    Screen.MousePointer = DEFAULT
    sErrors = "EncryptPassword " & Err.Description
    For Each lError In Errors
        sErrors = sErrors & vbCrLf & lError.Description
    Next
    MsgBox sErrors, vbCritical, Err & ""
    Resume EncryptPasswordExit
   
End Function

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:tonyckx
ID: 2578978
This question has a deletion request Pending
0
 
LVL 1

Expert Comment

by:holgrave
ID: 2579907
This question no longer is pending deletion
0
 
LVL 1

Expert Comment

by:holgrave
ID: 2579908
WHY?
It seems if you have had some pretty good answers.

holgrave.
0
 
LVL 14

Accepted Solution

by:
mcrider earned 100 total points
ID: 2608042
Here is code that will encrypt and decrypt strings.... All you have to do is read the unencrypted file into a string and pass the string to the Crypt function.  It will pass back an encrypted string that you can then save to a new file...

To decrypt a file, just do the same process again... Only this time, read the encrypted file into a string and pass it...


Cheers!®©


THE CODE:


Add This code to a module:

'-------------------------------------------------------------------------------
Public Function Crypt(Source As String, CryptKey As String) As String
    Dim lSource As String
    Dim iVal As Long
    Dim jVal As Long
    Dim lSwitch As Boolean
     
    lSwitch = True
    lSource = Source
    jVal = 0
    For iVal = 1 To Len(lSource)
        jVal = jVal + 1
        If jVal = Len(CryptKey) Then jVal = 1
        lSwitch = SwapBoolean(lSwitch)
        If lSwitch = True Then
            Mid$(lSource, iVal, 1) = Chr$(Asc(Mid$(lSource, iVal, 1)) Xor Asc(Mid$(CryptKey, jVal, 1)) + 1)
        Else
            Mid$(lSource, iVal, 1) = Chr$(Asc(Mid$(lSource, iVal, 1)) Xor Asc(Mid$(CryptKey, jVal, 1)) - 1)
        End If
    Next iVal
    Crypt = lSource
End Function
Public Function SwapBoolean(Source As Boolean) As Boolean
    If Source = True Then
        SwapBoolean = False
    Else
        SwapBoolean = True
    End If
End Function
'-------------------------------------------------------------------------------



Then you can do:


dim cString1 as string
dim cString2 as string
Cstring1=Crypt("String to encrypt","Password")


and Cstring1 will contain an encrypted string.

To unencrypt, do:

Cstring2=Crypt(Cstring1,"Password")

and Cstring2 will now have the unencrypted string.


By the way, in the above example, you can replace the password "Password" with anything you want.  This is your encryption mask, and it can be as long as you want... Just use the same string to encrypt that you use to decrypt, otherwise, the decrypt will fail and you get garbage (which is exactly what you want to have happen ;-)


Cheers!®©

0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question