locked
encrypt date using AES RRS feed

  • Question

  • I want to encrypt a date value for storing in the settings file. I then want to decrypt it back to a date value using AES in vb.net 2013

    Are there any pitfalls to be aware of?
    ie should I convert to a string before encryption, handling the conversion etc to ensure the data remains valid.

    Wednesday, December 2, 2015 8:55 AM

Answers

  •         
            Dim Dte As Date = Now
            Dim MyDate As String = Dte.Ticks.ToString
    
            'Encript MyDate
    
            '...
    
            'Decript it
    
            Dte = Date.FromBinary(CLng(MyDate))
    


    Luc

    • Marked as answer by AndyNakamura Thursday, December 3, 2015 11:56 AM
    Wednesday, December 2, 2015 12:13 PM

All replies

  •         
            Dim Dte As Date = Now
            Dim MyDate As String = Dte.Ticks.ToString
    
            'Encript MyDate
    
            '...
    
            'Decript it
    
            Dte = Date.FromBinary(CLng(MyDate))
    


    Luc

    • Marked as answer by AndyNakamura Thursday, December 3, 2015 11:56 AM
    Wednesday, December 2, 2015 12:13 PM
  • I've used AES to encrypt and decrypt data. Data to encrypt is a byte array. The encrypted output is also a sequence of bytes. Therefore you would need to save a sequence of bytes. I suppose a byte sequence may be able to be saved to a string and the string converted back to bytes for doing this but am not positive.

    Date will need to be converted to byte array. That probably means simplest method would be to convert Date to string of some type such that it can be parsed back into Date from the string representation. I believe you could use System.Text.AsciiEncoding.GetBytes(String value) to array of bytes which would then be encrypted then decrypted and System.Text.AsciiEncoding.GetString(Byte sequence) to string to parse for Date.

    See System.Text Namespace and below

    ' Understanding Machine-Level and User-Level RSA Key Containers
    ' https://msdn.microsoft.com/en-us/library/f5cs0acs(v=vs.140).aspx
    '
    ' RSAProtectedConfigurationProvider Class
    ' https://msdn.microsoft.com/en-us/library/system.configuration.rsaprotectedconfigurationprovider(v=vs.140).aspx
    '
    ' How to: Store Asymmetric Keys in a Key Container
    ' https://msdn.microsoft.com/en-us/library/tswxhw92(v=vs.100).aspx
    '
    ' Rijndael Class - Rijndael symmetric encryption algorithm
    ' https://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndael(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
    '
    ' Walkthrough: Creating a Cryptographic Application
    ' https://msdn.microsoft.com/en-us/library/bb397867(v=vs.110).aspx

    Update: Below is a link to an RSA encryption/decryption project in a zip folder on my onedrive. It uses a users RSA rather than all users or system perhaps RSA. Anyhow it has a bunch of capabilities in it but was designed for compressing/encrypting folders/files/subfolders and their files also inserting password and a term used to help remember password in the encrypted data such that even with RSA key it will not decrypt data back to folders/files/subfolders and files without the correct password. You may also want to see other capabilities used with it though. It has a help text file with it you can read also.

    https://onedrive.live.com/redir?resid=B8AB225FA76F270D!238&authkey=!AMv9942a1rZFTxU&ithint=file%2czip


    La vida loca

    Wednesday, December 2, 2015 12:20 PM
  • Hi Pennie,
    Can you explain why you are using .ticks rather than just :

    Dim MyDate as String = Dte

    I guess it is because of the resolution

    I have noticed that if I just simply convert to a string, then encrypt, then decrypt, then convert back to a date value without using ticks and  then do a datetime.compare they are not equal.

    I'm just not sure why

    Thursday, December 3, 2015 9:12 AM
  • Hi Pennie,
    Can you explain why you are using .ticks rather than just :

    Dim MyDate as String = Dte

    I guess it is because of the resolution

    I have noticed that if I just simply convert to a string, then encrypt, then decrypt, then convert back to a date value without using ticks and  then do a datetime.compare they are not equal.

    I'm just not sure why

    Using ticks allows to keep the fractions of seconds. ... Which are lost when using Date.ToString.

    If your date is not a time stamp or something like that where precision matters, you may very well encrypt the date as string instead of the ticks.

    And if you don't save the ticks, because of the precision lost, you dates will not be equals after decryption.


    Luc



    • Edited by Crazypennie Thursday, December 3, 2015 10:54 AM
    Thursday, December 3, 2015 10:51 AM
  • Thanks, I get it now.

    Whilst ever the value remains as a date it includes the ticks

    Cheers

    Thursday, December 3, 2015 11:56 AM
  • Thanks, I get it now.

    Whilst ever the value remains as a date it includes the ticks

    Cheers

    Andy,

    The tick is the value as an Int64 which represent the imaginary ticks in 100th nanoseconds from 1/1/1 00:00:00 according the Gregorian calendar.

    Imaginary because before around 1500 the Gregorian calendar was not used while the earlier used Julian Calendar starts at the birth of Jesus Christs. Nobody knows the second when he was born exact. 

    All what is shown as date is calculated from that tick (the properties are in fact methods).


    Success
    Cor

    Thursday, December 3, 2015 12:01 PM

  • Imaginary because before around 1500 the Gregorian calendar was not used while the earlier used Julian Calendar starts at the birth of Jesus Christs. Nobody knows the second when he was born exact. 

    All what is shown as date is calculated from that tick (the properties are in fact methods).


    Success
    Cor

    Cor,

      Nobody knows the date neither, and many scholars argue about the year also 

    And there are no year 0 in the Gregorian calender  ... it went to -1 to 1 skipping 0


    Luc


    • Edited by Crazypennie Thursday, December 3, 2015 1:22 PM
    Thursday, December 3, 2015 1:19 PM

  • Imaginary because before around 1500 the Gregorian calendar was not used while the earlier used Julian Calendar starts at the birth of Jesus Christs. Nobody knows the second when he was born exact. 

    All what is shown as date is calculated from that tick (the properties are in fact methods).


    Success
    Cor

    Cor,

      Nobody knows the date neither, and many scholars argue about the year also 

    And there are no year 0 in the Gregorian calender  ... it went to -1 to 1 skipping 0


    Luc


    Luc, be aware, I've read yesterday that Frank like me don't argue about it. 

    For me it is just a point taken in history. Where 1 stands for the begin and -1 for before the begin.


    Success
    Cor




    Thursday, December 3, 2015 5:18 PM