VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Encryption/decryption program

by Steve (12 Submissions)
Category: Coding Standards
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

This article explains how to encrypt a message and then decrypt it using an array and loops. Using random letters to change the original letters of the original message.

Rate Encryption/decryption program

First let me explain the algorithm.
For encrypting:
1) Dimension variables
2) Clear all variables
3) get the message
4) Loop through the message
5) In the loop, firstly randomize a number from 1 to 110 and hold this number in an array. Secondly increment a value by 1. Now, get the new character and add it to the encrypted message(Using chr$ in VB).
Now after the loop has finished the message has been encrypted.
To Decrypt it:
1) Get the coded message.
2) Loop through teh coded message.
3) increment a value by 1.
4) by using the Chr$, mid$ and asc functions, take away the ascii relative to codded letter and add the letter to a variable (decoded message).
In a module set these variables:
Global a% 'the value that increments in the loops
Global msgnum(10000) As Long 'the array that holds the ascii numericals relative to the letters
Global codedmsg As String 'the encrypted message
In your encryption procedure:
Dim n%
Dim x%
Dim message$
Dim emessage$
Dim word$
Dim ctext
Dim i As Integer
Erase msgnum 'erase all value in the array
a% = 0
ctext = txtmessage.Text 
word$ = "" 
For i = 1 To Len(ctext) 'loop through the string
 Randomize
  x% = Int((110 * Rnd) + 1) 'randomize a number from 1 to 110.
  a% = a% + 1 'Increment this value as it is used in the array
 word$ = word$ & Chr$(Asc(Mid(c_text, i, 1)) + x%)
'add the original letter ascii value to the randomized value and produce that character
  msgnum(a%) = x% 'hold the randomized number in the array
Next i
codedmsg = word$
In your decryption procedure:
Dim msg$
Dim x%
Dim ctext
Dim word$
Dim decodedmsg
Dim i As Integer

ctext = codedmsg
word$ = ""
a% = 0 
For i = 1 To Len(c_text) 'Loop through the coded message
  a% = a% + 1 'Increment value by 1
word$ = word$ & Chr$(Asc(Mid(c_text, i, 1)) - msgnum(a%))
'this time take away the the randomized value, which is held in the array from the codded character ascii value to produce the original letter
Next i
decodedmsg = word$ 'Decoded msg

Download this snippet    Add to My Saved Code

Encryption/decryption program Comments

No comments have been posted about Encryption/decryption program. Why not be the first to post a comment about Encryption/decryption program.

Post your comment

Subject:
Message:
0/1000 characters