0

嗨,我在解码我的代码时遇到问题,但有时它会显示一个错误,例如 Unexpected code:�</p>

我将与您分享我的编码和解码代码,以便我可以获得一些帮助

public class PasswordCheck {

    public static void main(String[] args) {
        String salt = "atscene";


        String password = RandomStringUtils.randomAlphanumeric(8);  

        byte[] passwordHash = null;

        passwordHash = (salt+password).getBytes();
        String str = new String(Base64.encode(passwordHash));

            byte[] passwordHash1 = Base64.decode(str);
            String pass= new String(passwordHash1); 
            System.out.println("Random Password: "+password);
            System.out.println("Coded password: "+str);

            System.out.println(pass.substring(7));


    }

}

Base64.java

public class Base64
{
    static final char[] charTab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();

    public static String encode(byte[] paramArrayOfByte)
    {
        return encode(paramArrayOfByte, 0, paramArrayOfByte.length, null).toString();
    }

    public static StringBuffer encode(byte[] paramArrayOfByte, int paramInt1, int paramInt2,
            StringBuffer paramStringBuffer)
    {
        int l;
        if (paramStringBuffer == null)
            paramStringBuffer = new StringBuffer(paramArrayOfByte.length * 3 / 2);
        int i = paramInt2 - 3;
        int j = paramInt1;
        int k = 0;
        while (j <= i)
        {
            l = (paramArrayOfByte[j] & 0xFF) << 16 | (paramArrayOfByte[(j + 1)] & 0xFF) << 8
                    | paramArrayOfByte[(j + 2)] & 0xFF;
            paramStringBuffer.append(charTab[(l >> 18 & 0x3F)]);
            paramStringBuffer.append(charTab[(l >> 12 & 0x3F)]);
            paramStringBuffer.append(charTab[(l >> 6 & 0x3F)]);
            paramStringBuffer.append(charTab[(l & 0x3F)]);
            j += 3;
            if (k++ < 14)
                continue;
            k = 0;
            paramStringBuffer.append("\r\n");
        }
        if (j == paramInt1 + paramInt2 - 2)
        {
            l = (paramArrayOfByte[j] & 0xFF) << 16 | (paramArrayOfByte[(j + 1)] & 0xFF) << 8;
            paramStringBuffer.append(charTab[(l >> 18 & 0x3F)]);
            paramStringBuffer.append(charTab[(l >> 12 & 0x3F)]);
            paramStringBuffer.append(charTab[(l >> 6 & 0x3F)]);
            paramStringBuffer.append("=");
        }
        else if (j == paramInt1 + paramInt2 - 1)
        {
            l = (paramArrayOfByte[j] & 0xFF) << 16;
            paramStringBuffer.append(charTab[(l >> 18 & 0x3F)]);
            paramStringBuffer.append(charTab[(l >> 12 & 0x3F)]);
            paramStringBuffer.append("==");
        }
        return paramStringBuffer;
    }

    static int decode(char paramChar)
    {
        if ((paramChar >= 'A') && (paramChar <= 'Z'))
            return (paramChar - 'A');
        if ((paramChar >= 'a') && (paramChar <= 'z'))
            return (paramChar - 'a' + 26);
        if ((paramChar >= '0') && (paramChar <= '9'))
            return (paramChar - '0' + 26 + 26);
        switch (paramChar)
        {
        case '+':
            return 62;
        case '/':
            return 63;
        case '=':
            return 0;
        }
        throw new RuntimeException("unexpected code: " + paramChar);
    }

    public static byte[] decode(String paramString)
    {
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        try
        {
            decode(paramString, localByteArrayOutputStream);
        }
        catch (IOException localIOException)
        {
            throw new RuntimeException();
        }
        return localByteArrayOutputStream.toByteArray();
    }

    public static void decode(String paramString, OutputStream paramOutputStream) throws IOException
    {
        int i = 0;
        int j = paramString.length();
        while (true)
        {
            if ((i < j) && (paramString.charAt(i) <= ' '))
                ++i;
            if (i == j)
                return;
            int k = (decode(paramString.charAt(i)) << 18) + (decode(paramString.charAt(i + 1)) << 12)
                    + (decode(paramString.charAt(i + 2)) << 6) + decode(paramString.charAt(i + 3));
            paramOutputStream.write(k >> 16 & 0xFF);
            if (paramString.charAt(i + 2) == '=')
                return;
            paramOutputStream.write(k >> 8 & 0xFF);
            if (paramString.charAt(i + 3) == '=')
                return;
            paramOutputStream.write(k & 0xFF);
            i += 4;
        }
    }
}

我得到编码为 TIspn`VKyg 的代码,这是无法从 Base64 解码的。并且在异常情况下,我得到了 Unexpected code: � 。请告诉我是否有解决此问题的方法。

4

0 回答 0