Des类,java代码(八)

2014-11-24 10:38:38 · 作者: · 浏览: 3
// c = ((b1 & 0x0F) << 2) | ((b2 & 0xC0) >>> 6);
c = ((b1 & 0x0F) << 2) | ((b2 & 0xC0) >> 6);
if (notleading || c != 0) {
returnValue += BASE64_CHARSET[c];
notleading = true;
}
c = b2 & 0x3F;
if (notleading || c != 0) {
returnValue += BASE64_CHARSET[c];
notleading = true;
}
if (pos >= len) {
break;
} else {
try {
b0 = buffer[pos++];
b1 = buffer[pos++];
b2 = buffer[pos++];
} catch (Exception x) {
break;
}
}
} while (true);

if (notleading) {
return returnValue;
}
return "0";
}

/**
* 字符串转换为字节数组
*
* @param str
* @return
* @throws Exception
*/
private byte[] fromBase64(String str) throws Exception {
int len = str.length();
if (len == 0) {
throw new Exception("Empty string");
}
byte[] a = new byte[len + 1];
int i, j;
for (i = 0; i < len; i++) {
try {
a[i] = (byte) BASE64_CHARS.indexOf(str.charAt(i));
} catch (Exception x) {
throw new Exception("Illegal character at #" + i);
}
}
i = len - 1;
j = len;
try {
while (true) {
a[j] = a[i];
if (--i < 0) {
break;
}
a[j] |= (a[i] & 0x03) << 6;
j--;
// a[j] = (byte)((a[i] & 0x3C) >>> 2);
a[j] = (byte) ((a[i] & 0x3C) >> 2);
if (--i < 0) {
break;
}
a[j] |= (a[i] & 0x0F) << 4;
j--;
// a[j] = (byte)((a[i] & 0x30) >>> 4);
a[j] = (byte) ((a[i] & 0x30) >> 4);
if (--i < 0) {
break;
}
a[j] |= (a[i] << 2);
j--;
a[j] = 0;
if (--i < 0) {
break;
}
}
} catch (Exception ignored) {
}

try { // ignore leading 0-bytes
while (a[j] == 0) {
j++;
}
} catch (Exception x) {
return new byte[1]; // one 0-byte
}
byte[] result = new byte[len - j + 1];
arraycopy(a, j, result, 0, len - j + 1);
return result;
}

public byte[] eCode(byte[] bytes){
DesEncrypt(key.getBytes(), bytes, 1);
return new byte [0];
}
}