Link to home
Start Free TrialLog in
Avatar of Barb4253
Barb4253

asked on

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).

Thanks
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Avatar of JasonRodrigues
JasonRodrigues

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
Avatar of Barb4253

ASKER

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,
Barb
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.
Still intereseted in helping resolve this question.
Thanks.
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))
        Console.WriteLine(m_des.KeySize.ToString)
        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, _
            CryptoStreamMode.Write)
        ' transform the bytes as requested
        cryptStream.Write(input, 0, input.Length)
        cryptStream.FlushFinalBlock()
        ' 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
        memStream.Close()
        cryptStream.Close()
        ' hand back the encrypted buffer
        Return result
    End Function

End Class
Glad to see you got it working...
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial