Re: Simple encryption/decryption
Lew wrote:
rossum wrote:
Base-64 is not part of standard Java, though there is an
undocumented version in sun.misc. There are a lot of implementations
floating about on the internet; I use Bouncy Castle, which has Base-64
as part of their general crypto package.
Apache commons-codec has it also. Besides, as
Arne Vajh??j wrote:
Base64 is part of the Java EE spec. It is under the mail stuff.
<http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeUtility.html#decode(java.io.InputStream,%20java.lang.String)>
<http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeUtility.html#encode(java.io.OutputStream,%20java.lang.String)>
The code is simple:
public static String b64encode(byte[] b) throws MessagingException,
IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream b64os = MimeUtility.encode(baos, "base64");
b64os.write(b);
b64os.close();
return new String(baos.toByteArray());
}
public static byte[] b64decode(String s) throws
MessagingException, IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
InputStream b64is = MimeUtility.decode(bais, "Base64");
byte[] tmp = new byte[s.length()];
int n = b64is.read(tmp);
byte[] res = new byte[n];
System.arraycopy(tmp, 0, res, 0, n);
return res;
}
and if it is in Java EE context the classes are there - if it is in
pure Java SE context then getting JavaMail and put that jar in classpath
is enough.
Arne