Advertisement
ZeekoSec

Text Encrypter 3 VB.NET

Mar 25th, 2015
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. Imports System.Security
  2. Imports System.Security.Cryptography
  3. Imports System.IO
  4. Imports System.Runtime.InteropServices
  5. Imports System.Text.RegularExpressions
  6. Imports System.Text
  7.  
  8. Public Function Encrypt(ByVal plainText As String) As String
  9.  
  10. Dim passPhrase As String = "yourPassPhrase"
  11. Dim saltValue As String = "mySaltValue"
  12. Dim hashAlgorithm As String = "SHA1"
  13.  
  14. Dim passwordIterations As Integer = 2
  15. Dim initVector As String = "@1B2c3D4e5F6g7H8"
  16. Dim keySize As Integer = 256
  17.  
  18. Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
  19. Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue)
  20.  
  21. Dim plainTextBytes As Byte() = Encoding.UTF8.GetBytes(plainText)
  22.  
  23.  
  24. Dim password As New PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations)
  25.  
  26. Dim keyBytes As Byte() = password.GetBytes(keySize \ 8)
  27. Dim symmetricKey As New RijndaelManaged()
  28.  
  29. symmetricKey.Mode = CipherMode.CBC
  30.  
  31. Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
  32.  
  33. Dim memoryStream As New MemoryStream()
  34. Dim cryptoStream As New CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)
  35.  
  36. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
  37. cryptoStream.FlushFinalBlock()
  38. Dim cipherTextBytes As Byte() = memoryStream.ToArray()
  39. memoryStream.Close()
  40. cryptoStream.Close()
  41. Dim cipherText As String = Convert.ToBase64String(cipherTextBytes)
  42. Return cipherText
  43. End Function
  44.  
  45. Public Function Decrypt(ByVal cipherText As String) As String
  46. Dim passPhrase As String = "yourPassPhrase"
  47. Dim saltValue As String = "mySaltValue"
  48. Dim hashAlgorithm As String = "SHA1"
  49.  
  50. Dim passwordIterations As Integer = 2
  51. Dim initVector As String = "@1B2c3D4e5F6g7H8"
  52. Dim keySize As Integer = 256
  53. ' Convert strings defining encryption key characteristics into byte
  54. ' arrays. Let us assume that strings only contain ASCII codes.
  55. ' If strings include Unicode characters, use Unicode, UTF7, or UTF8
  56. ' encoding.
  57. Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
  58. Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue)
  59.  
  60. ' Convert our ciphertext into a byte array.
  61. Dim cipherTextBytes As Byte() = Convert.FromBase64String(cipherText)
  62.  
  63. ' First, we must create a password, from which the key will be
  64. ' derived. This password will be generated from the specified
  65. ' passphrase and salt value. The password will be created using
  66. ' the specified hash algorithm. Password creation can be done in
  67. ' several iterations.
  68. Dim password As New PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations)
  69.  
  70. ' Use the password to generate pseudo-random bytes for the encryption
  71. ' key. Specify the size of the key in bytes (instead of bits).
  72. Dim keyBytes As Byte() = password.GetBytes(keySize \ 8)
  73.  
  74. ' Create uninitialized Rijndael encryption object.
  75. Dim symmetricKey As New RijndaelManaged()
  76.  
  77. ' It is reasonable to set encryption mode to Cipher Block Chaining
  78. ' (CBC). Use default options for other symmetric key parameters.
  79. symmetricKey.Mode = CipherMode.CBC
  80.  
  81. ' Generate decryptor from the existing key bytes and initialization
  82. ' vector. Key size will be defined based on the number of the key
  83. ' bytes.
  84. Dim decryptor As ICryptoTransform = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
  85.  
  86. ' Define memory stream which will be used to hold encrypted data.
  87. Dim memoryStream As New MemoryStream(cipherTextBytes)
  88.  
  89. ' Define cryptographic stream (always use Read mode for encryption).
  90. Dim cryptoStream As New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)
  91.  
  92. ' Since at this point we don't know what the size of decrypted data
  93. ' will be, allocate the buffer long enough to hold ciphertext;
  94. ' plaintext is never longer than ciphertext.
  95. Dim plainTextBytes As Byte() = New Byte(cipherTextBytes.Length - 1) {}
  96.  
  97. ' Start decrypting.
  98. Dim decryptedByteCount As Integer = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)
  99.  
  100. ' Close both streams.
  101. memoryStream.Close()
  102. cryptoStream.Close()
  103.  
  104. ' Convert decrypted data into a string.
  105. ' Let us assume that the original plaintext string was UTF8-encoded.
  106. Dim plainText As String = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)
  107.  
  108. ' Return decrypted string.
  109. Return plainText
  110. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement