1

I want to generate a serial number for an application (to be used just in a period of time), this serial number based on the time, so it's will be unnecessary code if change the clock time in my computer so am asking you if is there any possibility to get the real DateTime even if i changes time on my computer using C# ?

(I assume that the user does not have an internet connection)

AHmedRef
  • 2,555
  • 12
  • 43
  • 75

2 Answers2

8

am asking you if is there any possibility to get the real DateTime even if i changes time on my computer using C# ?

(I assume that the user does not have an internet connection)

No. The system clock is the local source of time information. If you can't go to a reliable external source (due to a lack of internet connection) there's nothing else you can rely on.

I suggest you use an internet connection to check the time if there is one, otherwise fall back on the system clock, possibly detect time having been reversed significantly (if at one point you "see" the time as April 20th, and then at another you "see" the time as April 15th, you can assume something odd is going on) and accept that a sufficiently motivated user will bypass the check.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

What people do: Record the time (of local clock) when your program was executed somewhere in the registry (or another hard to guess place).

If your program runs again and finds local time being before this recorded timestamp it refuses to start.

This is a method to prevent users from manipulating the clock in order to reactvate your license.

You'll have to consider chages in daylight saving time, so you should allow one hour of "time jumps"

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • 1
    This would only be the case if you stored the timestamp in plain text. If it were encrypted, changing the timestamp would be much more difficult. – Cameron Tinker Apr 24 '15 at 11:35
  • 1
    The user can even delete this timestamp from wherever he finds it. Thats why I mentioned a "hard to find place" – DrKoch Apr 24 '15 at 11:37
  • if he delete it from wherever that's means that application cannot find a correct datestamp – AHmedRef Apr 24 '15 at 11:40
  • Yes, in that case, you'd prompt the user that the license is no longer valid and will need to be renewed to continue using the application. – Cameron Tinker Apr 24 '15 at 11:41
  • @ahmed Well, like if it runs for the first time. Probably you write a second entry somewhere to find ot if your program runs not for the first time, and so on... – DrKoch Apr 24 '15 at 11:41
  • but at the first time im gonna put a default datestamp (crypted + incorrect value), so he will have to insert the right one – AHmedRef Apr 24 '15 at 11:45
  • 2
    I think it would make more sense to record the UTC time *obtained* locally - then you don't need to worry about DST. – Jon Skeet Apr 24 '15 at 12:20
  • @ahmet you can't distiguish between "first start" and "user has deleted my secret info" – DrKoch Apr 24 '15 at 12:23