# Encryption/decryption program

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.

Original Author: Steve

### Code

`First let me explain the algorithm.For encrypting:1) Dimension variables2) Clear all variables3) get the message4) Loop through the message5) 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 loopsGlobal msgnum(10000) As Long 'the array that holds the ascii numericals relative to the lettersGlobal codedmsg As String 'the encrypted messageIn your encryption procedure:Dim n%Dim x%Dim message\$Dim emessage\$Dim word\$Dim ctextDim i As IntegerErase msgnum 'erase all value in the arraya% = 0ctext = 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 arrayNext icodedmsg = word\$In your decryption procedure:Dim msg\$Dim x%Dim ctextDim word\$Dim decodedmsgDim i As Integerctext = codedmsgword\$ = ""a% = 0 For i = 1 To Len(c_text) 'Loop through the coded message  a% = a% + 1 'Increment value by 1word\$ = 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 letterNext idecodedmsg = word\$ 'Decoded msg`

Posted: 2002-06-01
By: ArchiveBot
Viewed: 91 times

Categories

Visual Basic 6

Attachments

No attachments for this post