Questo algoritmo fa un po' pena, però ti fa capire abbastanza come funge il tutto!!
ps: produce un'errore negli ultimi bytes
codice:import java.io.*; public class Base64Decoding { StringBuffer buf; StringBuffer buf2; String Source; FileInputStream FileI; BufferedInputStream Bis; FileOutputStream FileO; DataOutputStream Bos; public static void main(String[] args) { Base64Decoding bas = new Base64Decoding("E:\\e.txt"); } public Base64Decoding(String source) { Source = source; leggi(); traduci(); scrivi(); } public void scrivi(){ boolean ela=false; int half=0; int half2=0; int tem=0; String temp; try { FileO = new FileOutputStream(Source+".ex_"); Bos = new DataOutputStream(FileO); for (int i = 0; i < buf2.length()-4; i=i+4) { temp=buf2.substring(i,i+4); if((temp.compareTo("0000"))==0){ tem=0x0; }else if(temp.compareTo("0001")==0){ tem=0x1; }else if(temp.compareTo("0010")==0){ tem=0x2; }else if(temp.compareTo("0011")==0){ tem=0x3; }else if(temp.compareTo("0100")==0){ tem=0x4; }else if(temp.compareTo("0101")==0){ tem=0x5; }else if(temp.compareTo("0110")==0){ tem=0x6; }else if(temp.compareTo("0111")==0){ tem=0x7; }else if(temp.compareTo("1000")==0){ tem=0x8; }else if(temp.compareTo("1001")==0){ tem=0x9; }else if(temp.compareTo("1010")==0){ tem=0xa; }else if(temp.compareTo("1011")==0){ tem=0xb; }else if(temp.compareTo("1100")==0){ tem=0xc; }else if(temp.compareTo("1101")==0){ tem=0xd; }else if(temp.compareTo("1110")==0){ tem=0xe; }else if(temp.compareTo("1111")==0){ tem=0xf; } if(ela==false){ half=tem; tem=0; ela=true; }else{ half2=tem; tem=0; ela=false; int finale=(half*0x10)+half2; Bos.writeByte(finale); half=0; half2=0; } } Bos.flush(); Bos.close(); } catch(Exception e) { System.err.println(e.toString()); e.printStackTrace(); } } public void leggi() { try { FileI = new FileInputStream(Source); Bis = new BufferedInputStream(FileI); buf = new StringBuffer(); int i = 0; do { i = Bis.read(); if ((i != -1) && ((char) i != '\r') && ((char) i != '\n')) { buf.append((char) i); } } while (i != -1); } catch (Exception e) { System.err.println(e.toString()); e.printStackTrace(); } // System.out.println("" + buf); } public void traduci() { int a = 0; buf2 = new StringBuffer(); for (int i = 0; i < buf.length(); i++) { switch (buf.charAt(i)) { case 'A' : buf2.append("000000"); break; case 'B' : buf2.append("000001"); break; case 'C' : buf2.append("000010"); break; case 'D' : buf2.append("000011"); break; case 'E' : buf2.append("000100"); break; case 'F' : buf2.append("000101"); break; case 'G' : buf2.append("000110"); break; case 'H' : buf2.append("000111"); break; case 'I' : buf2.append("001000"); break; case 'J' : buf2.append("001001"); break; case 'K' : buf2.append("001010"); break; case 'L' : buf2.append("001011"); break; case 'M' : buf2.append("001100"); break; case 'N' : buf2.append("001101"); break; case 'O' : buf2.append("001110"); break; case 'P' : buf2.append("001111"); break; case 'Q' : buf2.append("010000"); break; case 'R' : buf2.append("010001"); break; case 'S' : buf2.append("010010"); break; case 'T' : buf2.append("010011"); break; case 'U' : buf2.append("010100"); break; case 'V' : buf2.append("010101"); break; case 'W' : buf2.append("010110"); break; case 'X' : buf2.append("010111"); break; case 'Y' : buf2.append("011000"); break; case 'Z' : buf2.append("011001"); break; case 'a' : buf2.append("011010"); break; case 'b' : buf2.append("011011"); break; case 'c' : buf2.append("011100"); break; case 'd' : buf2.append("011101"); break; case 'e' : buf2.append("011110"); break; case 'f' : buf2.append("011111"); break; case 'g' : buf2.append("100000"); break; case 'h' : buf2.append("100001"); break; case 'i' : buf2.append("100010"); break; case 'j' : buf2.append("100011"); break; case 'k' : buf2.append("100100"); break; case 'l' : buf2.append("100101"); break; case 'm' : buf2.append("100110"); break; case 'n' : buf2.append("100111"); break; case 'o' : buf2.append("101000"); break; case 'p' : buf2.append("101001"); break; case 'q' : buf2.append("101010"); break; case 'r' : buf2.append("101011"); break; case 's' : buf2.append("101100"); break; case 't' : buf2.append("101101"); break; case 'u' : buf2.append("101110"); break; case 'v' : buf2.append("101111"); break; case 'w' : buf2.append("110000"); break; case 'x' : buf2.append("110001"); break; case 'y' : buf2.append("110010"); break; case 'z' : buf2.append("110011"); break; case '0' : buf2.append("110100"); break; case '1' : buf2.append("110101"); break; case '2' : buf2.append("110110"); break; case '3' : buf2.append("110111"); break; case '4' : buf2.append("111000"); break; case '5' : buf2.append("111001"); break; case '6' : buf2.append("111010"); break; case '7' : buf2.append("111011"); break; case '8' : buf2.append("111100"); break; case '9' : buf2.append("111101"); break; case '+' : buf2.append("111110"); break; case '/' : buf2.append("111111"); break; case '=' : a++; break; } if(a==1){ buf2.delete((buf2.length()-2),buf2.length()); }else if (a==2){ buf2.delete((buf2.length()-4),buf2.length()); } } } }