gpt4 book ai didi

Java 解码加盐 Base64

转载 作者:太空宇宙 更新时间:2023-11-04 14:13:11 48 4
gpt4 key购买 nike

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

我目前正在尝试使解码器从控制台获取输入,然后执行解码并在控制台中显示结果。我对如何通过解码我的输入有点迷失“public static byte[]解码(String 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;
}
}

最佳答案

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

关于Java 解码加盐 Base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28042183/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com