1

java.lang.IllegalArgumentException: bad base-64 Android1.5でBase64を使用しようとすると取得します

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)
4

3 に答える 3

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

上記のクラスは、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);
}
于 2012-10-25T11:00:02.480 に答える
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);
    }
}

不正な引数の例外キャッチを追加.....それが機能することを願っています

于 2012-10-25T10:47:34.433 に答える
0

これを見たことがありますか ?

AndroidでBase64を使用して文字列をエンコードするにはどうすればよいですか?

文字列のエンコード/デコードにiHarder.netのBase64ライブラリを使用しています

于 2012-10-25T10:51:37.397 に答える