Java:如何将String转换为base64string?

时间:2017-03-17 11:15:38

标签: java android string decode

我正在使用此方法解密收到的消息:

private static String decrypt(String key, String initVector, String dataToDecrypt) {
    try {
        IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

        String safeString = dataToDecrypt.replace('-', '+').replace('_', '/');
        byte[] decodedString = Base64.decodeBase64(safeString);

        byte[] original = cipher.doFinal(decodedString);

        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

但是,我的Android应用崩溃,显示以下异常:

  

java.lang.NoSuchMethodError:没有静态方法   decodeBase64(Ljava / lang / String;)[B类   Lorg /阿帕奇/公/编解码器/二进制/ Base64编码;或者它的超级课程   ('org.apache.commons.codec.binary.Base64'的声明出现在   /system/framework/ext.jar)

因此,方法decodeBase64需要base64string,但我通过string。这是我的问题:

如何将String转换为base64string?!

请注意我正在尝试解码而不是ENCODE。几乎所有提供的解决方案都是编码部分,这不是我的担心。

P.S。:我正在开发Android-Studio上的Android应用程序

4 个答案:

答案 0 :(得分:1)

您检查this answer

        // decode data from base 64
        private static byte[] decodeBase64(String dataToDecode)
        {
            byte[] dataDecoded = Base64.decode(dataToDecode, Base64.DEFAULT);
            return dataDecoded;
        }

       //enconde data in base 64
        private static byte[] encodeBase64(byte[] dataToEncode)
        {
            byte[] dataEncoded = Base64.encode(dataToEncode, Base64.DEFAULT);
            return dataEncoded;
        }

答案 1 :(得分:1)

试试这个:

Base64.encodeToString(mStringToEncode.getBytes(), Base64.NO_WRAP)

它存在很多编码模式使用自动完成功能来查看更多Base64.NO_WRAP,Base64.CRLF等...

您需要导入包:

import android.util.Base64;

答案 2 :(得分:1)

puts  "enter your name:
@name = gets
puts @name
...

答案 3 :(得分:1)

试试此代码

private static String encryptNew(String key, String initVector, String dataToEncrypt) throws Exception{

        byte[] plainTextbytes = dataToEncrypt.getBytes("UTF-8");
        byte[] keyBytes = key.getBytes("UTF-8");
        byte[] IvkeyBytes = initVector.getBytes("UTF-8");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(IvkeyBytes);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        plainTextbytes = cipher.doFinal(plainTextbytes);
        return Base64.encodeToString(plainTextbytes, Base64.DEFAULT);
    }

    private static String decrypt(String key, String initVector, String dataToDecrypt) {
            try {

                byte[] cipheredBytes = Base64.decode(dataToDecrypt, Base64.DEFAULT);
                byte[] keyBytes = key.getBytes("UTF-8");
                byte[] IvkeyBytes = initVector.getBytes("UTF-8");

                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
                SecretKeySpec secretKeySpecy = new SecretKeySpec(keyBytes, "AES");
                IvParameterSpec ivParameterSpec = new IvParameterSpec(IvkeyBytes);
                cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec);
                cipheredBytes = cipher.doFinal(cipheredBytes);

                return new String(cipheredBytes,"UTF-8");
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            return null;
        }