Advertisement
Guest User

Encryption Function

a guest
Nov 14th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.65 KB | None | 0 0
  1. Function Encrypt(text As String, pw As String, type_of As Boolean) As String
  2. 'Declaring variables
  3. Dim x As Integer
  4. Dim i As Integer
  5. Dim a As Integer
  6. Dim text_chr As String
  7. Dim text_asc As Integer
  8. Dim pw_chr As String
  9. Dim pw_asc As Integer
  10. Dim fin As String
  11. Dim fin_chr As String
  12. Dim fin_asc As Integer
  13.  
  14. 'if type_of = true then Encrypt
  15. 'if type_of = False then Decrypt
  16. Call percent(Picture1, 0) 'clear percent
  17. 'making sure there is text to encrypt and a password
  18. 'to go with it
  19. If Len(text$) = 0 Then Exit Function
  20. If Len(pw$) = 0 Then Exit Function
  21.  
  22. x% = 1
  23. 'the X variable is the loop that goes through the password
  24. 'characters individually through the encrpytion processes
  25. 'X = 1 to set the loop at the first character
  26. For i% = 1 To Len(text$) 'start of encrpyt loop
  27.     'taking out characters from text to encrypt
  28.     'the single character
  29.     text_chr$ = Mid(text$, i, 1)
  30.     'changing the character to its ASCII value to
  31.     'easily change the character for encrypting
  32.     text_asc% = Asc(text_chr$)
  33.     'doing the same process with the password
  34.     'using the X variable
  35.     pw_chr$ = Mid(pw$, x, 1)
  36.     pw_asc% = Asc(pw_chr$)
  37.     'adding up variable to continue loop through different
  38.     'characters within the password
  39.     x% = x% + 1
  40.     If x% > Len(pw$) Then x% = 1 'restarting password loop
  41.     'Case to check if the user is Encrypting or Decrypting the text
  42.     Select Case type_of
  43.     Case True: 'Encrypting
  44.         'adding the characters of both string and password
  45.         fin_asc% = text_asc% + pw_asc%
  46.         'making sure the final_asc will equal a valid ASCII character
  47.         If fin_asc% > 255 Then
  48.             'Character was an invalid character so we modify it
  49.             'to equal a valid character
  50.             a% = fin_asc% - 255
  51.             fin_chr$ = Chr$(a%)
  52.         Else
  53.             'character was valid;D
  54.             fin_chr$ = Chr$(fin_asc%)
  55.         End If
  56.     Case False: 'Decrypting
  57.         'here we subtract the characters...does the opposite of
  58.         'what encrypting does to put it back in its
  59.         'original state, which is why it's called Decrypting
  60.         fin_asc% = text_asc% - pw_asc% 'subtracting character values
  61.         If fin_asc% < 1 Then   'checking for invalid character
  62.             'invalid character..fixing problem =)
  63.             a% = fin_asc% + 255
  64.             fin_chr$ = Chr$(a%)
  65.         Else
  66.             'it was all good.
  67.             fin_chr$ = Chr$(fin_asc%)
  68.         End If
  69.     End Select 'End of case
  70.     'adding the final encrypted character to a string
  71.     'to be later shown in its final state at the end
  72.     fin$ = fin$ & fin_chr$
  73.     'thought i'd be mr. fancy pants by adding a little
  74.     'percentage bar =)
  75.     Call percent(Picture1, CInt((i / Len(text) * 100)))
  76. Next 'continuing loop =)
  77. 'finalizing function to equal the final encrypted string
  78. Encrypt$ = fin$
  79. DoEvents
  80. Call percent(Picture1, 0)
  81. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement