DH Keypair generation time on Android

我是研究僧i 提交于 2019-12-03 00:48:25

I did some further coding/research and apparently the call that's the most time (battery?) consuming is:

new SecureRandom()

In particular, though, since for DH the parameters (g, p, l) can be pre-computed and hard-coded it's a wise suggestion to do so beforehand and use the generated values to generate the key pair (which will be almost instantaneous).

Example code:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
keyGen.initialize(new DHParameterSpec(p, g, l));
KeyPair ackp = keyGen.generateKeyPair();

Where p, g, and l are:

final BigInteger p = new BigInteger("X");
final BigInteger g = new BigInteger("Y");
final int l = 1023;

And X and Y can be generated offline with:

AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance("DH");
paramGen.init(1024, new SecureRandom());
AlgorithmParameters params = paramGen.generateParameters();
DHParameterSpec dhSpec = (DHParameterSpec)params.getParameterSpec(DHParameterSpec.class);
System.out.println("p: " + dhSpec.getP() + "\ng: " + dhSpec.getG() + " \nl: " + dhSpec.getL());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!