Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的Base64编码,使用android.util.Base64_Java_Android_Encryption_Base64 - Fatal编程技术网

Java中的Base64编码,使用android.util.Base64

Java中的Base64编码,使用android.util.Base64,java,android,encryption,base64,Java,Android,Encryption,Base64,我知道,关于这个问题有很多线索——我读过大部分,但没有一条给了我正确的答案 我有以下代码: import android.util.Base64; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; public class Crypter { public static void main(String[] args) { St

我知道,关于这个问题有很多线索——我读过大部分,但没有一条给了我正确的答案

我有以下代码:

import android.util.Base64;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Crypter {

    public static void main(String[] args) {
        String data = "Arnab C";
        final String enc = DarKnight.getEncrypted(data);
        System.out.println("Encrypted : " + enc);
        System.out.println("Decrypted : " + DarKnight.getDecrypted(enc));
    }

    static class DarKnight {

        private static final String ALGORITHM = "AES";

        private static final byte[] SALT = "tHeApAcHe6410111".getBytes();// THE KEY MUST BE SAME
        private static final String X = DarKnight.class.getSimpleName();

        static String getEncrypted(String plainText) {

            if (plainText == null) {
                return null;
            }

            Key salt = getSalt();

            try {
                Cipher cipher = Cipher.getInstance(ALGORITHM);
                cipher.init(Cipher.ENCRYPT_MODE, salt);
                byte[] encodedValue = cipher.doFinal(plainText.getBytes());
                return Base64.encode(encodedValue,Base64.DEFAULT);


            } catch (Exception e) {
                e.printStackTrace();
            }

            throw new IllegalArgumentException("Failed to encrypt data");
        }

        public static String getDecrypted(String encodedText) {

            if (encodedText == null) {
                return null;
            }

            Key salt = getSalt();
            try {
                Cipher cipher = Cipher.getInstance(ALGORITHM);
                cipher.init(Cipher.DECRYPT_MODE, salt);
                byte[] decodedValue = Base64.decode(encodedText, Base64.DEFAULT);
                byte[] decValue = cipher.doFinal(decodedValue);
                return new String(decValue);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        static Key getSalt() {
            return new SecretKeySpec(SALT, ALGORITHM);
        }

    }

}
我从中复制了这个,但是改变了

import com.sun.org.apache.xml.internal.security.utils.Base64;

因为apache版本在Java8中不起作用

由于该更改,我不得不向Base64.decode和Base64.encode添加一个标志。在解码方面效果很好:

byte[] decodedValue = Base64.decode(encodedText, Base64.DEFAULT);
但在Base64.encode中添加标志时,会发生一些奇怪的情况:

当我写“returnbase64.encode”()时,Android Studio告诉我它需要一个byte[]输入和一个int标志。 所以我想,我可以简单地使用变量encodedValue作为第一个参数,因为它是一个字节[]。 作为下一个参数,我可以使用Base64.DEFAULT,它是一个值为0的int。 但Android Studio不同意:不兼容类型,必需:java.lang.String,找到:byte[]

当Android Studio第一次说它需要字节[]时,为什么它需要字符串

事实上,“为什么”并不重要,更重要的是:我该如何解决这个问题

任何帮助都将不胜感激。

使用
encodeToString()
而不是
encode()
。第一个返回函数返回类型所期望的
字符串,后者返回
字节[]

文件:

byte[] decodedValue = Base64.decode(encodedText, Base64.DEFAULT);