Weil es in Java keine mir bekannte Standard-Klasse gibt, um einen String in Base64 zu verschlüsseln oder zu entschlüsseln, habe ich mal eine Klasse dafür zusammen programmiert.
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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | public class Base64Coding { private final static char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray(); private static int[] toInt = new int[128]; static { for (int i = 0; i < ALPHABET.length; i++) { toInt[ALPHABET[i]] = i; } } /** * Encodes the specified String into a Base64-String. * * @param String The string which should be encoded. * @return The Base64-encoded String */ public static String encode(String string) { byte[] buf = string.getBytes(); int size = buf.length; char[] ar = new char[((size + 2) / 3) * 4]; int a = 0; int i = 0; while (i < size) { byte b0 = buf[i++]; byte b1 = (i < size) ? buf[i++] : 0; byte b2 = (i < size) ? buf[i++] : 0; int mask = 0x3F; ar[a++] = ALPHABET[(b0 >> 2) & mask]; ar[a++] = ALPHABET[((b0 << 4) | ((b1 & 0xFF) >> 4)) & mask]; ar[a++] = ALPHABET[((b1 << 2) | ((b2 & 0xFF) >> 6)) & mask]; ar[a++] = ALPHABET[b2 & mask]; } switch (size % 3) { case 1: ar[--a] = '='; case 2: ar[--a] = '='; } return new String(ar); } /** * Decodes a Bas64-encoded String into a usual String. * * @param base64 The Base64-encoded String. * @return The Base64-decoded String. */ public static String decode(String base64) { int delta = base64.endsWith("==") ? 2 : base64.endsWith("=") ? 1 : 0; byte[] buffer = new byte[base64.length() * 3 / 4 - delta]; int mask = 0xFF; int index = 0; for (int i = 0; i < base64.length(); i += 4) { int c0 = toInt[base64.charAt(i)]; int c1 = toInt[base64.charAt(i + 1)]; buffer[index++] = (byte) (((c0 << 2) | (c1 >> 4)) & mask); if (index >= buffer.length) { return new String(buffer); } int c2 = toInt[base64.charAt(i + 2)]; buffer[index++] = (byte) (((c1 << 4) | (c2 >> 2)) & mask); if (index >= buffer.length) { return new String(buffer); } int c3 = toInt[base64.charAt(i + 3)]; buffer[index++] = (byte) (((c2 << 6) | c3) & mask); } return new String(buffer); } } |
import sun.misc.BASE64Encoder;
😉
Bei der Verwendung von sun.misc.BASE64Encoder gibt Java beim Kompilieren die Meldung: „warning: sun.misc.BASE64Decoder is Sun proprietary API and may be removed in a future release“ aus. Deshalb habe ich darauf verzichtet und eine eigene Version implementiert. Trotzdem vielen Dank für den Hinweis!
wie wärs es damit:
org.apache.commons.codec.binary.Base64
Wenn der zu encodierende String Umlaute enthält werden diese beim Decodieren als Fragezeichen (?) dargestellt. Kennst du eine Lösung für dieses Problem?