String decode mit Base64

BaseBallBatBoy

Erfahrenes Mitglied
Hallo!

Ich hab n string, den ich mit Base64 decodieren will. Ich brauch die org.apache.commons.codec libs. Aber irgendwie krieg ich das nicht gebacken.... Habt ihr da eine Idee? Kann auch mit einem anderen Package der commons.codec sein.

PS: "OTkw" ergibt 990, nur so zum Test....

PHP:
import org.apache.commons.codec.net.BCodec;
import org.apache.commons.codec.DecoderException;

public class Base64Demo 
{ 
  public static void main(String[] args) {
	String inputB64 = "OTkw";
    System.out.println(inputB64); 
    
    
	try {
		inputB64 = decodeB64(inputB64);
	} catch (DecoderException de) {

	}
	
	System.out.println(inputB64); 
  }
  
  public static String decodeB64(String inputBase64) throws DecoderException {
	  System.out.println(inputBase64); 
	  String inputClear;
	  
	  try {
		  System.out.println("Beginn decoding");
		  inputClear = new BCodec().decode(inputBase64);
		  
	  } catch (DecoderException de) {
		  inputClear = "";
		  System.out.println("FAIL"); 
	  }
	  System.out.println(inputClear); 
	  return inputClear;
  }
}
 
Hallo,

schau mal hier:
Java:
/**
 * 
 */
package de.tutorials;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @author Thomas.Darimont
 *
 */
public class Base64Example {

    /**
     * @param args
     * 
     * http://de.wikipedia.org/wiki/Base64
     */
    public static void main(String[] args) throws Exception{
        String encoded = new BASE64Encoder().encode("Hätten Hüte ein ß im Namen, wären sie möglicherweise keine Hüte mehr,\nsondern Hüße.".getBytes("UTF-8"));
        System.out.println(encoded);
        String decoded = new String(new BASE64Decoder().decodeBuffer(encoded),"UTF-8");
        System.out.println(decoded);
    }
}

Nur mal so zum testen... da bekomme ich für deinen Eingabestring aber auch was anderes raus...

Gruß Tom
 
die sun.misc will ich nicht brauchen....
aber ich habs nun:

PHP:
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;

public class Base64Demo{

	public static String decodeBase64(String encoded) throws DecoderException {
		//decode byte array
		byte[] decoded = Base64.decodeBase64(encoded.getBytes());
		//byte to string and return it
		return new String(decoded);
	}

	public static void main(String[] args) {
		String testStr = "OTkw";
		try {
			testStr = decodeBase64(testStr);
		} catch (DecoderException ex) {
			System.out.println("Was not able to decode.");
		}
    }
}

bin mir nur noch nicht sicher, ob das mit dem exceptionhandling richtig ist.... comments?
 
Zurück