有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

密码Java解码盐碱64

我对Java编程非常陌生,尝试制作一个基于Base64和UTF-8的解码器

我目前正在尝试使解码器从控制台获取输入,然后执行解码并在控制台中显示结果。 我有点不知所措,不知如何才能把我的输入解码 “公共静态字节[]解码(字符串src)”,然后在控制台中打印出来

到目前为止,我得到的代码如下:

import java.io.*;
import java.io.ByteArrayOutputStream;

public class Decode {
  //String to decode ="AHzRlmUMPa7PhTZdmoCU7Swy/YWcaMF20/TQJP8PJAOXqY12sf90XzxQ+jq/4ktnpYaSsrAc2KBA/ZpycGueks9khoJvRPPeZft7SR8WTrvbTtvHXvpm5Yjo3KD02MBjp6dfGsWAXtitqHuJDhK1O36E3CV0vn5iVjlpDIZrYQJramXoK2gVttFlDkaN86deWmoutSKDkn4o/ppPD2dK6Oo48ydJA6QsgEDdkR9nsmZ7rYZigjdb0y+o4ByjD1oFBG/5odZGpYPbvclA5tWcZBcmzxuumcKu5+Adu9L6paWltXgYUV1Kxkt7mGZWGiXljkedfFd2F49AaRE2wv+1tdeCvOuuDGuuxYVkXc2AxO0bESXdjoTOiSM=";
private static final char[] encodeTable = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
private static final char PAD = '=';
public static final int HASHLEN = 16;
public static final int HASHHEXLEN = 32;

public static void main (String[] args) {
  System.out.print("Enter text to decode: ");
   //  open up standard input
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   String passwd = null;
  try {
     passwd = br.readLine();
  } catch (IOException ioe) {
     System.out.println("IO error trying to read your name!");
     System.exit(1);
  }
   System.out.println("The Password is: " + passwd);

 }
 public static byte[] decode(String src)  {

int bits = src.length() * 6;
ByteArrayOutputStream res = new ByteArrayOutputStream(bits / 8);

int index = 0;
int bytesRemaining = src.length();
while (bytesRemaining >= 4)
{
  int val0 = getVal(src.charAt(index));
  while ((val0 == -2) && (bytesRemaining > 0))
  {
    index++;
    bytesRemaining--;
    if (bytesRemaining > 0) {
      val0 = getVal(src.charAt(index));
    }
  }
  if (bytesRemaining == 0) {
    throw new IllegalStateException("Unexpected end of input.");
  }
  int val1 = getVal(src.charAt(index + 1));
  while ((val1 == -2) && (bytesRemaining > 0))
  {
    index++;
    bytesRemaining--;
    if (bytesRemaining > 0) {
      val1 = getVal(src.charAt(index + 1));
    }
  }
  if (bytesRemaining == 0) {
    throw new IllegalStateException("Unexpected end of input.");
  }
  int val2 = getVal(src.charAt(index + 2));
  while ((val2 == -2) && (bytesRemaining > 0))
  {
    index++;
    bytesRemaining--;
    if (bytesRemaining > 0) {
      val2 = getVal(src.charAt(index + 2));
    }
  }
  if (bytesRemaining == 0) {
    throw new IllegalStateException("Unexpected end of input.");
  }
  int val3 = getVal(src.charAt(index + 3));
  while ((val3 == -2) && (bytesRemaining > 0))
  {
    index++;
    bytesRemaining--;
    if (bytesRemaining > 0) {
      val3 = getVal(src.charAt(index + 3));
    }
  }
  if (bytesRemaining == 0) {
    throw new IllegalStateException("Unexpected end of input.");
  }
  int group = 0;
  int padCount = 0;
  if (val0 != -1) {
    group |= val0 << 18;
  } else {
    padCount++;
  }
  if (val1 != -1) {
    group |= val1 << 12;
  } else {
    padCount++;
  }
  if (val2 != -1) {
    group |= val2 << 6;
  } else {
    padCount++;
  }
  if (val3 != -1) {
    group |= val3;
  } else {
    padCount++;
  }
  res.write((group & 0xFF0000) >> 16);
  if (val2 != -1)
  {
    res.write((group & 0xFF00) >> 8);
    if (val3 != -1) {
      res.write(group & 0xFF);
    }
  }
  if (padCount > 0) {
    bytesRemaining = 0;
  } else {
    bytesRemaining -= 4;
  }
  index += 4;
}
  return res.toByteArray();
}
  public static String decodeToString(String s)
  {
  return new String(decode(s));
  } 
   private static final int getVal(char ch)
{
if (ch == '=') {
  return -1;
}
int val = ch;
if ((val >= 65) && (val <= 90)) {
  return val - 65;
}
if ((val >= 97) && (val <= 122)) {
  return val - 71;
}
if ((val >= 48) && (val <= 57)) {
  return val + 4;
}
if (val == 43) {
  return 62;
}
if (val == 47) {
  return 63;
}
return -2;
}
}

共 (1) 个答案

  1. # 1 楼答案

        byte[] data = decode(passwd);
        System.out.print("Hex: ");
        for (byte element : data) {
            System.out.printf("%02X", element);
        }