本人有如下代码进行加密并对加密后的字符解密,加密能行,但解密的结果为空,错在那里,请高手指点!
public class TEST {
private static byte[] cvtTable = {
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E',
(byte)'F', (byte)'G', (byte)'H', (byte)'I', (byte)'J',
(byte)'K', (byte)'L', (byte)'M', (byte)'N', (byte)'O',
(byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T',
(byte)'U', (byte)'V', (byte)'W', (byte)'X', (byte)'Y',
(byte)'Z',
(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e',
(byte)'f', (byte)'g', (byte)'h', (byte)'i', (byte)'j',
(byte)'k', (byte)'l', (byte)'m', (byte)'n', (byte)'o',
(byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t',
(byte)'u', (byte)'v', (byte)'w', (byte)'x', (byte)'y',
(byte)'z',
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4',
(byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9',
(byte)'+', (byte)'/'};
public static String encode(String content){
byte input[] = (content).getBytes();
byte[] output;
//System.out.println("input[] length = "+input.length);
if(input.length%3 == 0) {
//System.out.println("xxxxx");
output = new byte[input.length/3*4];
}
else
output = new byte[((input.length / 3) + 1) * 4];
int ridx = 0;
int chunk = 0; for (int i = 0; i < input.length; i += 3) {
int left = input.length - i;
if (left > 2) {
chunk = (input[i] << 16)|
(input[i + 1] << 8) |
input[i + 2];
output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
output[ridx++] = cvtTable[(chunk&0xFC0) >> 6];
output[ridx++] = cvtTable[(chunk&0x3F)];
} else if (left == 2) {
chunk = (input[i] << 16) |
(input[i + 1] << 8);
output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
output[ridx++] = cvtTable[(chunk&0xFC0) >> 6];
output[ridx++] = cvtTable[63];
} else if(left == 1){
chunk = input[i] << 16;
output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
output[ridx++] = cvtTable[63];
output[ridx++] = cvtTable[63];
}
}
return new String(output);
}
static String revert(String content) {
byte tempArray[] = (content).getBytes();
byte[] result = new byte[tempArray.length/4*3];
int temp = 0;
int pos = 0; //for(int i= tempArray.length-1;i>= 0;i--)
//System.out.println(tempArray[i]);
for(int i = 0;i < tempArray.length;i += 4) { temp = (search(tempArray[i]) << 18) |
(search(tempArray[i+1]) << 12) |
(search(tempArray[i+2]) << 6) |
(search(tempArray[i+3]));
result[pos++] = (byte)((temp & 0xff0000) >> 16);
result[pos++] = (byte)((temp & 0xff00) >> 8);
result[pos++] = (byte)(temp & 0xff);
}
return new String(result);
}
static int search(int target) {
int i = 0;
while(target != cvtTable[i] && i < 64) {
i++;
}
return i;
}
public static void main(String args[]){
try{
String str = encode("ABCD");
System.out.println("befor="+str); ////这里能加密
System.out.println("after="+revert(str)); //但这里不能解密出“ABCD”
}catch(Exception ex){
ex.printStackTrace();
}
}
}盼望加解密高手指点!!
public class TEST {
private static byte[] cvtTable = {
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E',
(byte)'F', (byte)'G', (byte)'H', (byte)'I', (byte)'J',
(byte)'K', (byte)'L', (byte)'M', (byte)'N', (byte)'O',
(byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T',
(byte)'U', (byte)'V', (byte)'W', (byte)'X', (byte)'Y',
(byte)'Z',
(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e',
(byte)'f', (byte)'g', (byte)'h', (byte)'i', (byte)'j',
(byte)'k', (byte)'l', (byte)'m', (byte)'n', (byte)'o',
(byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t',
(byte)'u', (byte)'v', (byte)'w', (byte)'x', (byte)'y',
(byte)'z',
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4',
(byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9',
(byte)'+', (byte)'/'};
public static String encode(String content){
byte input[] = (content).getBytes();
byte[] output;
//System.out.println("input[] length = "+input.length);
if(input.length%3 == 0) {
//System.out.println("xxxxx");
output = new byte[input.length/3*4];
}
else
output = new byte[((input.length / 3) + 1) * 4];
int ridx = 0;
int chunk = 0; for (int i = 0; i < input.length; i += 3) {
int left = input.length - i;
if (left > 2) {
chunk = (input[i] << 16)|
(input[i + 1] << 8) |
input[i + 2];
output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
output[ridx++] = cvtTable[(chunk&0xFC0) >> 6];
output[ridx++] = cvtTable[(chunk&0x3F)];
} else if (left == 2) {
chunk = (input[i] << 16) |
(input[i + 1] << 8);
output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
output[ridx++] = cvtTable[(chunk&0xFC0) >> 6];
output[ridx++] = cvtTable[63];
} else if(left == 1){
chunk = input[i] << 16;
output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
output[ridx++] = cvtTable[63];
output[ridx++] = cvtTable[63];
}
}
return new String(output);
}
static String revert(String content) {
byte tempArray[] = (content).getBytes();
byte[] result = new byte[tempArray.length/4*3];
int temp = 0;
int pos = 0; //for(int i= tempArray.length-1;i>= 0;i--)
//System.out.println(tempArray[i]);
for(int i = 0;i < tempArray.length;i += 4) { temp = (search(tempArray[i]) << 18) |
(search(tempArray[i+1]) << 12) |
(search(tempArray[i+2]) << 6) |
(search(tempArray[i+3]));
result[pos++] = (byte)((temp & 0xff0000) >> 16);
result[pos++] = (byte)((temp & 0xff00) >> 8);
result[pos++] = (byte)(temp & 0xff);
}
return new String(result);
}
static int search(int target) {
int i = 0;
while(target != cvtTable[i] && i < 64) {
i++;
}
return i;
}
public static void main(String args[]){
try{
String str = encode("ABCD");
System.out.println("befor="+str); ////这里能加密
System.out.println("after="+revert(str)); //但这里不能解密出“ABCD”
}catch(Exception ex){
ex.printStackTrace();
}
}
}盼望加解密高手指点!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货