如何设置加密字符映射以使用小写拉丁字母 a-z 0-9 + / =?

问题描述 投票:0回答:1

我想修改minecraft Nochatreports以仅使用小写拉丁字母a-z 0-9 + / =。 我添加了这个字符映射表,但现在它无法解密消息。 :

package com.aizistral.nochatreports.common.encryption;

import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.SecureRandom;
import java.util.Arrays;

import it.unimi.dsi.fastutil.chars.Char2CharArrayMap;
import it.unimi.dsi.fastutil.chars.Char2CharMap;
import it.unimi.dsi.fastutil.chars.Char2CharMaps;

public abstract class Encryptor<T extends Encryption> {
    protected static final SecureRandom RANDOM = new SecureRandom();
    protected static final Char2CharMap BASE64R_SHIFTS = createBase64RShifts();
    protected static final Char2CharMap BASE64R_SHIFTS_REVERSE = createBase64RShiftsReverse();

    protected Encryptor() {
        // NO-OP
    }

    public abstract String encrypt(String message);

    public abstract String decrypt(String message);

    public abstract T getAlgorithm();

    public abstract String getKey();

    protected static String shiftBase64R(String string) {
        char[] chars = ensureUTF8(string).toCharArray();

        for (int i = 0; i < chars.length; i++) {
            chars[i] = BASE64R_SHIFTS.get(chars[i]);
        }

        return new String(chars);
    }

    protected static String unshiftBase64R(String string) {
        char[] chars = ensureUTF8(string).toCharArray();

        for (int i = 0; i < chars.length; i++) {
            chars[i] = BASE64R_SHIFTS_REVERSE.get(chars[i]);
        }

        return new String(chars);
    }

    protected static byte[] toBytes(String string) {
        return string.getBytes(StandardCharsets.UTF_8);
    }

    protected static String fromBytes(byte[] bytes) {
        return new String(bytes, StandardCharsets.UTF_8);
    }

    protected static String ensureUTF8(String string) {
        return fromBytes(toBytes(string));
    }

    protected static String encodeBase64R(String string) {
        return encodeBase64R(toBytes(string));
    }

    protected static String encodeBase64R(byte[] bytes) {
        return shiftBase64R(fromBytes(Encryption.BASE64_ENCODER.encode(bytes)));
    }

    protected static byte[] encodeBase64RBytes(String string) {
        return toBytes(encodeBase64R(toBytes(string)));
    }

    protected static String decodeBase64R(String string) {
        return fromBytes(decodeBase64RBytes(string));
    }

    protected static byte[] decodeBase64RBytes(String string) {
        return Encryption.BASE64_DECODER.decode(toBytes(unshiftBase64R(string)));
    }

    protected static String encodeBinaryKey(byte[] key) {
        return fromBytes(Encryption.BASE64_ENCODER.encode(key));
    }

    protected static byte[] decodeBinaryKey(String key) throws InvalidKeyException {
        try {
            return Encryption.BASE64_DECODER.decode(toBytes(key));
        } catch (Exception ex) {
            throw new InvalidKeyException(ex);
        }
    }

    protected static byte[] mergeBytes(byte[] array1, byte[] array2) {
        byte[] result = Arrays.copyOf(array1, array1.length + array2.length);
        System.arraycopy(array2, 0, result, array1.length, array2.length);
        return result;
    }

    private static Char2CharMap createBase64RShiftsReverse() {
        Char2CharMap map = createBase64RShifts();
        Char2CharMap reverse = new Char2CharArrayMap(64);

        for (char ch : map.keySet()) {
            reverse.put(map.get(ch), ch);
        }

        return Char2CharMaps.unmodifiable(reverse);
    }

    private static Char2CharMap createBase64RShifts() {
    Char2CharMap map = new Char2CharArrayMap(64);

    // Map Base64 uppercase characters to lowercase Latin alphabet characters
    map.put('A', 'a');
    map.put('B', 'b');
    map.put('C', 'c');
    map.put('D', 'd');
    map.put('E', 'e');
    map.put('F', 'f');
    map.put('G', 'g');
    map.put('H', 'h');
    map.put('I', 'i');
    map.put('J', 'j');
    map.put('K', 'k');
    map.put('L', 'l');
    map.put('M', 'm');
    map.put('N', 'n');
    map.put('O', 'o');
    map.put('P', 'p');
    map.put('Q', 'q');
    map.put('R', 'r');
    map.put('S', 's');
    map.put('T', 't');
    map.put('U', 'u');
    map.put('V', 'v');
    map.put('W', 'w');
    map.put('X', 'x');
    map.put('Y', 'y');
    map.put('Z', 'z');
    
    // Map lowercase Base64 characters to themselves
    map.put('a', 'a');
    map.put('b', 'b');
    map.put('c', 'c');
    map.put('d', 'd');
    map.put('e', 'e');
    map.put('f', 'f');
    map.put('g', 'g');
    map.put('h', 'h');
    map.put('i', 'i');
    map.put('j', 'j');
    map.put('k', 'k');
    map.put('l', 'l');
    map.put('m', 'm');
    map.put('n', 'n');
    map.put('o', 'o');
    map.put('p', 'p');
    map.put('q', 'q');
    map.put('r', 'r');
    map.put('s', 's');
    map.put('t', 't');
    map.put('u', 'u');
    map.put('v', 'v');
    map.put('w', 'w');
    map.put('x', 'x');
    map.put('y', 'y');
    map.put('z', 'z');
    
    // Digits 0-9 map to themselves
    map.put('0', '0');
    map.put('1', '1');
    map.put('2', '2');
    map.put('3', '3');
    map.put('4', '4');
    map.put('5', '5');
    map.put('6', '6');
    map.put('7', '7');
    map.put('8', '8');
    map.put('9', '9');
    
    // Base64-specific characters +, /, and = remain unchanged
    map.put('+', '+');
    map.put('/', '/');
    map.put('=', '=');

    return Char2CharMaps.unmodifiable(map);
    }


}

这是完整的项目:(https://github.com/Aizistral-Studios/No-Chat-Reports)

不显示解密文本

我想将加密字符映射设置为小写拉丁字母 a-z 0-9 + / =。

java encryption aes minecraft
1个回答
0
投票
  • 源字符串是

    Hello World ! 2024-10-12 ! This ia a book.

  • 使用 Base 64 对源字符串进行编码会产生以下结果:

    • SGVsbG8gV29ybGQgISAyMDI0LTEwLTEyICEgVGhpcyBpYSBhIGJvb2su

它包含大写字母。

映射规则:

大写字母

A
映射规则:

A
ascii 代码是 65,->
065
a
ascii 代码是 97。->
097

我们将相应的

A
编码为
065
+
a
+
097
并再次重复。
065a097
+
065a097
=
065a097065a097
,这就是
A

的编码规则

大写字母

B
映射规则:

同样,

B
ascii 代码是 65,->
066
b
ascii 代码为 98,->
098

我们将

B
编码为
066b098066b098

  • 我们使用Base64ExtCustomCoder.customEncode将原始包含大写字母的base64映射转换为不包含大写字母的字符串。
>>>> BASE 64 ENCODE <<<<
SGVsbG8gV29ybGQgISAyMDI0LTEwLTEyICEgVGhpcyBpYSBhIGJvb2su

String msgB64StrToLowerCase = Base64ExtCustomCoder.customEncode(msgB64Str);

输出:

>>>> BASE 64 ENCODE to lower case <<<<
083s115083s115071g103071g103086v118086v118sb071g103071g1038g086v118086v11829yb071g103071g103081q113081q113g073i105073i105083s115083s115065a097065a097y077m109077m109068d100068d100073i105073i1050076l108076l108084t116084t116069e101069e101w076l108076l108084t116084t116069e101069e101y073i105073i105067c099067c099069e101069e101g086v118086v118071g103071g103hpcy066b098066b098p089y121089y121083s115083s115066b098066b098h073i105073i105071g103071g103074j106074j106vb2su
  • 如果我们想恢复,可以使用Base64ExtCustomCoder.customDecode解码并将内容恢复为包含大写字母的编码字符串。
String msgB64StrToOrginialUpperCase = Base64ExtCustomCoder.customDecode(msgB64StrToLowerCase);

只需要添加Base64ExtCustomCoder.java,然后就可以使用customEncode和customDecode进行编码和解码。 您应该能够在您的

Encryptor.java
中轻松应用此功能。

Base64ExtCustomCoder.java

package com.example;

import java.util.ArrayList;

public class Base64ExtCustomCoder {
    static String[] s0 = {
            "065a097065a097",
            "066b098066b098",
            "067c099067c099",
            "068d100068d100",
            "069e101069e101",
            "070f102070f102",
            "071g103071g103",
            "072h104072h104",
            "073i105073i105",
            "074j106074j106",
            "075k107075k107",
            "076l108076l108",
            "077m109077m109",
            "078n110078n110",
            "079o111079o111",
            "080p112080p112",
            "081q113081q113",
            "082r114082r114",
            "083s115083s115",
            "084t116084t116",
            "085u117085u117",
            "086v118086v118",
            "087w119087w119",
            "088x120088x120",
            "089y121089y121",
            "090z122090z122"
    };
    static String[] s1 = {
            "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"
    };

    static int findA2ZPos(String a2z) {
        int rt = -1;
        for (int i = 0; i < 26; i++) {
            if (a2z.equals(s1[i])) {
                rt = i;
                break;
            }
        }
        return rt;
    }

    public static String customEncode(String base64Str) {
        String a1 = base64Str;
        StringBuffer sb = new StringBuffer();
        ArrayList<Integer> posList = new ArrayList<Integer>();
        for (int i = 0; i < a1.length(); i++) {
            char ch1 = a1.charAt(i);
            if (Character.isUpperCase(ch1)) {
                int p1 = findA2ZPos(String.valueOf(ch1));
                String m1 = s0[p1];
                sb.append(m1);
            } else {
                //lower case
                sb.append(ch1);
            }
        } //for1
        String encode = sb.toString();
        return encode;
    }

    public static String customDecode(String encodedStr) {
        StringBuffer sb = new StringBuffer();
        String e1 = encodedStr;
        boolean wEnd = false;
        int pos = 0;
        while (!wEnd) {
            if ((pos + 14) > e1.length()) {
                sb.append(e1.substring(pos));
                wEnd = true;
            } else {
                String check1 = e1.substring(pos, pos + 14);
                boolean findUpperCase = false;
                for (int i = 0; i < 26; i++) {
                    if (check1.equals(s0[i])) {
                        sb.append(s1[i]);
                        findUpperCase = true;
                        pos = pos + 14;
                        break;
                    }
                } //for
                if (!findUpperCase) {
                    sb.append(e1.charAt(pos));
                    pos = pos + 1;
                }
            }


            if (pos >= e1.length()) {
                wEnd = true;
            }
        } //while
        String rt = sb.toString();
        return rt;
    }

}

Main.java

演示代码

import com.example.Base64ExtCustomCoder;

import java.util.Base64;

public class Main {
    public static void main(String[] args) {
        String msg0 = "Hello World ! 2024-10-12 ! This ia a book.";
        System.out.println(">>>> source <<<<<");
        System.out.println(msg0);

        //ENCODE
        byte[] encodedBytes = msg0.getBytes();
        String msgB64Str = Base64.getEncoder().encodeToString(encodedBytes);
        System.out.println();
        System.out.println(">>>> BASE 64 ENCODE <<<<");
        System.out.println(msgB64Str);

        // SGVsbG8gV29ybGQgISAyMDI0LTEwLTEyICEgVGhpcyBpYSBhIGJvb2su
        // HAVE UPPER CASE Char

        String msgB64StrToLowerCase = Base64ExtCustomCoder.customEncode(msgB64Str);
        System.out.println();
        System.out.println(">>>> BASE 64 ENCODE to lower case <<<<");
        System.out.println(msgB64StrToLowerCase);

        String msgB64StrToOrginialUpperCase = Base64ExtCustomCoder.customDecode(msgB64StrToLowerCase);
        System.out.println(">>>> BASE 64 ENCODE to Orginial Upper Case <<<<");
        System.out.println(msgB64StrToOrginialUpperCase);

        //DECODE
        byte[] decodedBytes = Base64.getDecoder().decode(msgB64Str);
        String msg2 = new String(decodedBytes);
        System.out.println();
        System.out.println(">>>> BASE 64 DECODE <<<<");
        System.out.println(msg2);

        byte[] decodedBytesX2 = Base64.getDecoder().decode(msgB64StrToOrginialUpperCase);
        String msgX2 = new String(decodedBytesX2);
        System.out.println();
        System.out.println(">>>> BASE 64 DECODE X2 <<<<");
        System.out.println(msgX2);
        
    } //main
} //class

结果:

>>>> source <<<<<
Hello World ! 2024-10-12 ! This ia a book.

>>>> BASE 64 ENCODE <<<<
SGVsbG8gV29ybGQgISAyMDI0LTEwLTEyICEgVGhpcyBpYSBhIGJvb2su

>>>> BASE 64 ENCODE to lower case <<<<
083s115083s115071g103071g103086v118086v118sb071g103071g1038g086v118086v11829yb071g103071g103081q113081q113g073i105073i105083s115083s115065a097065a097y077m109077m109068d100068d100073i105073i1050076l108076l108084t116084t116069e101069e101w076l108076l108084t116084t116069e101069e101y073i105073i105067c099067c099069e101069e101g086v118086v118071g103071g103hpcy066b098066b098p089y121089y121083s115083s115066b098066b098h073i105073i105071g103071g103074j106074j106vb2su
>>>> BASE 64 ENCODE to Orginial Upper Case <<<<
SGVsbG8gV29ybGQgISAyMDI0LTEwLTEyICEgVGhpcyBpYSBhIGJvb2su

>>>> BASE 64 DECODE <<<<
Hello World ! 2024-10-12 ! This ia a book.

>>>> BASE 64 DECODE X2 <<<<
Hello World ! 2024-10-12 ! This ia a book.
© www.soinside.com 2019 - 2024. All rights reserved.