Почему не шифруются русские символы?
Почему данный код шифрует все символы, кроме русских?
Здесь я ищу и заменяю символ в алфавите:
private void encrypting(Path sourcePath, Path resultPath, List<Character> alphabet, int key) {
try (FileChannel source = FileChannel.open(sourcePath, StandardOpenOption.READ);
FileChannel result = FileChannel.open(resultPath, StandardOpenOption.WRITE)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (source.read(buffer) != -1) {
buffer.flip();
while (buffer.hasRemaining()) {
byte b = buffer.get();
char c = (char) b;
int index = alphabet.indexOf(c);
if (index != -1) {
int newIndex = Math.floorMod(index + key, alphabet.size());
char encrypted = alphabet.get(newIndex);
result.write(ByteBuffer.wrap(new byte[]{(byte) encrypted}));
} else {
result.write(ByteBuffer.wrap(new byte[]{(byte) c}));
}
}
buffer.clear();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Здесь русские буквы добавляются в алфавит:
for (char i = 'А'; i <= 'Я'; i++) {
alphabet.add(i);
}
for (char i = 'а'; i <= 'я'; i++) {
alphabet.add(i);
}
Исходная строка, которую надо было зашифровать:
Бородино Borodino 1234567890(),?>
И результат шифрования:
Бородино%Gtwtinst%6789:;<=>5-.1ED
Почему данный код шифрует все символы, кроме русского алфавита?