DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   vbnet (http://www.daniweb.com/code/vbnet.html)
-   -   Simple Encryption (http://www.daniweb.com/code/snippet726.html)

Fungus1487 vbnet syntax
Jun 16th, 2007
below is a very simple encryption class which shows how to encrypt sensitive data (very simply) this example converts a string to hexadecimal form and back again with some simple error handling. please leave some feedback if you find this useful

  1. Public Class clsCrypt
  2. Public Function Encrypt(ByVal sCrypt As String) As String
  3. On Error GoTo Err_RaiseErrorClass
  4. Dim tmp As String
  5. Dim sVal As String
  6. Dim l As Long
  7. For l = 1 To Len(sCrypt)
  8. sVal = HexValue(Mid(sCrypt, l, 1))
  9. If Len(sVal) > 4 Then
  10. If Left(sVal, 4) = "ERR:" Then
  11. tmp = sVal
  12. Exit For
  13. End If
  14. End If
  15. If Len(sVal) = 1 Then sVal = "0" & sVal
  16. tmp = tmp & sVal
  17. Next l
  18. Encrypt = tmp
  19. Exit_RaiseErrorClass:
  20. Exit Function
  21. Err_RaiseErrorClass:
  22. MsgBox(Err.Number & ": " & Err.Description)
  23. Resume Exit_RaiseErrorClass
  24. End Function
  25.  
  26. Public Function Decrypt(ByVal sCrypt As String) As String
  27. On Error GoTo Err_RaiseErrorClass
  28. Dim tmp As String
  29. Dim sVal As String
  30. Dim str As String
  31. Dim l As Long
  32. sVal = UCase(sCrypt)
  33. Do Until sVal = ""
  34. If Len(sVal) < 2 Then
  35. str = ChrValue(sVal)
  36. If Len(str) > 4 Then
  37. If Left(str, 4) = "ERR:" Then
  38. tmp = str
  39. Exit Do
  40. End If
  41. End If
  42. tmp = tmp & str
  43. sVal = ""
  44. Else
  45. str = Left(sVal, 2)
  46. If Left(str, 1) = "0" Then str = Right(str, 1)
  47. str = ChrValue(str)
  48. If Len(str) > 4 Then
  49. If Left(str, 4) = "ERR:" Then
  50. tmp = str
  51. Exit Do
  52. End If
  53. End If
  54. tmp = tmp & str
  55. sVal = Right(sVal, Len(sVal) - 2)
  56. End If
  57. Loop
  58. Decrypt = tmp
  59. Exit_RaiseErrorClass:
  60. Exit Function
  61. Err_RaiseErrorClass:
  62. MsgBox(Err.Number & ": " & Err.Description)
  63. Resume Exit_RaiseErrorClass
  64. End Function
  65.  
  66. Private Function HexValue(ByVal sChr As String) As String
  67. On Error GoTo Err_HexValue
  68. Dim iDec As Integer
  69. iDec = Asc(sChr)
  70. HexValue = Hex(iDec)
  71. Exit_HexValue:
  72. Exit Function
  73. Err_HexValue:
  74. HexValue = "ERR:" & Err.Number
  75. Resume Exit_HexValue
  76. End Function
  77.  
  78. Private Function ChrValue(ByVal sHex As String) As String
  79. On Error GoTo Err_ChrValue
  80. Dim sChr As String
  81. Dim dblVal As Double
  82. Dim i As Integer
  83. Const cIdx = 16
  84. For i = 1 To Len(sHex)
  85. sChr = Mid(sHex, i, 1)
  86. If sChr <> " " Then
  87. If sChr <= "9" Then
  88. dblVal = dblVal + CInt(sChr)
  89. Else
  90. dblVal = dblVal + ((Asc(sChr) - 55) Mod 32)
  91. End If
  92. If i < Len(sHex) Then dblVal = dblVal * cIdx
  93. End If
  94. Next i
  95. ChrValue = Chr(dblVal)
  96. Exit_ChrValue:
  97. Exit Function
  98. Err_ChrValue:
  99. ChrValue = "ERR:" & Err.Number
  100. Resume Exit_ChrValue
  101. End Function
  102. End Class