我在Android应用上使用此代码来保护我的公钥:
private static String xorDecrypt(String input, String key) {
byte[] inputBytes = Base64.decode(input, Base64.DEFAULT);
int inputSize = inputBytes.length;
byte[] keyBytes = key.getBytes();
int keySize = keyBytes.length - 1;
byte[] outBytes = new byte[inputSize];
for (int i=0; i<inputSize; i++) {
outBytes[i] = (byte) (inputBytes[i] ^ keyBytes[i % keySize]);
}
return new String(outBytes);
}
在某些设备上,这可以正常工作但在某些设备上我收到此错误:
java.lang.IllegalArgumentException: com.myname.myapp.a.b: Bad Base64 input character at 160: -65(decimal)
at com.myname.myapp.a.s.a(Unknown Source)
at com.myname.myapp.a.s.a(Unknown Source)
at com.myname.myapp.a.d.a(Unknown Source)
at com.myname.myapp.a.d.a(Unknown Source)
at com.myname.myapp.a.d.a(Unknown Source)
at com.myname.myapp.a.f.run(Unknown Source)
at java.lang.Thread.run(Thread.java:841)
Caused by: com.myname.myapp.a.b: Bad Base64 input character at 160: -65(decimal)
at com.myname.myapp.a.a.a(Unknown Source)
at com.myname.myapp.a.a.a(Unknown Source)
at com.myname.myapp.a.a.a(Unknown Source)
... 7 more
我认为问题是xorDecrypt函数,因为我只使用了base64函数。
我该如何避免呢?
此错误强制关闭我的应用。
答案 0 :(得分:0)
在解码前检查,它可能会有所帮助:
if(Base64.isBase64(input)){
Base64.decodeBase64(input);
}