1

I get java.lang.IllegalArgumentException: bad base-64 while trying to use Base64 on Android 1.5

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)
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
pvllnspk
  • 5,667
  • 12
  • 59
  • 97

3 Answers3

5
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;
}
  }

above class will help in encrypt and decrypt string without base64

usage of above class

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);
}
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
Venkatesh S
  • 5,466
  • 1
  • 25
  • 30
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);
    }
}

add illegal argument exception catch ..... I hope it will work

Venkatesh S
  • 5,466
  • 1
  • 25
  • 30
  • the problem is in the Base64 class, it works great for the versions that it's embedded in the system, but if I try to import this class in android 1.5 - in the above case it doesn't work. – pvllnspk Oct 25 '12 at 10:53
0

have you seen this ?

How to encode a string with Base64 in Android?

They are using Base64 Library from iHarder.net to encode/decode strings

Community
  • 1
  • 1
sunil
  • 6,444
  • 1
  • 32
  • 44
  • Sorry, I need code snippet or direction to dig the problem, not a library. – pvllnspk Oct 25 '12 at 10:56
  • ok.. which third party Base64 library have you used in your example, as android 1.5 doesn't have native support for Base64 ? – sunil Oct 25 '12 at 11:02
  • please see the question: /* * Copyright (C) 2010 The Android Open Source Project * * Base64.java – pvllnspk Oct 25 '12 at 11:03
  • you said you use Android 1.5 and this version doesn't have native Base64 library. Then from where does you get this code segment ? – sunil Oct 25 '12 at 11:45
  • Base64.java class - you can find it easily, it's an open source – pvllnspk Oct 25 '12 at 12:57