Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. Public Shared Function GetLogFilePath() As String
  2. Return IO.Path.Combine(Environment.CurrentDirectory, "Log.txt")
  3. End Function
  4.  
  5.  
  6.  
  7.  
  8. Public Shared Function DecryptString(EncryptedString As String) As String
  9. If String.IsNullOrEmpty(EncryptedString) Then
  10. Return String.Empty
  11. Else
  12. Return Decrypt(EncryptedString, "N3st22", "88552299", 2, "464R5DFA5DL6LE28", 256)
  13. End If
  14. End Function
  15.  
  16. Public Shared Function EncryptString(PlainString As String) As String
  17. If String.IsNullOrEmpty(PlainString) Then
  18. Return String.Empty
  19. Else
  20. Return Encrypt(PlainString, "N3st22", "88552299", 2, "464R5DFA5DL6LE28", 256)
  21. End If
  22. End Function
  23.  
  24. Public Shared Function Encrypt(ByVal plainText As String, _
  25. ByVal passPhrase As String, _
  26. ByVal saltValue As String, _
  27. ByVal passwordIterations As Integer, _
  28. ByVal initVector As String, _
  29. ByVal keySize As Integer) _
  30. As String
  31.  
  32. Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
  33. Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue)
  34. Dim plainTextBytes As Byte() = Encoding.ASCII.GetBytes(plainText)
  35. Dim password As New Rfc2898DeriveBytes(passPhrase, _
  36. saltValueBytes, _
  37. passwordIterations)
  38. Dim keyBytes As Byte() = password.GetBytes(CInt(keySize / 8))
  39. Dim symmetricKey As New AesCryptoServiceProvider
  40. symmetricKey.Mode = CipherMode.CBC
  41. Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
  42. Using memoryStream As New IO.MemoryStream()
  43. Using cryptoStream As New CryptoStream(memoryStream, _
  44. encryptor, _
  45. CryptoStreamMode.Write)
  46. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
  47. cryptoStream.FlushFinalBlock()
  48. Dim cipherTextBytes As Byte() = memoryStream.ToArray()
  49. memoryStream.Close()
  50. cryptoStream.Close()
  51. Return Convert.ToBase64String(cipherTextBytes)
  52. End Using
  53. End Using
  54. End Function
  55.  
  56. Public Shared Function Decrypt(ByVal cipherText As String, _
  57. ByVal passPhrase As String, _
  58. ByVal saltValue As String, _
  59. ByVal passwordIterations As Integer, _
  60. ByVal initVector As String, _
  61. ByVal keySize As Integer) _
  62. As String
  63.  
  64. Dim initVectorBytes As Byte()
  65. initVectorBytes = Encoding.ASCII.GetBytes(initVector)
  66.  
  67. Dim saltValueBytes As Byte()
  68. saltValueBytes = Encoding.ASCII.GetBytes(saltValue)
  69.  
  70. Dim cipherTextBytes As Byte()
  71. cipherTextBytes = Convert.FromBase64String(cipherText)
  72.  
  73. Dim password As New Rfc2898DeriveBytes(passPhrase, _
  74. saltValueBytes, _
  75. passwordIterations)
  76.  
  77. Dim keyBytes As Byte()
  78. keyBytes = password.GetBytes(CInt(keySize / 8))
  79.  
  80. Dim symmetricKey As New AesCryptoServiceProvider
  81. symmetricKey.Mode = CipherMode.CBC
  82.  
  83. Dim decryptor As ICryptoTransform
  84. decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
  85.  
  86. Dim memoryStream As IO.MemoryStream
  87. memoryStream = New IO.MemoryStream(cipherTextBytes)
  88.  
  89. Dim cryptoStream As CryptoStream
  90. cryptoStream = New CryptoStream(memoryStream, _
  91. decryptor, _
  92. CryptoStreamMode.Read)
  93.  
  94. Dim plainTextBytes As Byte()
  95. ReDim plainTextBytes(cipherTextBytes.Length)
  96.  
  97. Dim decryptedByteCount As Integer
  98. decryptedByteCount = cryptoStream.Read(plainTextBytes, _
  99. 0, _
  100. plainTextBytes.Length)
  101.  
  102. memoryStream.Close()
  103. cryptoStream.Close()
  104.  
  105. Dim plainText As String
  106. plainText = Encoding.ASCII.GetString(plainTextBytes, _
  107. 0, _
  108. decryptedByteCount)
  109.  
  110. Return plainText
  111. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement