一、前言
关于dnsjava-2.1.2中实现base的转换加密操作处理,其中org.xbill.DNS.utils.base16、org.xbill.DNS.utils.base32、org.xbill.DNS.utils.base64分别对基于同步位数的加密类型进行二进制和字符串互转等,具体参见源码说明。
二、源码说明
1.base16
package org.xbill.DNS.utils;@b@@b@import java.io.ByteArrayOutputStream;@b@import java.io.DataOutputStream;@b@import java.io.IOException;@b@@b@public class base16@b@{@b@ private static final String Base16 = "0123456789ABCDEF";@b@@b@ public static String toString(byte[] b)@b@ {@b@ ByteArrayOutputStream os = new ByteArrayOutputStream();@b@@b@ for (int i = 0; i < b.length; ++i) {@b@ short value = (short)(b[i] & 0xFF);@b@ byte high = (byte)(value >> 4);@b@ byte low = (byte)(value & 0xF);@b@ os.write("0123456789ABCDEF".charAt(high));@b@ os.write("0123456789ABCDEF".charAt(low));@b@ }@b@ return new String(os.toByteArray());@b@ }@b@@b@ public static byte[] fromString(String str)@b@ {@b@ ByteArrayOutputStream bs = new ByteArrayOutputStream();@b@ byte[] raw = str.getBytes();@b@ for (int i = 0; i < raw.length; ++i)@b@ if (!(Character.isWhitespace((char)raw[i])))@b@ bs.write(raw[i]);@b@@b@ byte[] in = bs.toByteArray();@b@ if (in.length % 2 != 0) {@b@ return null;@b@ }@b@@b@ bs.reset();@b@ DataOutputStream ds = new DataOutputStream(bs);@b@@b@ for (int i = 0; i < in.length; i += 2) {@b@ byte high = (byte)"0123456789ABCDEF".indexOf(Character.toUpperCase((char)in[i]));@b@ byte low = (byte)"0123456789ABCDEF".indexOf(Character.toUpperCase((char)in[(i + 1)]));@b@ try {@b@ ds.writeByte((high << 4) + low);@b@ }@b@ catch (IOException e) {@b@ }@b@ }@b@ return bs.toByteArray();@b@ }@b@}
2.base32
package org.xbill.DNS.utils;@b@@b@import java.io.ByteArrayOutputStream;@b@import java.io.DataOutputStream;@b@import java.io.IOException;@b@@b@public class base32@b@{@b@ private String alphabet;@b@ private boolean padding;@b@ private boolean lowercase;@b@@b@ public base32(String alphabet, boolean padding, boolean lowercase)@b@ {@b@ this.alphabet = alphabet;@b@ this.padding = padding;@b@ this.lowercase = lowercase;@b@ }@b@@b@ private static int blockLenToPadding(int blocklen)@b@ {@b@ switch (blocklen)@b@ {@b@ case 1:@b@ return 6;@b@ case 2:@b@ return 4;@b@ case 3:@b@ return 3;@b@ case 4:@b@ return 1;@b@ case 5:@b@ return 0;@b@ }@b@ return -1;@b@ }@b@@b@ private static int paddingToBlockLen(int padlen)@b@ {@b@ switch (padlen) { case 6:@b@ return 1;@b@ case 4:@b@ return 2;@b@ case 3:@b@ return 3;@b@ case 1:@b@ return 4;@b@ case 0:@b@ return 5;@b@ case 2:@b@ case 5: }@b@ return -1;@b@ }@b@@b@ public String toString(byte[] b)@b@ {@b@ ByteArrayOutputStream os = new ByteArrayOutputStream();@b@@b@ for (int i = 0; i < (b.length + 4) / 5; ++i) {@b@ short[] s = new short[5];@b@ int[] t = new int[8];@b@@b@ int blocklen = 5;@b@ for (int j = 0; j < 5; ++j)@b@ if (i * 5 + j < b.length) {@b@ s[j] = (short)(b[(i * 5 + j)] & 0xFF);@b@ } else {@b@ s[j] = 0;@b@ --blocklen;@b@ }@b@@b@ int padlen = blockLenToPadding(blocklen);@b@@b@ t[0] = (byte)(s[0] >> 3 & 0x1F);@b@@b@ t[1] = (byte)((s[0] & 0x7) << 2 | s[1] >> 6 & 0x3);@b@@b@ t[2] = (byte)(s[1] >> 1 & 0x1F);@b@@b@ t[3] = (byte)((s[1] & 0x1) << 4 | s[2] >> 4 & 0xF);@b@@b@ t[4] = (byte)((s[2] & 0xF) << 1 | s[3] >> 7 & 0x1);@b@@b@ t[5] = (byte)(s[3] >> 2 & 0x1F);@b@@b@ t[6] = (byte)((s[3] & 0x3) << 3 | s[4] >> 5 & 0x7);@b@@b@ t[7] = (byte)(s[4] & 0x1F);@b@@b@ for (int j = 0; j < t.length - padlen; ++j) {@b@ char c = this.alphabet.charAt(t[j]);@b@ if (this.lowercase)@b@ c = Character.toLowerCase(c);@b@ os.write(c);@b@ }@b@@b@ if (this.padding)@b@ for (j = t.length - padlen; j < t.length; ++j)@b@ os.write(61);@b@@b@ }@b@@b@ return new String(os.toByteArray());@b@ }@b@@b@ public byte[] fromString(String str)@b@ {@b@ ByteArrayOutputStream bs = new ByteArrayOutputStream();@b@ byte[] raw = str.getBytes();@b@ for (int i = 0; i < raw.length; ++i)@b@ {@b@ char c = (char)raw[i];@b@ if (!(Character.isWhitespace(c))) {@b@ c = Character.toUpperCase(c);@b@ bs.write((byte)c);@b@ }@b@ }@b@@b@ if (this.padding) {@b@ if (bs.size() % 8 == 0) break label96;@b@ return null;@b@ }@b@ while (bs.size() % 8 != 0) {@b@ bs.write(61);@b@ }@b@@b@ label96: byte[] in = bs.toByteArray();@b@@b@ bs.reset();@b@ DataOutputStream ds = new DataOutputStream(bs);@b@@b@ for (int i = 0; i < in.length / 8; ++i) {@b@ int j;@b@ short[] s = new short[8];@b@ int[] t = new int[5];@b@@b@ int padlen = 8;@b@ for (int j = 0; j < 8; ++j) {@b@ char c = (char)in[(i * 8 + j)];@b@ if (c == '=')@b@ break;@b@ s[j] = (short)this.alphabet.indexOf(in[(i * 8 + j)]);@b@ if (s[j] < 0)@b@ return null;@b@ --padlen;@b@ }@b@ int blocklen = paddingToBlockLen(padlen);@b@ if (blocklen < 0) {@b@ return null;@b@ }@b@@b@ t[0] = (s[0] << 3 | s[1] >> 2);@b@@b@ t[1] = ((s[1] & 0x3) << 6 | s[2] << 1 | s[3] >> 4);@b@@b@ t[2] = ((s[3] & 0xF) << 4 | s[4] >> 1 & 0xF);@b@@b@ t[3] = (s[4] << 7 | s[5] << 2 | s[6] >> 3);@b@@b@ t[4] = ((s[6] & 0x7) << 5 | s[7]);@b@ try@b@ {@b@ for (j = 0; j < blocklen; ++j)@b@ ds.writeByte((byte)(t[j] & 0xFF));@b@ }@b@ catch (IOException e)@b@ {@b@ }@b@ }@b@ return bs.toByteArray();@b@ }@b@@b@ public static class Alphabet@b@ {@b@ public static final String BASE32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";@b@ public static final String BASE32HEX = "0123456789ABCDEFGHIJKLMNOPQRSTUV=";@b@ }@b@}
3.base64
package org.xbill.DNS.utils;@b@@b@import java.io.ByteArrayOutputStream;@b@import java.io.DataOutputStream;@b@import java.io.IOException;@b@@b@public class base64@b@{@b@ private static final String Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";@b@@b@ public static String toString(byte[] b)@b@ {@b@ ByteArrayOutputStream os = new ByteArrayOutputStream();@b@@b@ for (int i = 0; i < (b.length + 2) / 3; ++i) {@b@ short[] s = new short[3];@b@ short[] t = new short[4];@b@ for (int j = 0; j < 3; ++j)@b@ if (i * 3 + j < b.length)@b@ s[j] = (short)(b[(i * 3 + j)] & 0xFF);@b@ else@b@ s[j] = -1;@b@@b@@b@ t[0] = (short)(s[0] >> 2);@b@ if (s[1] == -1)@b@ t[1] = (short)((s[0] & 0x3) << 4);@b@ else@b@ t[1] = (short)(((s[0] & 0x3) << 4) + (s[1] >> 4));@b@ if (s[1] == -1) {@b@ t[2] = (t[3] = 64);@b@ } else if (s[2] == -1) {@b@ t[2] = (short)((s[1] & 0xF) << 2);@b@ t[3] = 64;@b@ }@b@ else {@b@ t[2] = (short)(((s[1] & 0xF) << 2) + (s[2] >> 6));@b@ t[3] = (short)(s[2] & 0x3F);@b@ }@b@ for (j = 0; j < 4; ++j)@b@ os.write("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(t[j]));@b@ }@b@ return new String(os.toByteArray());@b@ }@b@@b@ public static String formatString(byte[] b, int lineLength, String prefix, boolean addClose)@b@ {@b@ String s = toString(b);@b@ StringBuffer sb = new StringBuffer();@b@ for (int i = 0; i < s.length(); i += lineLength) {@b@ sb.append(prefix);@b@ if (i + lineLength >= s.length()) {@b@ sb.append(s.substring(i));@b@ if (addClose)@b@ sb.append(" )");@b@ }@b@ else {@b@ sb.append(s.substring(i, i + lineLength));@b@ sb.append("\n");@b@ }@b@ }@b@ return sb.toString();@b@ }@b@@b@ public static byte[] fromString(String str)@b@ {@b@ ByteArrayOutputStream bs = new ByteArrayOutputStream();@b@ byte[] raw = str.getBytes();@b@ for (int i = 0; i < raw.length; ++i)@b@ if (!(Character.isWhitespace((char)raw[i])))@b@ bs.write(raw[i]);@b@@b@ byte[] in = bs.toByteArray();@b@ if (in.length % 4 != 0) {@b@ return null;@b@ }@b@@b@ bs.reset();@b@ DataOutputStream ds = new DataOutputStream(bs);@b@@b@ for (int i = 0; i < (in.length + 3) / 4; ++i) {@b@ short[] s = new short[4];@b@ short[] t = new short[3];@b@@b@ for (int j = 0; j < 4; ++j)@b@ s[j] = (short)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(in[(i * 4 + j)]);@b@@b@ t[0] = (short)((s[0] << 2) + (s[1] >> 4));@b@ if (s[2] == 64) {@b@ t[1] = (t[2] = -1);@b@ if ((s[1] & 0xF) == 0) break label270;@b@ return null;@b@ }@b@ if (s[3] == 64) {@b@ t[1] = (short)((s[1] << 4) + (s[2] >> 2) & 0xFF);@b@ t[2] = -1;@b@ if ((s[2] & 0x3) == 0) break label270;@b@ return null;@b@ }@b@@b@ t[1] = (short)((s[1] << 4) + (s[2] >> 2) & 0xFF);@b@ t[2] = (short)((s[2] << 6) + s[3] & 0xFF);@b@ try@b@ {@b@ for (j = 0; j < 3; ++j)@b@ if (t[j] >= 0)@b@ ds.writeByte(t[j]);@b@ }@b@ catch (IOException e) {@b@ }@b@ }@b@ label270: return bs.toByteArray();@b@ }@b@}