"抛出:IllegalArgumentException:坏基地-64"试图使用的Base64在Android 1.5 [英] "IllegalArgumentException: bad base-64" while trying to use Base64 on Android 1.5

查看:214
本文介绍了"抛出:IllegalArgumentException:坏基地-64"试图使用的Base64在Android 1.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到 java.lang.IllegalArgumentException:如果坏基地-64 试图使用的Base64在Android 1.5

 私有静态加密字符串(上下文续,字符串值){
    尝试 {
        返回Base64.en codeToString(value.getBytes(),Base64.DEFAULT);
    }赶上(例外五){
        抛出新的RuntimeException(E);
    }
}

私有静态字符串解密(上下文续,字符串值){
    尝试 {
        返回新的String(Base64.de code(值,Base64.DEFAULT));
    }赶上(例外五){
        抛出新的RuntimeException(E);
    }
}

/ *
 *版权所有(C)2010年Android开源项目
 *
 * Apache许可证下授权,版本2.0(以下简称许可证);
 *您可能不能使用这个文件除了在遵守许可。
 *您可以在获得许可证的副本
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意,软件
 *许可证下发布分布在原样的基础上,
 *无担保或任何形式的条件,无论是EX preSS或暗示的保证。
 *请参阅许可的特定语言的管理权限和
 *许可下限制。
 * /
}



致:java.lang.IllegalArgumentException:如果坏基地-64
位置:Base64.de code(Base64.java:161)
 

解决方案

 公共类加密{
   公共静态字符串加密(字符串值,字符串键)抛出GeneralSecurityException {
    SecretKeySpec SKS =新SecretKeySpec(hexStringToByteArray(键),AES);
    密密码= Cipher.getInstance(AES);
    cipher.init(Cipher.ENCRYPT_MODE,SKS,cipher.getParameters());
    byte []的加密= cipher.doFinal(value.getBytes());
    返回byteArrayToHexString(加密的);
}

公共静态字符串解密(字符串消息,串键)抛出GeneralSecurityException {
    SecretKeySpec SKS =新SecretKeySpec(hexStringToByteArray(键),AES);
    密密码= Cipher.getInstance(AES);
    cipher.init(Cipher.DECRYPT_MODE,SKS);
    byte []的解密= cipher.doFinal(hexStringToByteArray(消息));
    返回新的String(解密);
}

私有静态字符串byteArrayToHexString(byte []的B){
    StringBuffer的SB =新的StringBuffer(b.length个* 2);
    的for(int i = 0; I< b.length个;我++){
        INT V = B〔1]  - 安培; 0xFF的;
        如果(V族; 16){
            sb.append('0');
        }
        sb.append(Integer.toHexString(ⅴ));
    }
    返回sb.toString()与toUpperCase()。
}

私有静态的byte [] hexStringToByteArray(String s)将{
    byte []的B =新的字节[s.length()/ 2]。
    的for(int i = 0; I< b.length个;我++){
        INT指数= I * 2;
        INT V =的Integer.parseInt(s.substring(指数,指数+ 2),16);
        B〔Ⅰ〕=(字节)v的
    }
    返回b;
}
  }
 

以上的课程将有助于加密和解密字符串没有的base64

上面级的使用

 尝试{
    字符串键= UUID.randomUUID()的toString()的replaceAll( - ,);
    的字符串src =世界,你好!;
    字符串标签1 = Crypto.encrypt(SRC,键);
    字符串标签2 = Crypto.decrypt(标签1,密钥);
    logger.info(SRC);
    logger.info(标签1);
    logger.info(标签2);
}赶上(例外五){
    logger.error(,E);
}
 

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)

解决方案

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

这篇关于&QUOT;抛出:IllegalArgumentException:坏基地-64&QUOT;试图使用的Base64在Android 1.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆