Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class RC_2
- Private Shared RC2 As New RC2CryptoServiceProvider
- Private Shared key As Byte(), IV As Byte() ' to hold our secret key and initialization vector...
- Private Shared Function Generate_Key() As Byte() 'Function to generate a new random secret key for encryption...
- RC2.GenerateKey()
- Dim a As Byte() = RC2.Key
- Return a
- End Function
- Private Shared Function Generate_IV() As Byte() 'Function to generate a new random Initialization vector for our encryption...
- RC2.GenerateIV()
- Dim a As Byte() = RC2.IV
- Return a
- End Function
- Public Shared Function Encrypt(ByVal input As String) As Byte()
- 'Set and save our key and the vector....These generate a new key and initializaion vector every time you call them...
- key = Generate_Key()
- IV = Generate_IV()
- Dim Crypt As ICryptoTransform = RC2.CreateEncryptor(key, IV)
- Dim input_bytes As Byte() = System.Text.Encoding.Default.GetBytes(input)
- Dim mem As New MemoryStream
- Dim crypto As New CryptoStream(mem, Crypt, CryptoStreamMode.Write)
- Dim out As Byte()
- crypto.Write(input_bytes, 0, input_bytes.Length)
- crypto.FlushFinalBlock()
- out = mem.ToArray
- Return out
- End Function
- Public Shared Function Decrypt(ByVal input As String) As Byte()
- Dim Crypt As ICryptoTransform = RC2.CreateDecryptor(key, IV)
- Dim input_bytes As Byte() = System.Text.Encoding.Default.GetBytes(input)
- Dim mem As New MemoryStream(input_bytes)
- Dim crypto As New CryptoStream(mem, Crypt, CryptoStreamMode.Read)
- Dim out(input_bytes.Length - 1) As Byte
- crypto.Read(out, 0, input_bytes.Length)
- Return out
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment