ice.cryptix
Class Base64

java.lang.Object
  extended byice.cryptix.Base64

public class Base64
extends Object

This class provides methods for encoding and decoding data in MIME base64 format.

The input to the encode methods is always a byte array. Strictly speaking the output represents a sequence of characters, but since these characters are from a subset of both the Unicode and ASCII character repertoires, it is possible to express the output either as a String or as a byte array.

References:

  1. RFC 2045, Multipurpose Internet Mail Extensions (MIME) Part One, Format of Internet Message Bodies, "Section 6.8 Base64 Content-Transfer-Encoding," http://www.imc.org/rfc2045

Copyright © 1995-1997 Systemics Ltd on behalf of the Cryptix Development Team.
All rights reserved.

Copied and modified from cryptix.util.mime.Base64.java for use in ICEsecure.

$Revision: 1.2 $

Author:
Jill Baker, David Hopwood

Field Summary
protected static byte[] dec_table
          A static array that maps ASCII code points to a 6-bit integer, or -1 for an invalid code point.
protected static char[] enc_table
          A static array that maps 6-bit integers to a specific char.
 
Constructor Summary
Base64()
           
 
Method Summary
static byte[] decode(byte[] data)
          Decodes a byte array containing base64-encoded ASCII.
static byte[] decode(String msg)
          Decodes a base64-encoded String.
static String encode(byte[] data)
          Encodes data as a String using base64 encoding.
static String encode(byte[] data, int linelength)
          Encodes data as a String using base64 encoding.
static byte[] encodeAsByteArray(byte[] data)
          Encodes data as a byte array using base64 encoding.
static byte[] encodeAsByteArray(byte[] data, int linelength)
          Encodes data as a byte array using base64 encoding.
static byte[] getNext(byte[] encoded)
          Returns the next base64 sequence in case of a concatenation of several base64 encoded objects.
static boolean isBase64(byte[] encoded)
          Returns true if the byte array starts with the isoLatin1 characters : "-----BEGIN", preceded or not by whitespaces.
static byte[] isoLatin1ToByteArray(String s)
          Converts a String to an ISO-Latin-1 encoded byte array.
static String isoLatin1ToString(byte[] b)
          Converts an ISO-Latin-1 encoded byte array to a String.
static byte[] removeMarkers(byte[] encoded)
          Returns the first base64 encoded data bloc, removing the markers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

enc_table

protected static final char[] enc_table
A static array that maps 6-bit integers to a specific char.


dec_table

protected static final byte[] dec_table
A static array that maps ASCII code points to a 6-bit integer, or -1 for an invalid code point.

Constructor Detail

Base64

public Base64()
Method Detail

encode

public static String encode(byte[] data)
Encodes data as a String using base64 encoding. Line breaks in the output are represented as CR LF.

Returns:
the encoded string.

encode

public static String encode(byte[] data,
                            int linelength)
Encodes data as a String using base64 encoding. Line breaks in the output are represented as CR LF.

Returns:
the encoded string.

encodeAsByteArray

public static byte[] encodeAsByteArray(byte[] data)
Encodes data as a byte array using base64 encoding. The characters 'A'-'Z', 'a'-'z', '0'-'9', '+', '/', and '=' in the output are mapped to their ASCII code points. Line breaks in the output are represented as CR LF (codes 13 and 10).

Returns:
the encoded byte array.
Since:
Cryptix 2.2.2

encodeAsByteArray

public static byte[] encodeAsByteArray(byte[] data,
                                       int linelength)
Encodes data as a byte array using base64 encoding. The characters 'A'-'Z', 'a'-'z', '0'-'9', '+', '/', and '=' in the output are mapped to their ASCII code points. Line breaks in the output are represented as CR LF (codes 13 and 10).

Returns:
the encoded byte array.
Since:
Cryptix 2.2.2

decode

public static byte[] decode(byte[] data)
Decodes a byte array containing base64-encoded ASCII. Characters with ASCII code points <= 32 (this includes whitespace and newlines) are ignored.

Returns:
the decoded data.
Throws:
IllegalArgumentException - if data contains invalid characters, i.e. not codes 0-32, 'A'-'Z', 'a'-'z', '+', '/'. or '=', or is incorrectly padded.

decode

public static byte[] decode(String msg)
                     throws IllegalArgumentException
Decodes a base64-encoded String. Characters with ASCII code points <= 32 (this includes whitespace and newlines) are ignored.

Returns:
the decoded data.
Throws:
IllegalArgumentException - if data contains invalid characters, i.e. not codes 0-32, 'A'-'Z', 'a'-'z', '+', '/'. or '=', or is incorrectly padded.

isoLatin1ToString

public static String isoLatin1ToString(byte[] b)
Converts an ISO-Latin-1 encoded byte array to a String.

This conversion function is neccessary because Java 1.1+ uses machine dependent char-to-byte and byte-to-char conversions. This is normally a useful feature, allowing machine independence. In order to maintain backwards compatibility with pre-Java conventions, we implement our own machine-independent deterministic converter.

Copied and modified from cryptix.util.mime.ISOLatin1.java

Parameters:
b - an array of bytes to be converted to a String.
Returns:
a String containing the bytes. Each char in the string will be in the range \u0000 to \u00FF.
Since:
Cryptix 2.2.2

isoLatin1ToByteArray

public static byte[] isoLatin1ToByteArray(String s)
Converts a String to an ISO-Latin-1 encoded byte array.

This conversion function is neccessary because Java 1.1+ uses machine dependent char-to-byte and byte-to-char conversions. This is normally a useful feature, allowing machine independence. In order to maintain backwards compatibility with pre-Java conventions, we implement our own machine-independent deterministic converter.

Copied and modified from cryptix.util.mime.ISOLatin1.java

Parameters:
s - the string to be converted to bytes.
Returns:
an array of bytes containing the lower 8 bits of each char.
Throws:
IllegalArgumentException - if any of the characters in s are out of range.
Since:
Cryptix 2.2.2

getNext

public static byte[] getNext(byte[] encoded)
Returns the next base64 sequence in case of a concatenation of several base64 encoded objects.

Created for ICEsecure


removeMarkers

public static byte[] removeMarkers(byte[] encoded)
Returns the first base64 encoded data bloc, removing the markers. Markers are the "-----BEGIN XXX-----" and "----END XXX----" lines.

Created for ICEsecure


isBase64

public static boolean isBase64(byte[] encoded)
Returns true if the byte array starts with the isoLatin1 characters : "-----BEGIN", preceded or not by whitespaces.

Created for ICEsecure