Decode Base64 data in Java

前端 未结 20 1496
时光说笑
时光说笑 2020-11-21 06:04

I have an image that is Base64 encoded. What is the best way to decode that in Java? Hopefully using only the libraries included with Sun Java 6.

相关标签:
20条回答
  • 2020-11-21 06:27

    I used android.util.base64 that works pretty good without any dependances:

    Usage:

    byte[] decodedKey = Base64.decode(encodedPublicKey, Base64.DEFAULT);
    

    package com.test;

    import java.io.UnsupportedEncodingException;
    
    /**
     * Utilities for encoding and decoding the Base64 representation of
     * binary data.  See RFCs <a
     * href="http://www.ietf.org/rfc/rfc2045.txt">2045</a> and <a
     * href="http://www.ietf.org/rfc/rfc3548.txt">3548</a>.
     */
    public class Base64 {
    
        public static final int DEFAULT = 0;
    
    
        public static final int NO_PADDING = 1;
    
    
        public static final int NO_WRAP = 2;
    
    
        public static final int CRLF = 4;
    
    
        public static final int URL_SAFE = 8;
    
    
        public static final int NO_CLOSE = 16;
    
        //  --------------------------------------------------------
        //  shared code
        //  --------------------------------------------------------
    
        /* package */ static abstract class Coder {
            public byte[] output;
            public int op;
    
            public abstract boolean process(byte[] input, int offset, int len, boolean finish);
    
    
            public abstract int maxOutputSize(int len);
        }
    
        //  --------------------------------------------------------
        //  decoding
        //  --------------------------------------------------------
    
    
        public static byte[] decode(String str, int flags) {
            return decode(str.getBytes(), flags);
        }
    
        public static byte[] decode(byte[] input, int flags) {
            return decode(input, 0, input.length, flags);
        }
    
    
        public static byte[] decode(byte[] input, int offset, int len, int flags) {
            // Allocate space for the most data the input could represent.
            // (It could contain less if it contains whitespace, etc.)
            Decoder decoder = new Decoder(flags, new byte[len*3/4]);
    
            if (!decoder.process(input, offset, len, true)) {
                throw new IllegalArgumentException("bad base-64");
            }
    
            // Maybe we got lucky and allocated exactly enough output space.
            if (decoder.op == decoder.output.length) {
                return decoder.output;
            }
    
            // Need to shorten the array, so allocate a new one of the
            // right size and copy.
            byte[] temp = new byte[decoder.op];
            System.arraycopy(decoder.output, 0, temp, 0, decoder.op);
            return temp;
        }
    
       static class Decoder extends Coder {       
            private static final int DECODE[] = {
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
                52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1,
                -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
                15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
                -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
                41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            };
    
            /**
             * Decode lookup table for the "web safe" variant (RFC 3548
             * sec. 4) where - and _ replace + and /.
             */
            private static final int DECODE_WEBSAFE[] = {
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1,
                52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1,
                -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
                15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
                -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
                41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            };
    
            /** Non-data values in the DECODE arrays. */
            private static final int SKIP = -1;
            private static final int EQUALS = -2;
    
    
            private int state;   // state number (0 to 6)
            private int value;
    
            final private int[] alphabet;
    
            public Decoder(int flags, byte[] output) {
                this.output = output;
    
                alphabet = ((flags & URL_SAFE) == 0) ? DECODE : DECODE_WEBSAFE;
                state = 0;
                value = 0;
            }
    
    
            public int maxOutputSize(int len) {
                return len * 3/4 + 10;
            }
    
            /**
             * Decode another block of input data.
             *
             * @return true if the state machine is still healthy.  false if
             *         bad base-64 data has been detected in the input stream.
             */
            public boolean process(byte[] input, int offset, int len, boolean finish) {
                if (this.state == 6) return false;
    
                int p = offset;
                len += offset;
    
                int state = this.state;
                int value = this.value;
                int op = 0;
                final byte[] output = this.output;
                final int[] alphabet = this.alphabet;
    
                while (p < len) {                
                    if (state == 0) {
                        while (p+4 <= len &&
                               (value = ((alphabet[input[p] & 0xff] << 18) |
                                         (alphabet[input[p+1] & 0xff] << 12) |
                                         (alphabet[input[p+2] & 0xff] << 6) |
                                         (alphabet[input[p+3] & 0xff]))) >= 0) {
                            output[op+2] = (byte) value;
                            output[op+1] = (byte) (value >> 8);
                            output[op] = (byte) (value >> 16);
                            op += 3;
                            p += 4;
                        }
                        if (p >= len) break;
                    }
    
                    int d = alphabet[input[p++] & 0xff];
    
                    switch (state) {
                    case 0:
                        if (d >= 0) {
                            value = d;
                            ++state;
                        } else if (d != SKIP) {
                            this.state = 6;
                            return false;
                        }
                        break;
    
                    case 1:
                        if (d >= 0) {
                            value = (value << 6) | d;
                            ++state;
                        } else if (d != SKIP) {
                            this.state = 6;
                            return false;
                        }
                        break;
    
                    case 2:
                        if (d >= 0) {
                            value = (value << 6) | d;
                            ++state;
                        } else if (d == EQUALS) {
                            // Emit the last (partial) output tuple;
                            // expect exactly one more padding character.
                            output[op++] = (byte) (value >> 4);
                            state = 4;
                        } else if (d != SKIP) {
                            this.state = 6;
                            return false;
                        }
                        break;
    
                    case 3:
                        if (d >= 0) {
                            // Emit the output triple and return to state 0.
                            value = (value << 6) | d;
                            output[op+2] = (byte) value;
                            output[op+1] = (byte) (value >> 8);
                            output[op] = (byte) (value >> 16);
                            op += 3;
                            state = 0;
                        } else if (d == EQUALS) {
                            // Emit the last (partial) output tuple;
                            // expect no further data or padding characters.
                            output[op+1] = (byte) (value >> 2);
                            output[op] = (byte) (value >> 10);
                            op += 2;
                            state = 5;
                        } else if (d != SKIP) {
                            this.state = 6;
                            return false;
                        }
                        break;
    
                    case 4:
                        if (d == EQUALS) {
                            ++state;
                        } else if (d != SKIP) {
                            this.state = 6;
                            return false;
                        }
                        break;
    
                    case 5:
                        if (d != SKIP) {
                            this.state = 6;
                            return false;
                        }
                        break;
                    }
                }
    
                if (!finish) {
                    // We're out of input, but a future call could provide
                    // more.
                    this.state = state;
                    this.value = value;
                    this.op = op;
                    return true;
                }
    
    
                switch (state) {
                case 0:
                    break;
                case 1:
                    this.state = 6;
                    return false;
                case 2:
                    output[op++] = (byte) (value >> 4);
                    break;
                case 3:
                   output[op++] = (byte) (value >> 10);
                    output[op++] = (byte) (value >> 2);
                    break;
                case 4:
                    this.state = 6;
                    return false;
                case 5:
                    break;
                }
    
                this.state = state;
                this.op = op;
                return true;
            }
        }
    
        //  --------------------------------------------------------
        //  encoding
        //  --------------------------------------------------------    
        public static String encodeToString(byte[] input, int flags) {
            try {
                return new String(encode(input, flags), "US-ASCII");
            } catch (UnsupportedEncodingException e) {
                // US-ASCII is guaranteed to be available.
                throw new AssertionError(e);
            }
        }
    
    
        public static String encodeToString(byte[] input, int offset, int len, int flags) {
            try {
                return new String(encode(input, offset, len, flags), "US-ASCII");
            } catch (UnsupportedEncodingException e) {
                // US-ASCII is guaranteed to be available.
                throw new AssertionError(e);
            }
        }
    
    
        public static byte[] encode(byte[] input, int flags) {
            return encode(input, 0, input.length, flags);
        }
    
    
        public static byte[] encode(byte[] input, int offset, int len, int flags) {
            Encoder encoder = new Encoder(flags, null);
    
            // Compute the exact length of the array we will produce.
            int output_len = len / 3 * 4;
    
            // Account for the tail of the data and the padding bytes, if any.
            if (encoder.do_padding) {
                if (len % 3 > 0) {
                    output_len += 4;
                }
            } else {
                switch (len % 3) {
                    case 0: break;
                    case 1: output_len += 2; break;
                    case 2: output_len += 3; break;
                }
            }
    
            // Account for the newlines, if any.
            if (encoder.do_newline && len > 0) {
                output_len += (((len-1) / (3 * Encoder.LINE_GROUPS)) + 1) *
                    (encoder.do_cr ? 2 : 1);
            }
    
            encoder.output = new byte[output_len];
            encoder.process(input, offset, len, true);
    
            assert encoder.op == output_len;
    
            return encoder.output;
        }
    
        /* package */ static class Encoder extends Coder {
            /**
             * Emit a new line every this many output tuples.  Corresponds to
             * a 76-character line length (the maximum allowable according to
             * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>).
             */
            public static final int LINE_GROUPS = 19;
    
            /**
             * Lookup table for turning Base64 alphabet positions (6 bits)
             * into output bytes.
             */
            private static final byte ENCODE[] = {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
            };
    
            /**
             * Lookup table for turning Base64 alphabet positions (6 bits)
             * into output bytes.
             */
            private static final byte ENCODE_WEBSAFE[] = {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_',
            };
    
            final private byte[] tail;
            /* package */ int tailLen;
            private int count;
    
            final public boolean do_padding;
            final public boolean do_newline;
            final public boolean do_cr;
            final private byte[] alphabet;
    
            public Encoder(int flags, byte[] output) {
                this.output = output;
    
                do_padding = (flags & NO_PADDING) == 0;
                do_newline = (flags & NO_WRAP) == 0;
                do_cr = (flags & CRLF) != 0;
                alphabet = ((flags & URL_SAFE) == 0) ? ENCODE : ENCODE_WEBSAFE;
    
                tail = new byte[2];
                tailLen = 0;
    
                count = do_newline ? LINE_GROUPS : -1;
            }
    
            /**
             * @return an overestimate for the number of bytes {@code
             * len} bytes could encode to.
             */
            public int maxOutputSize(int len) {
                return len * 8/5 + 10;
            }
    
            public boolean process(byte[] input, int offset, int len, boolean finish) {
                // Using local variables makes the encoder about 9% faster.
                final byte[] alphabet = this.alphabet;
                final byte[] output = this.output;
                int op = 0;
                int count = this.count;
    
                int p = offset;
                len += offset;
                int v = -1;
    
                // First we need to concatenate the tail of the previous call
                // with any input bytes available now and see if we can empty
                // the tail.
    
                switch (tailLen) {
                    case 0:
                        // There was no tail.
                        break;
    
                    case 1:
                        if (p+2 <= len) {
                            // A 1-byte tail with at least 2 bytes of
                            // input available now.
                            v = ((tail[0] & 0xff) << 16) |
                                ((input[p++] & 0xff) << 8) |
                                (input[p++] & 0xff);
                            tailLen = 0;
                        };
                        break;
    
                    case 2:
                        if (p+1 <= len) {
                            // A 2-byte tail with at least 1 byte of input.
                            v = ((tail[0] & 0xff) << 16) |
                                ((tail[1] & 0xff) << 8) |
                                (input[p++] & 0xff);
                            tailLen = 0;
                        }
                        break;
                }
    
                if (v != -1) {
                    output[op++] = alphabet[(v >> 18) & 0x3f];
                    output[op++] = alphabet[(v >> 12) & 0x3f];
                    output[op++] = alphabet[(v >> 6) & 0x3f];
                    output[op++] = alphabet[v & 0x3f];
                    if (--count == 0) {
                        if (do_cr) output[op++] = '\r';
                        output[op++] = '\n';
                        count = LINE_GROUPS;
                    }
                }
    
                // At this point either there is no tail, or there are fewer
                // than 3 bytes of input available.
    
                // The main loop, turning 3 input bytes into 4 output bytes on
                // each iteration.
                while (p+3 <= len) {
                    v = ((input[p] & 0xff) << 16) |
                        ((input[p+1] & 0xff) << 8) |
                        (input[p+2] & 0xff);
                    output[op] = alphabet[(v >> 18) & 0x3f];
                    output[op+1] = alphabet[(v >> 12) & 0x3f];
                    output[op+2] = alphabet[(v >> 6) & 0x3f];
                    output[op+3] = alphabet[v & 0x3f];
                    p += 3;
                    op += 4;
                    if (--count == 0) {
                        if (do_cr) output[op++] = '\r';
                        output[op++] = '\n';
                        count = LINE_GROUPS;
                    }
                }
    
                if (finish) {
    
                    if (p-tailLen == len-1) {
                        int t = 0;
                        v = ((tailLen > 0 ? tail[t++] : input[p++]) & 0xff) << 4;
                        tailLen -= t;
                        output[op++] = alphabet[(v >> 6) & 0x3f];
                        output[op++] = alphabet[v & 0x3f];
                        if (do_padding) {
                            output[op++] = '=';
                            output[op++] = '=';
                        }
                        if (do_newline) {
                            if (do_cr) output[op++] = '\r';
                            output[op++] = '\n';
                        }
                    } else if (p-tailLen == len-2) {
                        int t = 0;
                        v = (((tailLen > 1 ? tail[t++] : input[p++]) & 0xff) << 10) |
                            (((tailLen > 0 ? tail[t++] : input[p++]) & 0xff) << 2);
                        tailLen -= t;
                        output[op++] = alphabet[(v >> 12) & 0x3f];
                        output[op++] = alphabet[(v >> 6) & 0x3f];
                        output[op++] = alphabet[v & 0x3f];
                        if (do_padding) {
                            output[op++] = '=';
                        }
                        if (do_newline) {
                            if (do_cr) output[op++] = '\r';
                            output[op++] = '\n';
                        }
                    } else if (do_newline && op > 0 && count != LINE_GROUPS) {
                        if (do_cr) output[op++] = '\r';
                        output[op++] = '\n';
                    }
    
                    assert tailLen == 0;
                    assert p == len;
                } else {
                    // Save the leftovers in tail to be consumed on the next
                    // call to encodeInternal.
    
                    if (p == len-1) {
                        tail[tailLen++] = input[p];
                    } else if (p == len-2) {
                        tail[tailLen++] = input[p];
                        tail[tailLen++] = input[p+1];
                    }
                }
    
                this.op = op;
                this.count = count;
    
                return true;
            }
        }
    
        private Base64() { }   // don't instantiate
    }
    
    0 讨论(0)
  • 2020-11-21 06:30

    Using Java 8 -

        public static String encodeString(String plainString) {
            return  Base64.getEncoder().encodeToString(plainString.getBytes());
        }
    
        public static String decodeString(String encodedString) {
            byte[] bytes = Base64.getDecoder().decode(encodedString);
            return new String(bytes);
        }
    
    0 讨论(0)
  • 2020-11-21 06:32

    Specifically in Commons Codec: class Base64 to decode(byte[] array) or encode(byte[] array)

    0 讨论(0)
  • 2020-11-21 06:33
    import java.io.UnsupportedEncodingException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.Arrays;
    import java.util.Base64;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    /***
     * 
     * @author Vaquar khan
     * 
     *
     */
    public class AES {
    
        private static SecretKeySpec secretKey;
        private static final String VK_secretKey = "VaquarKhan-secrate-key!!!!";
        private static byte[] key;
    
        /**
         * 
         * @param myKey
         */
        public static void setKey(String myKey) {
            MessageDigest sha = null;
            try {
                key = myKey.getBytes("UTF-8");
                sha = MessageDigest.getInstance("SHA-1");
                key = sha.digest(key);
                key = Arrays.copyOf(key, 16);
                secretKey = new SecretKeySpec(key, "AES");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    /**
     * encrypt
     * @param strToEncrypt
     * @param secret
     * @return
     */
        public static String encrypt(String strToEncrypt, String secret) {
            try {
                setKey(secret);
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, secretKey);
                return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
            } catch (Exception e) {
                System.out.println("Error while encrypting: " + e.toString());
            }
            return null;
        }
    /**
     * decrypt
     * @param strToDecrypt
     * @param secret
     * @return
     */
        public static String decrypt(String strToDecrypt, String secret) {
            try {
                setKey(secret);
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
                cipher.init(Cipher.DECRYPT_MODE, secretKey);
                return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
            } catch (Exception e) {
                System.out.println("Error while decrypting: " + e.toString());
            }
            return null;
        }
    
        public static void main(String[] args) {
            final String secretKey = VK_secretKey;
            String password = "VKhan@12";
            //
            String encryptedString = AES.encrypt(password, secretKey);
            String decryptedString = AES.decrypt(encryptedString, secretKey);
            //
            System.out.println(password);
            System.out.println(encryptedString);
            System.out.println(decryptedString);
        }
    
    }
    
    0 讨论(0)
  • 2020-11-21 06:36

    In a code compiled with Java 7 but potentially running in a higher java version, it seems useful to detect presence of java.util.Base64 class and use the approach best for given JVM mentioned in other questions here.

    I used this code:

    private static final Method JAVA_UTIL_BASE64_GETENCODER;
    
    static {
        Method getEncoderMethod;
        try {
            final Class<?> base64Class = Class.forName("java.util.Base64");
            getEncoderMethod = base64Class.getMethod("getEncoder");
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            getEncoderMethod = null;
        }
        JAVA_UTIL_BASE64_GETENCODER = getEncoderMethod;
    }
    
    static String base64EncodeToString(String s) {
        final byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
        if (JAVA_UTIL_BASE64_GETENCODER == null) {
            // Java 7 and older // TODO: remove this branch after switching to Java 8
            return DatatypeConverter.printBase64Binary(bytes);
        } else {
            // Java 8 and newer
            try {
                final Object encoder = JAVA_UTIL_BASE64_GETENCODER.invoke(null);
                final Class<?> encoderClass = encoder.getClass();
                final Method encodeMethod = encoderClass.getMethod("encode", byte[].class);
                final byte[] encodedBytes = (byte[]) encodeMethod.invoke(encoder, bytes);
                return new String(encodedBytes);
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                throw new IllegalStateException(e);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:37

    Guava now has Base64 decoding built in.

    Use BaseEncoding.base64().decode()

    As for dealing with possible whitespace in input use

    BaseEncoding.base64().decode(CharMatcher.WHITESPACE.removeFrom(...));

    See this discussion for more information

    0 讨论(0)
提交回复
热议问题