|
-
Jun 30th, 2005, 09:52 AM
#1
Thread Starter
Junior Member
VB.NET - Symmetric Encryption/Decryption (Not using files)
Half of the code online that I could find was useless. All of the msdn examples read in from a file, and then output it all to a file... and all of the 'tutorials' that I've found online have serious issues that are always left unanswered. It should also be noted that this code was made in Beta 2.0 of the .NET framework, but it works in 1.1.
First of all, this is the interface that the encryption classes will follow. This allows us to easily create an abstract factory where we can add new encryption algorithms, and create them without changing much of the existing code.
Code:
Public Interface ICrypto
Function BlockSize() As Integer
Function KeySize() As Integer
Function Encrypt(ByVal data As String) As String
Function Decrypt(ByVal data As String) As String
End Interface
The abstract factory that will create whatever cryptographic object we have available. Just pass in the name of the encryption that you want to use, and it gives you back the object. (More on this in a second)
Code:
Public Class CryptoFactory
Public Function MakeCryptographer(ByVal type As String) As ICrypto
Select Case type.ToLower
Case "des"
Return New DES()
Case "tripledes"
Return New TripleDES()
Case "rijndael"
Return New Rijndael()
Case Else
Return New TripleDES()
End Select
End Function
End Class
Here are the goods. This will perform the encryption and decryption. One thing that I do want to mention here is that there is no way to adjust the key or iv at runtime using my code, you will have to modify it yourself. They must be adjusted at design time and cannot be changed once the program is implemented. This is due to the environment that I am going to be implementing the software in. The key and block(iv) size are given in bits... This was because different algorithms have different key/iv sizes. Remember this if you decide to create more encryption/decryption algorithms (as I ran into problems, easy to fix, but problems none-the-less).
Note 1: If you don't want to use the factory at all, you can just copy/paste the encrypt/decrypt functions.
Note 2: In the interest of space, the other algorithms aren't placed here, but they are practically the exact same as this, but using the different .NET objects.
Code:
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Public Class Rijndael
Implements ICrypto
' The key and initialization vector : change them for your application
Private _key() As Byte = {132, 42, 53, 124, 75, 56, 87, 38, 9, 10, 161, 132, 183, _
91, 105, 16, 117, 218, 149, 230, 221, 212, 235, 64}
Private _iv() As Byte = {83, 71, 26, 58, 54, 35, 22, 11, 83, 71, 26, 58, 54, 35, 22, 11}
' returns the default size, in bits of the iv
Public Function BlockSize() As Integer Implements ICrypto.BlockSize
Dim aes As New RijndaelManaged()
Return aes.BlockSize
End Function
' returns the default size, in bits of the key
Public Function KeySize() As Integer Implements ICrypto.KeySize
Dim aes As New RijndaelManaged()
Return aes.KeySize
End Function
' decrypts a string that was encrypted using the Encrypt method
Public Function Decrypt(ByVal data As String) As String Implements ICrypto.Decrypt
Try
Dim inBytes() As Byte = Convert.FromBase64String(data)
Dim mStream As New MemoryStream(inBytes, 0, inBytes.Length) ' instead of writing the decrypted text
Dim aes As New RijndaelManaged()
Dim cs As New CryptoStream(mStream, aes.CreateDecryptor(_key, _iv), CryptoStreamMode.Read)
Dim sr As New StreamReader(cs)
Return sr.ReadToEnd()
Catch ex As Exception
Throw ex
End Try
End Function
' Encrypts a given string
Public Function Encrypt(ByVal data As String) As String Implements ICrypto.Encrypt
Try
Dim utf8 As New UTF8Encoding
Dim inBytes() As Byte = utf8.GetBytes(data) ' ascii encoding uses 7
'bytes for characters whereas the encryption uses 8 bytes, thus we use utf8
Dim ms As New MemoryStream() 'instead of writing the encrypted
'string to a filestream, I will use a memorystream
Dim aes As New RijndaelManaged()
Dim cs As New CryptoStream(ms, aes.CreateEncryptor(_key, _iv), CryptoStreamMode.Write)
cs.Write(inBytes, 0, inBytes.Length) ' encrypt
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
Catch ex As Exception
Throw ex
End Try
End Function
End Class
Here's the implementation. The first call creates the factor, the second creats the encryption/decryption object and passes the name of the algorithm given from a combobox. The rest is pretty self-explanatory.
Code:
Try
Dim CryptographyFactory As New CryptoFactory()
Dim Cryptographer As ICrypto = CryptographyFactory.MakeCryptographer(cboEncryption.SelectedItem)
Dim etext As String = Cryptographer.Encrypt(txtInput.Text.ToString)
txtEOutput.Text = etext
Dim dtext As String = Cryptographer.Decrypt(etext)
txtDOutput.Text = dtext
Catch ex As Exception
MessageBox.Show(ex.Message, "Encryption Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Hope it clears up any wonders about symmetric encryption/decryption using .NET.
Last edited by dday9; Oct 7th, 2022 at 01:08 PM.
Reason: Fixed the bb tags
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|