Search results for 'base64'

JavaScript Base64 Encode / Decode

// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins --
http://rumkin.com

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
  
   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}
function encode64Han(str) {
  return encode64(escape(str))
}
function decode64Han(str) {
  return unescape(decode64(str))
}

2007/10/12 20:47 2007/10/12 20:47
Trackback Address:이 글에는 트랙백을 보낼 수 없습니다

JAVA Base64 Encoding / Decoding

2007/10/12 20:44

서비 JAVA ,

//Ok how about this pure Java API solution.
//Yours for free free free, no unintened theft.
//Completely portable per the Java promise.
//This code sets no preferences! The class just extends AbstractPreferences to get access to the put and get and over rides them.
//The super still calls the Base64 to do the encoding and decoding.
//This extension intercepts the encoded/decoded strings and makes them visible.
//TaDa Java API supports, has, allows, does base64 encoding, for the clever anyway.

package yourpackage;
public class YourPreferences extends java.util.prefs.AbstractPreferences {
//keep the key state
    private java.util.Hashtable encodedStore = new java.util.Hashtable();
   
    /** Creates a new instance of YourPreferences*/
    public YourPreferences(java.util.prefs.AbstractPreferences prefs, java.lang.String string) {
 super(prefs, string);
    }
//main acts as a driver i.e. used to test this class not required by this class
    public static void main(java.lang.String[] args ){
 java.util.prefs.AbstractPreferences myprefs = new yourpackage.YourPreferences(null, "");
 
 java.lang.String stringToEncode = "Aladdin:open sesame";
 java.lang.String key = "Aladdin:open sesame";
 java.lang.String key_ = "KEYisNOTtheSAMEasTHEstring";
 try{
     java.lang.String encoded = ((yourpackage.YourPreferences)myprefs).encodeBase64(stringToEncode);
     java.lang.System.out.println("ENCODED STRING TO: " + encoded);
     java.lang.String base64 = (java.lang.String)((yourpackage.YourPreferences)myprefs).encodedStore.get(key);
     java.lang.String decoded = ((yourpackage.YourPreferences)myprefs).decodeBase64("newkey",base64);
     java.lang.System.out.println("DECODED STRING TO: " + decoded);
    
     java.lang.String encoded_ = ((yourpackage.YourPreferences)myprefs).encodeBase64(key_,"ALONGSTRANGESTRINGTHATMAKESNOSENCEATALLBUTCANBEENCODEDANYWAYREGARDLESS");
     java.lang.System.out.println("ENCODED STRING TO: " + encoded_);
     java.lang.String base64_ = (java.lang.String)((yourpackage.YourPreferences)myprefs).encodedStore.get(key_);
     java.lang.String decoded_ = ((yourpackage.YourPreferences)myprefs).decodeBase64(key_,base64_);
     java.lang.System.out.println("DECODED STRING TO: " + decoded_);
     java.lang.System.out.println("\n");
     java.util.Enumeration enum = ((yourpackage.YourPreferences)myprefs).encodedStore.keys();
     java.lang.String enumKey = null;
     while(enum.hasMoreElements()){
  enumKey = (java.lang.String)enum.nextElement();
  java.lang.System.out.print(enumKey);
  java.lang.System.out.println(" : " +((yourpackage.YourPreferences)myprefs).encodedStore.get(enumKey).toString());
     }
 }catch(java.io.UnsupportedEncodingException uee){
     uee.printStackTrace();
 }catch(java.io.IOException ioe){
     ioe.printStackTrace();
 }
    }
    public java.lang.String encodeBase64(java.lang.String key, java.lang.String raw)throws java.io.UnsupportedEncodingException{
 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
 java.io.PrintWriter pw = new java.io.PrintWriter(
     new java.io.OutputStreamWriter(baos,"UTF8"));
 pw.write(raw.toCharArray());
 pw.flush();//ya know
 byte[] rawUTF8 = baos.toByteArray();
 this.putByteArray(key, rawUTF8);
 
 return (java.lang.String)this.encodedStore.get(key);
    }
    public java.lang.String encodeBase64(java.lang.String raw)throws java.io.UnsupportedEncodingException{
 
 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
 java.io.PrintWriter pw = new java.io.PrintWriter(
     new java.io.OutputStreamWriter(baos,"UTF8"));
 pw.write(raw.toCharArray());
 pw.flush();//ya know
 byte[] rawUTF8 = baos.toByteArray();
 this.putByteArray(raw, rawUTF8);
 
 return (java.lang.String)this.encodedStore.get(raw);
    }
    public java.lang.String decodeBase64(java.lang.String key, java.lang.String base64String)
    throws java.io.UnsupportedEncodingException, java.io.IOException{
 byte[] def = {(byte)'D',(byte)'E',(byte)'F'};//not used at any point
 this.encodedStore.put(key,base64String);
 char[] platformChars = null;
 byte[] byteResults = null;
 byteResults = this.getByteArray(key, def);
 java.io.InputStreamReader isr = new java.io.InputStreamReader(
     new java.io.ByteArrayInputStream(byteResults),"UTF8");
 platformChars = new char[byteResults.length];
 isr.read(platformChars);
 
 return new java.lang.String(platformChars);
    }
//intercept key lookup and return our own base64 encoded string to super
    public String get(String key, String def) {
 return (java.lang.String)this.encodedStore.get(key);
    }
//intercepts put captures the base64 encoded string and returns it
    public void put(String key, String value){
 this.encodedStore.put(key, value);//save the encoded string
    }
//dummy implementation as AbstractPreferences is extended to get acces to protected
//methods and to overide put(String,String) and get(String,String)
    protected java.util.prefs.AbstractPreferences childSpi(String name) {return null;}
    protected String[] childrenNamesSpi() throws java.util.prefs.BackingStoreException {return null;}
    protected void flushSpi() throws java.util.prefs.BackingStoreException {}
    protected String getSpi(String key) {return null;}
    protected String[] keysSpi() throws java.util.prefs.BackingStoreException {return null;}
    protected void putSpi(String key, String value) {}
    protected void removeNodeSpi() throws java.util.prefs.BackingStoreException {}
    protected void removeSpi(String key) {}
    protected void syncSpi() throws java.util.prefs.BackingStoreException {}
}

2007/10/12 20:44 2007/10/12 20:44
Trackback Address:이 글에는 트랙백을 보낼 수 없습니다