VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



A smaller encryption example

by Ben Doherty (5 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

Takes a string and shifts each character in that string according to a password set by the user. Much smaller than most of the examples of VBC.

Assumes
you need to put in 2 text boxes (text1 -string to encrypt) (text2 - where the password goes) and 2 command buttons cmdEncrypt and cmdDecrypt then just copy and paste the following code.

Rate A smaller encryption example

Private Sub cmdEncrypt_Click()
pass$ = Len(password.Text) 'the number you shift each letter to encrypt
tmpstr = Len(Text1.Text)
If tmpstr = "0" Then
MsgBox ("You must first type in something to Encrypt") 'You can't encrypt nothing
Exit Sub
End If
For i = 1 To tmpstr
letter = Mid$(Text1.Text, i, 1)   'takes the ascii value and adds the length of the password to it
encstr = Asc(letter) + pass$
newstr = Chr$(encstr)    'changes ascii value to a character
encrypted$ = encrypted$ & newstr 'puts all the encrypted characters together
Next i
Text1.Text = encrypted$  'puts the encrypted string in text box
End Sub
Private Sub cmdDecrypt_Click()
pass$ = Len(password.Text)        'this is the exact same for the Encrypt Function
tmpstr = Len(Text1.Text)        'the only difference is that instead of adding the lenght of password.text
              'it is subtracted
If tmpstr = "0" Then
MsgBox ("You must first type in something to Decrypt")
Exit Sub
End If
For i = 1 To tmpstr
letter = Mid$(Text1.Text, i, 1)
encstr = Asc(letter) - pass$
newstr = Chr$(encstr)
decrypted$ = decrypted$ & newstr
Next i
Text1.Text = decrypted$
End Sub

Download this snippet    Add to My Saved Code

A smaller encryption example Comments

No comments have been posted about A smaller encryption example. Why not be the first to post a comment about A smaller encryption example.

Post your comment

Subject:
Message:
0/1000 characters