Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 34,085 times

Related Categories

Encrypt/Decrypt

This is a test of a rudimentary encryption/decryption 
algorithm.

Note: It is possible for the encrypted string to include Chr$(0). 
Visual Basic handles this just fine but Windows (and 
therefore the TextBox control) uses Chr$(0) to signify the end 
of the string. Therefore, programs such as this one that store 
the encrypted data in a text box may end up truncating the data.

First, add a textbox, called txtText, with its MultiLine property set to true. Then, add another textbox called txtPassword, and two command buttons named cmdEncrypt, and cmdDecrypt. Finally, add the code below.

'This program may be distributed on the condition that it is
'distributed in full and unchanged, and that no fee is charged for
'such distribution with the exception of reasonable shipping and media
'charged. In addition, the code in this program may be incorporated
'into your own programs and the resulting programs may be distributed
'without payment of royalties.
'
'This example program was provided by:
' SoftCircuits Programming
' http://www.softcircuits.com
' P.O. Box 16262
' Irvine, CA 92623

Option Explicit

'Set to True to make the password case-sensitive
#Const CASE_SENSITIVE_PASSWORD = False

Private Sub cmdEncrypt_Click()
    ' You can encrypt twice for extra security
    txtText = EncryptText((txtText), txtPassword)
    txtText = EncryptText((txtText), txtPassword)
End Sub

Private Sub cmdDecrypt_Click()
    txtText = DecryptText((txtText), txtPassword)
    txtText = DecryptText((txtText), txtPassword)
End Sub

'Encrypt text
Private Function EncryptText(strText As String, ByVal strPwd As String)
    Dim i As Integer, c As Integer
    Dim strBuff As String

#If Not CASE_SENSITIVE_PASSWORD Then

    'Convert password to upper case
    'if not case-sensitive
    strPwd = UCase$(strPwd)

#End If

    'Encrypt string
    If Len(strPwd) Then
        For i = 1 To Len(strText)
            c = Asc(Mid$(strText, i, 1))
            c = c + Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
            strBuff = strBuff & Chr$(c And &HFF)
        Next i
    Else
        strBuff = strText
    End If
    EncryptText = strBuff
End Function

'Decrypt text encrypted with EncryptText
Private Function DecryptText(strText As String, ByVal strPwd As String)
    Dim i As Integer, c As Integer
    Dim strBuff As String

#If Not CASE_SENSITIVE_PASSWORD Then

    'Convert password to upper case
    'if not case-sensitive
    strPwd = UCase$(strPwd)

#End If

    'Decrypt string
    If Len(strPwd) Then
        For i = 1 To Len(strText)
            c = Asc(Mid$(strText, i, 1))
            c = c - Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
            strBuff = strBuff & Chr$(c And &HFF)
        Next i
    Else
        strBuff = strText
    End If
    DecryptText = strBuff
End Function

Comments

  • Re: [157] Encrypt/Decrypt

    Posted by amilath on 22 Aug 2007

    Dear Anuradha

    This  is the  C#  Code

    class Class1
        {
            // Encryption Method
        &...

  • Re: [157] Encrypt/Decrypt

    Posted by ashnir on 11 Jul 2007

    hi,


    i implemented the code and it encrypts correctly,but when i decrypt it it still gives me weird characters and not the string that i had passed to encrypt function.


    Can you pleas...

  • Re: [157] Encrypt/Decrypt

    Posted by ajanu81 on 20 Apr 2007

    hi,

    I have been trying to use your code for my application in C#,but i am less successful.

    Could you please post the code in C#?

    Thanks in advance.

    Regards

    Anuradha<...

  • Re: [157] Encrypt/Decrypt

    Posted by mahmud_uzzaman on 11 Feb 2007

    The problem with the chr(0) can be solved by this process.

    The pseudocode is like this. In the final encrypted text, the chr(0) cuts the rest of the string. We can
    mark those positions and ...

  • error reading encrypted word

    Posted by miglou on 30 Sep 2003

    Hello,
    i'm doing a small app to create encrypt keys, and i'm used your example, except in Chr$ i got an error and just used Chr, and it works fine.the problem is, after create encrypt word, and saved...