As described in the previous answer, a symmetric algorithm (where a single secret key is used to encrypt and decrypt) could work. I happen to have on hand a usage of the DES algorithm. This encrypt routine returns the output of the encrypting process (and the decrypt has as input) a base64 encoded string rather than a byte array (which is the 'natural' output of the framework encryption classes).
Private key() As Byte = {}
Private IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Private Const EncryptionKey As String = "abcdefgh"
Public Function Decrypt(ByVal stringToDecrypt As String) As String
Try
Dim inputByteArray(stringToDecrypt.Length) As Byte
key = System.Text.Encoding.UTF8.GetBytes(Left(EncryptionKey, 8))
Dim des As New DESCryptoServiceProvider
inputByteArray = Convert.FromBase64String(stringToDecrypt)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch ex As Exception
'oops - add your exception logic
End Try
End Function
Public Function Encrypt(ByVal stringToEncrypt As String) As String
Try
key = System.Text.Encoding.UTF8.GetBytes(Left(EncryptionKey, 8))
Dim des As New DESCryptoServiceProvider
Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(stringToEncrypt)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
Catch ex As Exception
'oops - add your exception logic
End Try
End Function
Edited to add:
Here are the Imports that I have in that module:
Imports System
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Security.Cryptography
A DES key is 56 bits in length (just short of 8 bytes or characters). In a "big picture", that's not considered very secure these days (see this Wikipedia article on key sizes), but as you described 'secure-ish', perhaps that's ok. If you do need a more secure encryption, you should investigate using one of the more secure algorithms.
The encryption key in the above routines is in the private constant EncryptionKey. Change that value to your desired key. Or you can implement your own key management (input from file, ask the user, etc).
Not sure why Left and Convert would be broke. Left is a member of Microsoft.VisualBasic.Strings and Convert is a member of System.
I highly recommend that you read the articles linked to by Remus Rusanu, as well as the further articles linked from those. They will provide you with much background on encryption in the framework classes.