尝试在Android 1.5上使用Base64时出现“IllegalArgumentException:bad base-64”

时间:2012-10-25 10:38:58

标签: android base64 android-1.5-cupcake

我尝试在Android 1.5上使用Base64时获得java.lang.IllegalArgumentException: bad base-64

private static String encrypt(Context cont, String value) {
    try {
        return Base64.encodeToString(value.getBytes(), Base64.DEFAULT);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

private static String decrypt(Context cont, String value) {
    try {
        return new String(Base64.decode(value, Base64.DEFAULT));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
}



Caused by: java.lang.IllegalArgumentException: bad base-64  
at: Base64.decode(Base64.java:161)

3 个答案:

答案 0 :(得分:4)

public class Crypto {
   public static String encrypt(String value, String key) throws GeneralSecurityException {
    SecretKeySpec sks = new SecretKeySpec(hexStringToByteArray(key), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks, cipher.getParameters());
    byte[] encrypted = cipher.doFinal(value.getBytes());
    return byteArrayToHexString(encrypted);
}

public static String decrypt(String message, String key) throws GeneralSecurityException {
    SecretKeySpec sks = new SecretKeySpec(hexStringToByteArray(key), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);
    byte[] decrypted = cipher.doFinal(hexStringToByteArray(message));
    return new String(decrypted);
}   

private static String byteArrayToHexString(byte[] b){
    StringBuffer sb = new StringBuffer(b.length * 2);
    for (int i = 0; i < b.length; i++){
        int v = b[i] & 0xff;
        if (v < 16) {
            sb.append('0');
        }
        sb.append(Integer.toHexString(v));
    }
    return sb.toString().toUpperCase();
}

private static byte[] hexStringToByteArray(String s) {
    byte[] b = new byte[s.length() / 2];
    for (int i = 0; i < b.length; i++){
        int index = i * 2;
        int v = Integer.parseInt(s.substring(index, index + 2), 16);
        b[i] = (byte)v;
    }
    return b;
}
  }

上面的类将有助于加密和解密不带base64的字符串

上述课程的使用

try {
    String key = UUID.randomUUID().toString().replaceAll("-", "");
    String src = "Hello world!";
    String tag1 = Crypto.encrypt(src, key);
    String tag2 = Crypto.decrypt(tag1, key);
    logger.info(src);
    logger.info(tag1);
    logger.info(tag2);
} catch (Exception e) {
    logger.error("", e);
}

答案 1 :(得分:0)

private static String encrypt(Context cont, String value) {
    try {
        return Base64.encodeToString(value.getBytes(), Base64.DEFAULT);
    }
catch (IllegalArgumentException e) {
            // TODO: handle exception
        }
 catch (Exception e) {
        throw new RuntimeException(e);
    }
}

private static String decrypt(Context cont, String value) {
    try {
        return new String(Base64.decode(value, Base64.DEFAULT));
    }
catch (IllegalArgumentException e) {
            // TODO: handle exception
        } 
catch (Exception e) {
        throw new RuntimeException(e);
    }
}

添加非法参数异常捕获..... 我希望它能起作用

答案 2 :(得分:0)

你见过这个吗?

How to encode a string with Base64 in Android?

他们正在使用iHarder.net中的Base64库来编码/解码字符串