Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:35:37+00:00 2026-06-17T12:35:37+00:00

I had a datetime column in a SQL Server table. We needed to encrypt

  • 0

I had a datetime column in a SQL Server table. We needed to encrypt it. So, I cast it to a varbinary in SSMS. I simply edited the table, set the datetime type to varbinary and let SQL Server convert it. Following that, I wrote throwaway C# code to pull it out, encrypt it (using the encryption algorithms in our middle layer) and push it back into the database. Did the same for some nvarchars for names and other string types.

When I pull out the encrypted data (using NHibernate), I pull the varbinary into a byte[] and decrypt it. I then try to convert it back to the original value.

The nvarchar-as-varbinary columns convert fine; for example, I get may names back.

return Encoding.Unicode.GetString(source.FirstName);

However, I’m having a hard time converting the dates back into their original form. I’m using:

long ticks = BitConverter.ToInt64(source.DateOfBirth, 0);
return new DateTime?(new DateTime(1980, 1, 1).AddMilliseconds(ticks));

This does not seem to return the date properly. What’s the correct way to cast it back to a DateTime?

Update: A sample value was requested. A datetime that was originally 1/1/1901, when decrypted, yields a byte[] where byte[2]=1 and byte[3]=109, and all others are 0. Another datetime ‘1941-04-26’ yields byte[2]=58 and byte[3]=242 upon decryption.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:35:38+00:00Added an answer on June 17, 2026 at 12:35 pm

    The date is stored as the number of days since 1900-01-01.

    Example:

    byte[] data = {0,0,58,242};
    
    if (BitConverter.IsLittleEndian) {
      Array.Reverse(data);
    }
    int days = BitConverter.ToInt32(data, 0);
    DateTime date = new DateTime(1900, 1, 1).AddDays(days);
    
    Console.WriteLine(date);
    

    Output:

    1941-04-26 00:00:00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server I have a DATETIME column which includes a time element. Example:
I had a table employeeswipe_tbl in SQL Server 2008 with these columns: create employeeswipe_tbl
I am trying to create a SQL Server 2008R2 Table column with automatically calculated
I had asked a question on stackoverflow: Large SQL Server database timing out PHP
I've got a column in my Microsoft SQL CE 3.5 SP2 database called Type
Suppose in my table I had a column defined with a default. e.g. CREATE
I had an model with an end column (datetime format), only to discover that
I had added in a new column called ForgotPasswordDate into aspnet_Membership table. When a
I have a simple function within my model to set a completed datetime column
I have a table in SQL Server 2005 which has three columns: id (int),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.