workflows

This commit is contained in:
Jonathan Jenne
2019-07-02 14:54:57 +02:00
parent 8a30f645f8
commit 507e67309f
14 changed files with 595 additions and 210 deletions

View File

@@ -1,5 +1,6 @@
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text.Encoding
Imports DigitalData.Modules.Logging
''' <summary>
@@ -19,8 +20,6 @@ Public Class Encryption
Private ReadOnly _password As String
Private _logger As Logger
Public Sub New(LogConfig As LogConfig, Password As String)
_logger = LogConfig.GetLogger()
@@ -31,13 +30,24 @@ Public Class Encryption
_password = Password
End Sub
Public Async Function EncryptAsync(oPlainTextBytes As Byte()) As Task(Of Byte())
Public Async Function EncryptAsync(PlainTextBytes As Byte()) As Task(Of Byte())
Return Await Task.Run(Function() As Byte()
Return Encrypt(oPlainTextBytes)
Return Encrypt(PlainTextBytes)
End Function)
End Function
Public Function Encrypt(oPlainTextBytes As Byte()) As Byte()
Public Function Encrypt(PlainText As String) As String
Try
Dim oBytes As Byte() = UTF8.GetBytes(PlainText)
Dim oEncrypted As Byte() = Encrypt(oBytes)
Return UTF8.GetString(oEncrypted)
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Public Function Encrypt(PlainTextBytes As Byte()) As Byte()
Try
' Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
' so that the same Salt and IV values can be used when decrypting.
@@ -53,7 +63,7 @@ Public Class Encryption
Using oEncryptor = oSymmetricKey.CreateEncryptor(oKeyBytes, oIvStringBytes)
Using oMemoryStream = New MemoryStream()
Using oCryptoStream = New CryptoStream(oMemoryStream, oEncryptor, CryptoStreamMode.Write)
oCryptoStream.Write(oPlainTextBytes, 0, oPlainTextBytes.Length)
oCryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length)
oCryptoStream.FlushFinalBlock()
' Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
Dim oCipherTextBytes = oSaltStringBytes
@@ -79,16 +89,27 @@ Public Class Encryption
End Function)
End Function
Public Function Decrypt(cipherTextBytesWithSaltAndIv As Byte()) As Byte()
Public Function Decrypt(CipherTextPlainWithSaltAndIv As String) As String
Try
Dim oBytes As Byte() = UTF8.GetBytes(CipherTextPlainWithSaltAndIv)
Dim oDecrypted As Byte() = Decrypt(oBytes)
Return UTF8.GetString(oDecrypted)
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Public Function Decrypt(CipherTextBytesWithSaltAndIv As Byte()) As Byte()
Try
' Get the complete stream of bytes that represent:
' [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
' Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
Dim oSaltStringBytes = cipherTextBytesWithSaltAndIv.Take(KEY_SIZE / 8).ToArray()
Dim oSaltStringBytes = CipherTextBytesWithSaltAndIv.Take(KEY_SIZE / 8).ToArray()
' Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
Dim oIvStringBytes = cipherTextBytesWithSaltAndIv.Skip(KEY_SIZE / 8).Take(KEY_SIZE / 8).ToArray()
Dim oIvStringBytes = CipherTextBytesWithSaltAndIv.Skip(KEY_SIZE / 8).Take(KEY_SIZE / 8).ToArray()
' Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
Dim oCipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((KEY_SIZE / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((KEY_SIZE / 8) * 2)).ToArray()
Dim oCipherTextBytes = CipherTextBytesWithSaltAndIv.Skip((KEY_SIZE / 8) * 2).Take(CipherTextBytesWithSaltAndIv.Length - ((KEY_SIZE / 8) * 2)).ToArray()
Using oPassword = New Rfc2898DeriveBytes(_password, oSaltStringBytes, DERIVATION_ITERATIONS)
Dim oKeyBytes = oPassword.GetBytes(KEY_SIZE / 8)