所有Android版本的安全随机数生成

所有Android版本的安全随机数生成,第1张

概述我目前正在研究涉及加密的android应用程序的想法.我打算在ctr模式下使用aes,并在带有漩涡的PBKDF2中使用键进行拉伸.我将实现一个新的充气城堡实现,而不是旧实现中内置的android.为了确保它可以在任何Android版本上正常工作.但是我在寻找一种可靠的方法来生成盐和密钥的随机数

我目前正在研究涉及加密的android应用程序的想法.我打算在ctr模式下使用aes,并在带有漩涡的PBKDF2中使用键进行拉伸.

我将实现一个新的充气城堡实现,而不是旧实现中内置的androID.为了确保它可以在任何Android版本上正常工作.

但是我在寻找一种可靠的方法来生成盐和密钥的随机数时遇到了一些问题.我读过某处文章说,在某些旧版本的androID中,androID中内置的安全随机性是不安全的,而且我还听说大多数androID手机很难在开发/随机性和块中保持较高的熵.难道这对dev / urandom的安全性没有重大影响吗?
因此,我正在寻找使用手机上的传感器收集更多熵的好方法.

解决方法:

以下类应帮助您减轻AndroID SecureRandom类的问题.该代码不是文本,而是创建的,因为细节不多.

/** * A strengthener that can be used to generate and re-seed random number * generators that do not seed themselves appropriately. *  * @author owlstead */public class SecureRandomStrengthener {    private static final String DEFAulT_PSEUDO_RANDOM_NUMBER_GENERATOR = "SHA1PRNG";    private static final EntropySource TIME_ENTROPY_SOURCE = new EntropySource() {        final ByteBuffer timeBuffer = ByteBuffer.allocate(Long.SIZE / Byte.SIZE                * 2);        @OverrIDe        public ByteBuffer provIDeEntropy() {            this.timeBuffer.clear();            this.timeBuffer.putLong(System.currentTimeMillis());            this.timeBuffer.putLong(System.nanoTime());            this.timeBuffer.flip();            return this.timeBuffer;        }    };    private final String algorithm;    private final List<EntropySource> entropySources = new linkedList<EntropySource>();    private final MessageDigest digest;    private final ByteBuffer seedBuffer;    /**     * Generates an instance of a {@link SecureRandomStrengthener} that     * generates and re-seeds instances of {@code "SHA1PRNG"}.     *      * @return the strengthener, never null     */    public static SecureRandomStrengthener getInstance() {        return new SecureRandomStrengthener(                DEFAulT_PSEUDO_RANDOM_NUMBER_GENERATOR);    }    /**     * Generates an instance of a {@link SecureRandomStrengthener} that     * generates instances of the given argument. Note that the availability of     * the given algorithm arguments in not tested until generation.     *      * @param algorithm     *            the algorithm indicating the {@link SecureRandom} instance to     *            use     * @return the strengthener, never null     */    public static SecureRandomStrengthener getInstance(final String algorithm) {        return new SecureRandomStrengthener(algorithm);    }    private SecureRandomStrengthener(final String algorithm) {        if (algorithm == null || algorithm.length() == 0) {            throw new IllegalArgumentException(                    "Please provIDe a PRNG algorithm string such as SHA1PRNG");        }        this.algorithm = algorithm;        try {            this.digest = MessageDigest.getInstance("SHA1");        } catch (final NoSuchAlgorithmException e) {            throw new IllegalStateException(                    "MessageDigest to create seed not available", e);        }        this.seedBuffer = ByteBuffer.allocate(this.digest.getDigestLength());    }    /**     * Add an entropy source, which will be called for each generation and     * re-seeding of the given random number generator.     *      * @param source     *            the source of entropy     */    public voID addEntropySource(final EntropySource source) {        if (source == null) {            throw new IllegalArgumentException(                    "EntropySource should not be null");        }        this.entropySources.add(source);    }    /**     * Generates and seeds a random number generator of the configured     * algorithm. Calls the {@link EntropySource#provIDeEntropy()} method of all     * added sources of entropy.     *      * @return the random number generator     */    public SecureRandom generateAndSeedRandomNumberGenerator() {        final SecureRandom secureRandom;        try {            secureRandom = SecureRandom.getInstance(this.algorithm);        } catch (final NoSuchAlgorithmException e) {            throw new IllegalStateException("PRNG is not available", e);        }        reseed(secureRandom);        return secureRandom;    }    /**     * Re-seeds the random number generator. Calls the     * {@link EntropySource#provIDeEntropy()} method of all added sources of     * entropy.     *      * @param secureRandom     *            the random number generator to re-seed     */    public voID reseed(final SecureRandom secureRandom) {        this.seedBuffer.clear();        secureRandom.nextBytes(this.seedBuffer.array());        for (final EntropySource source : this.entropySources) {            final ByteBuffer entropy = source.provIDeEntropy();            if (entropy == null) {                continue;            }            final ByteBuffer wipeBuffer = entropy.duplicate();            this.digest.update(entropy);            wipe(wipeBuffer);        }        this.digest.update(TIME_ENTROPY_SOURCE.provIDeEntropy());        this.digest.update(this.seedBuffer);        this.seedBuffer.clear();        // remove data from seedBuffer so it won't be retrIEvable        // reuse        try {            this.digest.digest(this.seedBuffer.array(), 0,                    this.seedBuffer.capacity());        } catch (final DigestException e) {            throw new IllegalStateException(                    "DigestException should not be thrown", e);        }        secureRandom.setSeed(this.seedBuffer.array());        wipe(this.seedBuffer);    }    private voID wipe(final ByteBuffer buf) {        while (buf.hasRemaining()) {            buf.put((byte) 0);        }    }}

这是EntropySource的小接口:

/** * A simple interface that can be used to retrIEve entropy from any source. *  * @author owlstead */public interface EntropySource {    /**     * RetrIEves the entropy.     * The position of the ByteBuffer must be advanced to the limit by any users calling this method.     * The values of the bytes between the position and limit should be set to zero by any users calling this method.     *      * @return entropy within the position and limit of the given buffer     */    ByteBuffer provIDeEntropy();}

请注意,尚未对类的输出进行随机性测试(但这主要取决于返回的SecureRandom类,因此应该可以).

最后,由于我还没有准备好AndroID 1.6运行时,因此应该针对此版本或更低版本进行兼容性(!)测试.

总结

以上是内存溢出为你收集整理的所有Android版本的安全随机数生成全部内容,希望文章能够帮你解决所有Android版本的安全随机数生成所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1076543.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存