问题
Currently, I'm looking for a way to increase the quality of randomness in my Android
application (a card game). Previously, it was estimated that for my situation (52! permutation) at least 226 bits of entropy (226 random bits) are needed..
I'm planning to use this byte[]
as a seed for SecureRandom
:
SecureRandom random = new SecureRandom();
random.setSeed(/* insert seed here, byte[] */)
The question is -- Where can I reliably get random bits in this amount (at least 226 bits) on Android
, preferably without requiring any permissions and without internet. Also, it should work regardless of device and API level.
回答1:
On Java 8+ you can use
SecureRandom rand = SecureRandom.getInstanceStrong();
To get the strongest randomness available on your platform. To be explicit you can use
SecureRandom rand = SecureRandom.getInstance("NativePRNGBlocking");
which use the entropy of /dev/random
on Linux like systems. However, I expect it will fail if not available.
https://www.synopsys.com/blogs/software-security/proper-use-of-javas-securerandom/
Alternatively
You could create randomness based on the user's input by taking a SHA256 or higher of the System.nanoTime()
of previous events.
回答2:
Without being able to access the mic (which would require permissions) or grab bytes from random.org
(which would require internet), the only thing that I can think of is the user him/herself: Present a blank square the user moves their finger across, instructing them to do it as randomly as possible, ideally for several seconds, and use that touch data. (I seem to recall an app I used to use — TrueCrypt? — did this.) You might even throw some pseudo-randomness on top of their human-randomness to try to avoid people gaming the system with extraordinarily precise repeatable movements.
If you relax your requirements a bit, you can probably get some quite good entropy from the mic (ambient noise) and/or accelerometer. And of course, if you request network access, you can download truly random data from http://random.org.
来源:https://stackoverflow.com/questions/51787428/where-can-i-get-a-reliable-source-of-entropy-real-randomness-byte