简体   繁体   English

如何用org.apache.commons.codec.binary.Base64替换android.util.Base64?

[英]How to replace android.util.Base64 by org.apache.commons.codec.binary.Base64?

I'm migrating my native Android game to libGDX. 我正在将本机Android游戏迁移到libGDX。 So I can't access the Android libraries anymore and I'm trying to replace android.util.Base64 by org.apache.commons.codec.binary.Base64 . 因此,我无法再访问Android库,并且尝试用org.apache.commons.codec.binary.Base64替换android.util.Base64 (I need Base64's encodeToString and decode methods.) (我需要Base64的encodeToStringdecode方法。)

Unfortunately, with the new package I get this error: java.security.InvalidKeyException: Illegal key size (using the same 24-character-key as I did before). 不幸的是,在新软件包中,我收到以下错误消息: java.security.InvalidKeyException: Illegal key size (使用与以前相同的24个字符的密钥)。

Here at stackoverflow they say it's probably because "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 7" are missing. 他们在stackoverflow上这可能是因为缺少“ Java密码学扩展(JCE)无限强度管辖权策略文件7”。 But if I use them, the users of my app have to install them, too. 但是,如果我使用它们,我的应用程序的用户也必须安装它们。

Is there any easier solution? 有没有更简单的解决方案? Why did it work before? 为什么以前起作用?

EDIT: 编辑:

This is the code that leads to the InvalidKeyException : 这是导致InvalidKeyException的代码:

javax.crypto.Cipher writer = Cipher.getInstance("AES/CBC/PKCS5Padding");

String keyOf24Chars = "abcdefghijklmnopqrstuvwx";

IvParameterSpec ivSpec = getIv();

MessageDigest md = MessageDigest.getInstance("SHA-256");
md.reset();
byte[] keyBytes = md.digest(keyOf24Chars.getBytes("UTF-8"));

SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES/CBC/PKCS5Padding");

// secretKey.getAlgorithm(): "AES/CBC/PKCS5Padding"
// secretKey.getFormat(): "RAW"
// secretKey.getEncoded().length: 32

writer.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec); // java.security.InvalidKeyException: Illegal key size

EDIT 2: 编辑2:

As explained in Maarten Bodewes' comment, Android has it's own implementation of the java and javax classes which apparently have no problem with 32 byte keys. 正如Maarten Bodewes的评论中所解释的那样,Android拥有自己的javajavax类的实现,显然32位字节的密钥没有问题。 After I have installed the "JCE Unlimited Strength Jurisdiction Policy Files 7" we are coming to the code that uses Base64 and causes this error: java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeToString : 在安装“ JCE无限强度管辖权策略文件7”之后,我们来Base64使用Base64并导致此错误的代码: java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeToString

String valueToEncode = "xyz";
byte[] secureValue;
try {
    secureValue = writer.doFinal(valueToEncode.getBytes("UTF-8"));
} catch (Exception e) {
    throw new SecurePreferencesException(e);
}
Base64 base64 = new Base64();
String secureValueEncoded = base64.encodeToString(secureValue);

But this method does exist (in BaseNCodec which Base64 extends): 但是此方法确实存在(在Base64扩展的BaseNCodec中):

public String encodeToString(final byte[] pArray) {
    return StringUtils.newStringUtf8(encode(pArray));
}

How can I make Android use this method? 如何使Android使用此方法?

EDIT 3: 编辑3:

Finally I solved my problem by writing an interface and then using my old Android code (when compiling for Android). 最后,我编写了一个接口,然后使用旧的Android代码(针对Android进行编译)解决了我的问题。 Check this example for libGDX: Interfacing with platform specific code . 检查此示例以获取libGDX: 与平台特定代码的接口

No, there isn't an easier solution. 不,没有更简单的解决方案。 You could use 3DES instead of AES (which I presume you are using) but you would be downgrading your security, and still be incompatible with the previous code. 您可以使用3DES代替AES(我想您正在使用AES),但是您将降低安全性,并且仍然与先前的代码不兼容。 Downgrading security of AES to 128 is a better idea, but the incompatibility issue won't go away. 将AES的安全性降级为128是一个更好的主意,但是不兼容的问题不会消失。

If you are not using the encryption/decryption in a third party library (eg JSSE for SSL or XML encryption) then you could directly use the Bouncy Castle or Spongy Castle API's. 如果您不在第三方库中使用加密/解密(例如,JSSE用于SSL或XML加密),则可以直接使用Bouncy Castle或Spongy Castle API。 So that means directly using AESBlockCipher + a mode of encryption. 因此,这意味着直接使用AESBlockCipher +一种加密模式。 Bouncy Castle doesn't have these kind of limitations - they are part of the Oracle Cipher implementation. Bouncy Castle没有这些限制-它们是Oracle Cipher实现的一部分。

It was working before because Android doesn't have these kind of restrictions while Java 7/8 SE does. 它之所以能够工作是因为Android并没有这些限制,而Java 7/8 SE却没有。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#中的org.apache.commons.codec.binary.Base64 - org.apache.commons.codec.binary.Base64 in C# 如何将sun.misc.BASE64Encoder转换为org.apache.commons.codec.binary.Base64 - How to convert sun.misc.BASE64Encoder to org.apache.commons.codec.binary.Base64 如何使用org.apache.commons.codec.binary.base64对Java对象进行Base64编码? - How to Base64 encode a Java object using org.apache.commons.codec.binary.base64? Java-org.apache.commons.codec.binary.Base64消除换行符 - Java - org.apache.commons.codec.binary.Base64 eliminate new line characters 尝试创建CommonsHttpOAuthConsumer时java.lang.ClassNotFoundException:org.apache.commons.codec.binary.Base64 - java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64 when trying to create CommonsHttpOAuthConsumer java.lang.ClassNotFoundException:org.apache.commons.codec.binary.Base64 - java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64 org.apache.commons.codec.binary.Base64和com.sun.org.apache.xml.internal.security.utils.Base64之间有什么区别吗? - Is there any difference between org.apache.commons.codec.binary.Base64 & com.sun.org.apache.xml.internal.security.utils.Base64? NoClassDefFoundError:org / apache / tomcat / util / codec / binary / Base64 - NoClassDefFoundError: org/apache/tomcat/util/codec/binary/Base64 NoSuchMethodError:Android上的org.apache.commons.codec.binary.Base64.decode - NoSuchMethodError: org.apache.commons.codec.binary.Base64.decode on Android 适用于JSE 7的android.util.Base64 - android.util.Base64 for JSE 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM