Encrypt / Decrypt Date to store in registry

Hi, I need some fairly simple code to encrypt a given date, store it in the registry, then later retrieve it and decrypt it. I don't need 128 bit encryption or anything, just so it's not recognizable (or easily decrypted).

Who is Participating?
moduloConnect With a Mentor Commented:
PAQed with points refunded (250)

Community Support Moderator
Jeff CertainCommented:
In this case you could either convert the date into string reverse it and store it.

In case you want to further encrypt it you could scramble it up in different peices or alternatively get the ascii code for each individual character then add or substract a certain digit to it and reconvert it into a character, join it up and store it into the registry.

Since you will be writing to the registry I presume that you would know quite a bit of coding and therefore I am not putting the code here. In case you do require it do let me know
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Barb4253Author Commented:
Thank you both. Chaosian, I also saw that code... but simple is key! JasonRodrigues, That would be simple, however, I thought there was a simple encryption/decryption function within vb.net, no???

Thanks again,
Jeff CertainCommented:
There's a simple "encryption" function for passwords -- HashPasswordForStoringInConfigFile. This might be what you're thinking of. The problem is that hashing is a one-way process. You can't get the original date back -- you'd have to hash the new date and compare the hashes.

You could do something simple like casting the date to a single -- this would make it a decimal number. Then, if you wanted to take it a step further, you could perform some operation on it -- square the decimal portion, multiply the whole thing by a constant, etc, etc.
Jeff CertainCommented:
Still intereseted in helping resolve this question.
Barb4253Author Commented:
I ended up using a TripleDES class:

Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Friend Class cTripleDES

    Private m_des As New TripleDESCryptoServiceProvider

    ' define the string handler
    Private m_utf8 As New UTF8Encoding

    ' define the local property arrays
    Private m_key() As Byte
    Private m_iv() As Byte

    Public Sub New(ByVal key() As Byte, ByVal iv() As Byte)
        Me.m_key = key
        Me.m_iv = iv
    End Sub

    Public Function Encrypt(ByVal input() As Byte) As Byte()
        Return Transform(input, m_des.CreateEncryptor(m_key, m_iv))
    End Function

    Public Function Decrypt(ByVal input() As Byte) As Byte()
        Return Transform(input, m_des.CreateDecryptor(m_key, m_iv))
    End Function

    Public Function Encrypt(ByVal text As String) As String
        Dim input() As Byte = m_utf8.GetBytes(text)
        Dim output() As Byte = Transform(input, m_des.CreateEncryptor(m_key, m_iv))
        Return Convert.ToBase64String(output)
    End Function

    Public Function Decrypt(ByVal text As String) As String
        Dim input() As Byte = Convert.FromBase64String(text)
        Dim output() As Byte = Transform(input, m_des.CreateDecryptor(m_key, m_iv))
        Return m_utf8.GetString(output)
    End Function

    Private Function Transform(ByVal input() As Byte, _
        ByVal CryptoTransform As ICryptoTransform) As Byte()
        ' create the necessary streams
        Dim memStream As MemoryStream = New MemoryStream
        Dim cryptStream As CryptoStream = New _
            CryptoStream(memStream, CryptoTransform, _
        ' transform the bytes as requested
        cryptStream.Write(input, 0, input.Length)
        ' Read the memory stream and convert it back into byte array
        memStream.Position = 0
        Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
        memStream.Read(result, 0, CType(result.Length, System.Int32))
        ' close and release the streams
        ' hand back the encrypted buffer
        Return result
    End Function

End Class
Jeff CertainCommented:
Glad to see you got it working...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.