我一直在使用下面提供的解决方案来使用 Flexiprovider 加密/解密一段时间。该解决方案适用于 Galaxy s5 和 s6。但是,在 s9 中它不起作用,因为它与 BouncyCastle 冲突。在 s5 和 s6 中工作的代码:
KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECIES", "FlexiEC");
CurveParams ecParams = new CurveRegistry.BrainpoolP512r1();
kpg.initialize(ecParams, new SecureRandom());
KeyPair keyPair = kpg.generateKeyPair();
PublicKey pubKey = keyPair.getPublic();
PrivateKey privKey = keyPair.getPrivate();
String STRpubKey = Base64.encodeToString(pubKey.getEncoded(),
Base64.DEFAULT);
byte[] decodedPublicKey = Base64.decode(STRpubKey, Base64.DEFAULT);
X509EncodedKeySpec X509spec = new X509EncodedKeySpec(decodedPublicKey);
KeyFactory keyFactory = KeyFactory.getInstance("ECIES","FlexiEC");
PublicKey pubkey = keyFactory.generatePublic(X509spec);
上面的代码在 s9 中给出了以下错误:
java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.org.bouncycastle.math.ec.ECCurve com.android.org.bouncycastle.asn1.x9.X9ECParameters.getCurve()' on a null object reference
异常发生在这里:
PublicKey pubkey = keyFactory.generatePublic(X509spec);