0

I try to come up with a way to encrypt a DateTime object to string object and decrypt vice-versa.

public string Encrypt(DateTime dt)
{
  string Encrypt = "";
  {
     //Encryption code here
  }

  return Encrypt;
}

public string Decrypt(string Input)
{
  DateTime Decrypt = new DateTime();
  {
     //Decryption code here
  }

  return Decrypt;
}
Kevin Brechbühl
  • 4,717
  • 3
  • 24
  • 47
SE_VS_NET
  • 121
  • 1
  • 3
  • 11
  • 2
    possible duplicate of [Convert date time to string and back to date time](http://stackoverflow.com/questions/16580673/convert-date-time-to-string-and-back-to-date-time) – Kevin Brechbühl Jan 27 '14 at 17:02
  • 2
    No it's not, the one above is just about converting to a string. By saying Encrypt/Decrypt, I mean, client won't be able to know what DateTime value does this string represent. – SE_VS_NET Jan 27 '14 at 17:04
  • It'll still be a duplicate of something. – Magus Jan 27 '14 at 17:04

1 Answers1

2

Depends on what kind of encryption you want to perform. .NET has some built-in classes to assist, you just need to decide what algorithm you want to use. AES is great:

Using AES encryption in C#

Now the question is, how do you get the DateTime into a byte[] so you can encrypt it? There's a few ways. You could convert it to a string and encrypt the string, or you can use the .ToBinary method to get the ticks. That method is made for serializing DateTimes.

Edit: that first link shows using the Rijndael classes, but there is a set of AES classes as well:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged(v=vs.110).aspx

Community
  • 1
  • 1
Curtis Rutland
  • 776
  • 4
  • 12