每个人,我最近在PC和Android上实施了RSA加密。有些在 PC(jdk1.6) 上运行良好,但是当我尝试Android时( Android 2.1 with jdk1.5 ),奇怪的错误发生。
代码如下:
BigInteger m = m;
BigInteger e = e;
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
KeyFactory fact = KeyFactory.getInstance("RSA");
try{
PublicKey pubKey = fact.generatePublic(keySpec);
return pubKey;
} catch (Exception e) {
throw new RuntimeException("Spurious serialisation error", e);
}
return null;
问题是 PublicKey pubKey = fact.generatePublic(keySpec); 似乎会导致异常,但我无法抓住它,甚至将其修改为 Throwable e 。它只是跳转到返回null; 但实际上返回值不为null,它是一个非法的Pubkey,具有正常指数(65537)和一个坏模数(填充一个字母,'ae3432a ** *'。
有人可以为我测试吗?
答案 0 :(得分:0)
我们在Android 2.3.6 App中使用RSA-Publickey。我们使用以下代码生成密钥:
// data[] is pre-filled with modulus and publicExponent
String ENCRYPTION_ALGORITHM = "RSA";
BigInteger modulus = (BigInteger) data[0];
BigInteger publicExponent = (BigInteger) data[1];
PublicKey publicKey = getKeyFactory().generatePublic(new RSAPublicKeySpec(modulus, publicExponent));
return publicKey;
private static KeyFactory getKeyFactory() {
if (keyFactory == null) {
try {
keyFactory = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
// Algorithm is part of every Android installation. Since we do not get here under realistic
// circumstances it is OK to crash here.
throw new HellFrozeOverException();
}
}
return keyFactory;
}
如果我看对了,你也会这样做。对不起,我帮不了你......