Encrypt / Decrypt Date to store in registry

Posted on 2004-11-22
Last Modified: 2010-05-18
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).

Question by:Barb4253
    LVL 24

    Expert Comment

    by:Jeff Certain
    LVL 1

    Expert Comment

    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

    Author Comment

    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, no???

    Thanks again,
    LVL 24

    Expert Comment

    by:Jeff Certain
    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.
    LVL 24

    Expert Comment

    by:Jeff Certain
    Still intereseted in helping resolve this question.

    Author Comment

    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
    LVL 24

    Expert Comment

    by:Jeff Certain
    Glad to see you got it working...

    Accepted Solution

    PAQed with points refunded (250)

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now