I am using NetBeans 7.0.1 and JDK 1.6 Update 24 and when importing the package com.sun.org.apache.xml.internal.security.utils.Base64
to encode a password hash:<
With org.apache.commons.codec.binary.Base64
use the specific method (such as Base64.encodeBase64String()
for example) to replace Base64.encode
com.sun.* classes are not part of the Java API, and you shouldn't be relying on them. I would suggest using Apache Commons Codec to do Base64 encoding instead.
As Greg pointed out in a comment to the previously accepted answer:
Note from the future: in Java 8, there is a
java.util.Base64
package.
Since it is now implemented as a part of the standard Java API, it is probably preferable to use it instead of Apache Commons Codec. With this in mind I think this should be posted as an answer.
The API has a number of methods in the class Base64 to create a Decoder or Encoder. The following types of encoders and decoders are supported:
Basic - Uses "The Base64 Alphabet" as specified in Table 1 of RFC 4648 and RFC 2045 for encoding and decoding operation. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.
URL and Filename safe - Uses the "URL and Filename safe Base64 Alphabet" as specified in Table 2 of RFC 4648 for encoding and decoding. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.
MIME - Uses the "The Base64 Alphabet" as specified in Table 1 of RFC 2045 for encoding and decoding operation. The encoded output must be represented in lines of no more than 76 characters each and uses a carriage return '\r' followed immediately by a linefeed '\n' as the line separator. No line separator is added to the end of the encoded output. All line separators or other characters not found in the base64 alphabet table are ignored in decoding operation.
So, for example, this is how to create a basic encoder and encode a byte
array:
byte[] encodedArray = Base64.getEncoder().encode(someArrayToEncode);