School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,138 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,870 people online right now. Registration is fast and FREE... Join Now!



Creating an Encryption Program

Page 1 of 1

Creating an Encryption Program Encryption Program - The Other Way Rate Topic: -----

#1 Nikhil_07n  Icon User is offline

  • Idiot of the year.
  • Icon
  • View blog
  • Group: Author w/DIC++
  • Posts: 2,175
  • Joined: 09-January 09


Dream Kudos: 1200

Post icon  Posted 26 January 2009 - 06:31 PM

This time i've included all info through comments in the 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



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 ===================

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.
Was This Post Helpful? 0
  • +
  • -


#2 bayron.jofel  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 5
  • Joined: 16-April 09


Dream Kudos: 0

Posted 12 August 2009 - 08:56 AM

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

View Postbayron.jofel, on 12 Aug, 2009 - 08:55 AM, said:

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

Was This Post Helpful? 0
  • +
  • -

#3 cripplewarlord  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 2
  • Joined: 25-September 09


Dream Kudos: 0

Posted 25 September 2009 - 10:48 PM

View Postbayron.jofel, on 12 Aug, 2009 - 08:56 AM, said:

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

View Postbayron.jofel, on 12 Aug, 2009 - 08:55 AM, said:

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 ??
Was This Post Helpful? 0
  • +
  • -

#4 matsen  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 26-October 09


Dream Kudos: 0

Posted 26 October 2009 - 03:26 AM

Nice it actually worked :)

All you haft to do is to remove all:
Call percent(Picture1, 0)
Call percent(Picture1, 0)
Call percent(Picture1, CInt((i / Len(text) * 100)))



ONLY TRIED IT IN VB6

HOPE THIS HELPED!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



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