(byte)225, (byte)248, (byte)152, (byte)17, (byte)105, (byte)217, (byte)142, (byte)148,
(byte)155, (byte)30, (byte)135, (byte)233, (byte)206, (byte)85, (byte)40, (byte)223,
(byte)140, (byte)161, (byte)137, (byte)13, (byte)191, (byte)230, (byte)66, (byte)104,
- (byte)65, (byte)153, (byte)45, (byte)15, (byte)176, (byte)84, (byte)187, (byte)22,
+ (byte)65, (byte)153, (byte)45, (byte)15, (byte)176, (byte)84, (byte)187, (byte)22
};
// The inverse S-box
(byte)160, (byte)224, (byte)59, (byte)77, (byte)174, (byte)42, (byte)245, (byte)176,
(byte)200, (byte)235, (byte)187, (byte)60, (byte)131, (byte)83, (byte)153, (byte)97,
(byte)23, (byte)43, (byte)4, (byte)126, (byte)186, (byte)119, (byte)214, (byte)38,
- (byte)225, (byte)105, (byte)20, (byte)99, (byte)85, (byte)33, (byte)12, (byte)125,
+ (byte)225, (byte)105, (byte)20, (byte)99, (byte)85, (byte)33, (byte)12, (byte)125
};
// vector used in calculating key schedule (powers of x in GF(256))
*/
package org.ibex.crypto;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
public final class Base64 {
- private Base64() { /* can't be instansiated */ }
-
- private static final char encodeMap[] = {
+ private Base64() { /* can't be instansiated */ }
+
+ private static final char[] encodeMap = {
'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'4','5','6','7','8','9','+','/'
};
- private static final int decodeMap[] = {
+ private static final int[] decodeMap = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1
};
- public static void main(String[] args){
+ public static void main(String[] args) throws IOException {
if(args.length == 2 && args[0].equals("encode"))
- System.out.println(encode(args[1].getBytes()));
+ System.out.println(encode(getBytes(args[1])));
else if(args.length == 2 && args[0].equals("decode"))
- System.out.println(new String(decode(args[1])));
- else {
- System.out.println("Usage: Base64 {encode,decode} text");
+ System.out.write(decode(args[1]));
+ else if(args.length == 2 && args[0].equals("encodefile")) {
+ FileInputStream fis = new FileInputStream(args[1]);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ byte[] buf = new byte[4096];
+ int n;
+ while((n = fis.read(buf)) != -1) baos.write(buf,0,n);
+ fis.close();
+ System.out.println(encode(baos.toByteArray()));
+ } else {
+ System.out.println("Usage: Base64 {encode,decode} filename");
System.exit(1);
}
System.exit(0);
- }
+ }
- public static byte[] decode(String inString){
+ public static byte[] decode(String inString){
char[] in = inString.toCharArray();
int part=0;
int theBytes = 0;
}
}
- public static String encode(String in) { return encode(getBytes(in)); }
+ public static String encode(String in) { return encode(getBytes(in)); }
+
public static String encode(byte[] in){
int part=0;
int theBytes = 0;
char[] out = new char[(in.length / 3 + 1) * 4];
int outPos = 0;
for(int i=0;i<in.length;i++){
- theBytes = (theBytes << 8) | in[i];
+ theBytes = (theBytes << 8) | (in[i]&0xff);
part++;
if(part == 3){
part = 0;