School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,314 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,055 people online right now. Registration is fast and FREE... Join Now!




Creating an Encryption Program

 
Reply to this topicStart new topic

> Creating an Encryption Program, Encryption Program - The Other Way

Nikhil_07n
Group Icon



post 26 Jan, 2009 - 06:31 PM
Post #1


This time i've included all info through comments in the code.

CODE


Private Sub Command1_Click()
Dim a As String
Dim B As String
a$ = Text1.text 'Text to encrypt
B$ = Text2.text 'Password
'encrypting text with a return to text1
Text1.text = Encrypt(a$, B$, True) 'true = Encrypt
End Sub



Explanation : Command1 is a Command Button used to Encrypt text

CODE



Private Sub Command2_Click()
Dim a As String
Dim B As String
a$ = Text1.text 'Text to decrypt
B$ = Text2.text 'Password
'decrypting text with a return to text1
Text1.text = Encrypt(a$, B$, False) 'false = decrypt
End Sub



Explanation : Command2 is a command button to decrypt.


==================Encrypt Function ===================

CODE

Function Encrypt(text As String, pw As String, type_of As Boolean) As String
'Declaring variables
Dim x As Integer
Dim i As Integer
Dim a As Integer
Dim text_chr As String
Dim text_asc As Integer
Dim pw_chr As String
Dim pw_asc As Integer
Dim fin As String
Dim fin_chr As String
Dim fin_asc As Integer

'if type_of = true then Encrypt
'if type_of = False then Decrypt
Call percent(Picture1, 0) 'clear percent
'making sure there is text to encrypt and a password
'to go with it
If Len(text$) = 0 Then Exit Function
If Len(pw$) = 0 Then Exit Function

x% = 1
'the X variable is the loop that goes through the password
'characters individually through the encrpytion processes
'X = 1 to set the loop at the first character
For i% = 1 To Len(text$) 'start of encrpyt loop
    'taking out characters from text to encrypt
    'the single character
    text_chr$ = Mid(text$, i, 1)
    'changing the character to its ASCII value to
    'easily change the character for encrypting
    text_asc% = Asc(text_chr$)
    'doing the same process with the password
    'using the X variable
    pw_chr$ = Mid(pw$, x, 1)
    pw_asc% = Asc(pw_chr$)
    'adding up variable to continue loop through different
    'characters within the password
    x% = x% + 1
    If x% > Len(pw$) Then x% = 1 'restarting password loop
    'Case to check if the user is Encrypting or Decrypting the text
    Select Case type_of
    Case True: 'Encrypting
        'adding the characters of both string and password
        fin_asc% = text_asc% + pw_asc%
        'making sure the final_asc will equal a valid ASCII character
        If fin_asc% > 255 Then
            'Character was an invalid character so we modify it
            'to equal a valid character
            a% = fin_asc% - 255
            fin_chr$ = Chr$(a%)
        Else
            'character was valid;D
            fin_chr$ = Chr$(fin_asc%)
        End If
    Case False: 'Decrypting
        'here we subtract the characters...does the opposite of
        'what encrypting does to put it back in its
        'original state, which is why it's called Decrypting
        fin_asc% = text_asc% - pw_asc% 'subtracting character values
        If fin_asc% < 1 Then   'checking for invalid character
            'invalid character..fixing problem =)
            a% = fin_asc% + 255
            fin_chr$ = Chr$(a%)
        Else
            'it was all good.
            fin_chr$ = Chr$(fin_asc%)
        End If
    End Select 'End of case
    'adding the final encrypted character to a string
    'to be later shown in its final state at the end
    fin$ = fin$ & fin_chr$
    'thought i'd be mr. fancy pants by adding a little
    'percentage bar =)
    Call percent(Picture1, CInt((i / Len(text) * 100)))
Next 'continuing loop =)
'finalizing function to equal the final encrypted string
Encrypt$ = fin$
DoEvents
Call percent(Picture1, 0)
End Function



NOTE : For details, carefully go through the Comments.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

bayron.jofel
*



post 12 Aug, 2009 - 08:56 AM
Post #2
what's the use of $ this sign for your variable

QUOTE(bayron.jofel @ 12 Aug, 2009 - 08:55 AM) *

what's the use of $ this sign for your variable a and b

Go to the top of the page
+Quote Post

cripplewarlord
*



post 25 Sep, 2009 - 10:48 PM
Post #3
QUOTE(bayron.jofel @ 12 Aug, 2009 - 08:56 AM) *

what's the use of $ this sign for your variable

QUOTE(bayron.jofel @ 12 Aug, 2009 - 08:55 AM) *

what's the use of $ this sign for your variable a and b

i keep getting a error it says expected end sub at the a$ bit ??
Go to the top of the page
+Quote Post

matsen
*



post 26 Oct, 2009 - 03:26 AM
Post #4
Nice it actually worked smile.gif

All you haft to do is to remove all:
CODE

Call percent(Picture1, 0)
Call percent(Picture1, 0)
Call percent(Picture1, CInt((i / Len(text) * 100)))


ONLY TRIED IT IN VB6

HOPE THIS HELPED!
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 02:20PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month