为什么此代码会加密除俄语字符之外的所有字符?
这里我搜索并替换字母表中的字符:
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
为什么此代码会加密除俄文字母之外的所有字符?