解码十六进制方法在设备上找不到,但在模拟器上工作



我正在研究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 上运行
  • 我的设备在牛轧糖和棉花糖上运行

一些Android版本包含旧版本的Apache commons-codec库(1.3(,其中decodeHex(String)方法尚不存在。请尝试改为拨打decodeHex(char[])。即像这样修改你的代码:

byte[] decryptedBytes = cipher.doFinal(Hex.decodeHex(message.toCharArray()));

这应该适用于commons-codec v1.3。

最新更新