Anonymous
Как настроить карту символов шифрования для использования строчных латинских букв a-z 0-9 +/=?
Сообщение
Anonymous » 09 окт 2024, 14:56
Я хотел бы изменить отчеты Minecraft Nochat, чтобы использовать только строчные буквы латинского алфавита 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 {
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 )
Не показывать decrpyted text
Я хотел бы использовать карту символов шифрования для строчных букв латинского алфавита a-z 0-9 + / =.
Подробнее здесь:
https://stackoverflow.com/questions/790 ... et-a-z-0-9
1728474976
Anonymous
Я хотел бы изменить отчеты Minecraft Nochat, чтобы использовать только строчные буквы латинского алфавита a-z 0-9 + / = . Я добавил эту карту символов, но теперь она не может расшифровать сообщения. : [code]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 { 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); } } [/code] Это полный проект: (https://github.com/Aizistral-Studios/No-Chat-Reports) Не показывать decrpyted text Я хотел бы использовать карту символов шифрования для строчных букв латинского алфавита a-z 0-9 + / =. Подробнее здесь: [url]https://stackoverflow.com/questions/79070113/how-to-set-charactermap-of-encryption-to-use-lowercase-latinalphabet-a-z-0-9[/url]