我正在进行RSA加密的项目,需要使用Apache commons-codec中的方法,即:
- Hex.encodeHex(byte[])
- Hex.decodeHex(String)
这两种方法在 Android 模拟器上都可以正常工作,但在设备上会返回 NoSuchMethodError
public String RSADecrypt(final String message) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, getPrivateKey());
byte[] decryptedBytes = cipher.doFinal(Hex.decodeHex(message));
return new String(decryptedBytes);
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (DecoderException e) {
e.printStackTrace();
}
return "";
}
java.lang.NoSuchMethodError: No static method decodeHex(Ljava/lang/String;)[B in class Lorg/apache/commons/codec/binary/Hex; or its super classes (declaration of 'org.apache.commons.codec.binary.Hex' appears in /system/framework/org.apache.http.legacy.boot.jar)
- 我的模拟器在 Pie、Oreo 和 Nougat 上运行
- 我的设备在 Nougat & Marshmallow 上运行
最佳答案
某些 Android 版本包含旧版本的 Apache commons-codec 库 (1.3),其中尚不存在 decodeHex(String)
方法。尝试改为调用 decodeHex(char[])
。 IE。像这样修改你的代码:
byte[] decryptedBytes = cipher.doFinal(Hex.decodeHex(message.toCharArray()));
这应该适用于 commons-codec v1.3。
关于android - 在设备上找不到decodeHex方法,但在模拟器上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56556627/