0

I have a file I want to encrypt and decrypt based on a date. When writing it, I want to use the current date, but not the time. What would I use for this?

Then, whenever I want to decrypt it, I can just use File.lastModified to get the long date. From here, how do I convert this into only the date (and not the time) it was written (or last modified, which should be the same)?

I would prefer to avoid extra libraries, and if it's overly complex, that's fine because I am encrypting data and complex code would help throw off snoopers. Most of all, it needs to be able to read the date across all locales and platforms in the same manner, so as to not throw off the encryption.

EDIT: URL problem solved. Java wanted a / in between .net and ? in the following:

"http://login.minecraft.net/?user=" + username + "&password=" + password + "&version=99"

Now my encryption class is flipping out over something...

Nifty255
  • 397
  • 2
  • 3
  • 14
  • Not much at the moment, I'm a bit stuck on how to cut time from Java's date. Right now, the reader is using the raw long to decrypt, but that has time in it, and I understand that there will very likely be a difference in milliseconds between when my code gets the date and when the file is actually created (modified). That's why I only want the date. – Nifty255 Dec 12 '12 at 13:03
  • Left out that it does not make sense to use the last modified date as a key to encrypt a file (the key is known to anyone wanting to access the data and lost, just if you make a copy of the file), why do you think using the date instead of date+time solves your problem that the encryption happens some time before the actual file modification? What if the file is encrypted just before midnight and not modified until after midnight? – jarnbjo Dec 12 '12 at 13:59
  • First off, it won't be known to my program's users what the key is, and it is likely that if they need my program, they won't have any knowledge of encryption anyway, and it's much more secure than hard coding a key. Second, just how often do you think someone will want their data saved at exactly 11:59:59 PM? The chances are exponential! Should I make it 31 times more difficult to produce this effect by only including the year and month in the key? – Nifty255 Dec 12 '12 at 14:04

3 Answers3

2

Simple suggestion: first convert the date to a yyyy-mm-dd string. Second hash the string along with any other relevant data you need to make your key. See Convert timestamp long to normal date format for long/date to string conversion.

Community
  • 1
  • 1
rossum
  • 15,344
  • 1
  • 24
  • 38
  • Ok, I am currently using Date's depreciated methods to get a string in the form of mm-dd-yyyy. I'll see if this works. – Nifty255 Dec 12 '12 at 13:14
  • Problem solved! All I had to do was figure out how the heck to encrypt in the first place, since it's vastly different from C# .NET... Regardless, your idea did indeed help. User data now successfully encrypts and decrypts using the file's date. – Nifty255 Dec 12 '12 at 18:34
1
GregorianCalendar c = new GregorianCalendar(time);
c.set(GregorianCalendar.HOUR, 0);
c.set(GregorianCalendar.HOUR_OF_DAY, 0);
c.set(GregorianCalendar.MINUTE, 0);
c.set(GregorianCalendar.SECOND, 0);
c.set(GregorianCalendar.MILLISECOND, 0);
c.getTimeInMillis();
Jeff Miller
  • 1,424
  • 1
  • 10
  • 19
  • Good idea, includes the time, but alwalys at the same hour, minute, second etc. – Maarten Bodewes Dec 12 '12 at 19:16
  • Due to an overwhelming amount of warnings over date's methods' depreciation, I will instead be also using this answer in combination with the accepted answer. I would vote up, but I have too little reputation. – Nifty255 Dec 13 '12 at 09:45
-1

The long time value is defined as:

A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT. 

So to round down to the nearest day you need something like:

public static final long OneDay = 1000l * 60l * 60l * 24l;

long today = (System.currentTimeMillis() / OneDay) * OneDay;
Date lastMidnight = new Date(today);

But please do not forget potential time zone issues.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • One day is not always `1000l * 60l * 60l * 24l` ms. – jarnbjo Dec 12 '12 at 13:56
  • In addition, there are more efficient ways of doing this. I know I said complex code is okay, but only if it is necessary. – Nifty255 Dec 12 '12 at 14:51
  • @Nifty255 - I divide by a `long` and then multiply by a `long`. How much more efficient do you need? Other options involve using a `Calendar` object and a date formatter. – OldCurmudgeon Dec 12 '12 at 15:46
  • It's a matter of getting that dd-mm-yyyy format. If it takes a Date object, sure, but if I use this, that I'm for sure going to need to comment all the steps so I don't lose myself later, and commenting a piece of code that deals with encryption kinda takes away from the security in my opinion. – Nifty255 Dec 12 '12 at 16:57
  • @Nifty255 Commenting in code takes away from the encryption...what??? That is one of the most daft things I've ever heard. – Maarten Bodewes Dec 12 '12 at 19:18