Blog

  • Today I am going to explain Basic Encryption using java Base64 classes.you can also check my last two Articles which explain that how to encrypt the URL and MIME encryption. In this Article, I am going to focus on basic encoding and decoding in java.if You want to know Basic details of Base64 then open this link.

    Basically, it uses Java specified Base64 alphabets in RFC 4648 and RFC 2045 while encoding and decoding operations. Encoder never allows any line separator while encoding and also decoder check the character that if any character belongs outside the  base64 alphabets, it rejected. 

     

    Nested Classes of BASE64.

    1.Base64.Decoder: Implements a decoder for decoding byte data.
    2.Base64.Encoder: Implements an encoder for encoding byte data.

    Methods using for Encoding and Decoding.

    1. public static Base64.Encoder getEncoder()  :Using for encoding and return type of this method is Base64.

    2. public static Base64.Decoder getDecoder()  :Using for decode and return type is  BASE64.

    Base64.Decoder Methods
    1. public byte[] decode(byte[] src): This method used for decoding all bytes from the input byte array, writing the results into a newly-allocated output byte array. The returned the length of the resulting bytes.

    2. public byte[] decode(String src): This method used for decoding a Base64 encoded String into a newly-allocated byte array.


    Base64.Encoder Methods
    1.  byte[] encode(byte[] source): Encoding all bytes from the byte array into a new-allocated byte array. The size of the byte array is the length of the resulting bytes.
    2.  int encode(byte[] source, byte[] destin): Encoding all bytes from the byte array using the Base64.

    Example: Basic Encoding and Decoding

    import java.util.Base64;  
    publicclass BasicEncryptionExample {  
          public static void main(String[] args) {  
            // Getting encoder  
            Base64.Encoder encoder = Base64.getEncoder();  
            // Creating byte array  
            byte byteArr[] = {10,20};  
            // encoding byte array  
            byte byteArr2[] = encoder.encode(byteArr);  
            System.out.println("Encoded byte array: "+byteArr2);  
            byte byteArr3[] = newbyte[5];                // Make sure it has enough size to store copied bytes  
            int x = encoder.encode(byteArr,byteArr3);    // Returns number of bytes written  
            System.out.println("Encoded byte array written to another array: "+byteArr3);  
            System.out.println("Number of bytes written: "+x);  
          
            // Encoding string  
            String str = encoder.encodeToString("Oodles technologies".getBytes());  
            System.out.println("Encoded string: "+str);  
            // Getting decoder  
            Base64.Decoder decoder = Base64.getDecoder();  
            // Decoding string  
            String dStr = new String(decoder.decode(str));  
            System.out.println("Decoded string: "+dStr);  
        }  
    }        
    

    output:

      
    Encoded byte array: [B@6bc7c054
    Encoded byte array written to another array: [B@232204a1
    Number of bytes written: 4
    Encoded string: SmF2YVRwb2ludA==
    Decoded string: Oodles technologies
       

    Hope so this content will help you.

Tags: Java

Mobile Applications

Video Content

Bigdata & NoSQL

SaaS Applications

Miscellaneous

Archives


Alexa Certified Site Stats for www.oodlestechnologies.com