39 lines
1.5 KiB
VB.net
39 lines
1.5 KiB
VB.net
Public Class SecurityUtils
|
|
|
|
#Region " Declarations "
|
|
Private Const ENCRYPTION_KEY_SUFFIX As String = "SDT"
|
|
Private Const KEY_SIZE_BYTES As Integer = 32 ' 256 bits for AES-256
|
|
#End Region
|
|
|
|
#Region " Methods "
|
|
|
|
Private Shared Function PadKey(key As String) As String
|
|
Dim paddedKey As String = key & ENCRYPTION_KEY_SUFFIX
|
|
If paddedKey.Length < KEY_SIZE_BYTES Then
|
|
paddedKey = paddedKey.PadRight(KEY_SIZE_BYTES, "X"c)
|
|
ElseIf paddedKey.Length > KEY_SIZE_BYTES Then
|
|
paddedKey = paddedKey.Substring(0, KEY_SIZE_BYTES)
|
|
End If
|
|
Return paddedKey
|
|
End Function
|
|
|
|
Public Shared Function EncryptValue(value As String, key As String) As String
|
|
Dim encryption As New EncryptionUtils.SymmetricEncryptor(EncryptionUtils.SymmetricEncryptor.Provider.Rijndael)
|
|
|
|
Return encryption.Encrypt(New EncryptionUtils.Data(value), New EncryptionUtils.Data(PadKey(key))).ToBase64
|
|
End Function
|
|
|
|
Public Shared Function DecryptValue(encryptedValue As String, key As String) As String
|
|
Dim encryption As New EncryptionUtils.SymmetricEncryptor(EncryptionUtils.SymmetricEncryptor.Provider.Rijndael)
|
|
|
|
' note EncryptValue returns Base64 string so we need to initialized encryptedData as Base64
|
|
Dim encryptedData As EncryptionUtils.Data = New EncryptionUtils.Data()
|
|
encryptedData.Base64 = encryptedValue
|
|
|
|
Return encryption.Decrypt(encryptedData, New EncryptionUtils.Data(PadKey(key))).Text
|
|
End Function
|
|
|
|
#End Region
|
|
|
|
End Class
|