示例A(使用org.apache.commons.codec.binary.Base64):
Base64.encodeBase64("foobar".getBytes());
Run Code Online (Sandbox Code Playgroud)
示例B(使用android.util.Base64):
Base64.encode("foobar".getBytes(), Base64.DEFAULT);
Run Code Online (Sandbox Code Playgroud)
这些产生相同的字符串吗?
我按照这里的说明在eclipse中为我的Android应用程序添加了来自apache.org(commons-codec-1.4.jar)的公共编解码器.代码中没有错误.但是当我运行应用程序并调用使用编解码器的函数时,应用程序停止并需要关闭.
在logCat中说:
Android Runtime:java.lang.NoSuchMethodError:org.apache.commons.codec.binary.Base64.encodeBase64String
代码行是:String tmpStr = Base64.encodeBase64String(msg); // msg是一个byte []
该应用程序适用于最低SDK版本= 7(Android 2.1),因此我无法使用Android Base64
不知道怎样才能解决问题?
干草,我如何将org.apache.commons包导入android,以便我可以在我的应用程序中使用它们?
谢谢
我正在使用此方法来解密收到的消息:
private static String decrypt(String key, String initVector, String dataToDecrypt) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
String safeString = dataToDecrypt.replace('-', '+').replace('_', '/');
byte[] decodedString = Base64.decodeBase64(safeString);
byte[] original = cipher.doFinal(decodedString);
return new String(original);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
但是,我的Android应用程序崩溃了,显示以下异常:
java.lang.NoSuchMethodError:没有静态方法decodeBase64(Ljava/lang/String;)[B in class Lorg/apache/commons/codec/binary/Base64; 或其超类(“org.apache.commons.codec.binary.Base64”的声明出现在 /system/framework/ext.jar 中)
因此,该方法decodeBase64需要base64string,但我通过了string。我的问题来了:
如何转换String为base64string?! …