CBC 中的 Caesar / 如何在 Java 中进行 XOR?

Caesar in CBC / How to XOR in Java?

我一直在尝试''improve''通过在CBC模式下加密的简单凯撒加密。

据我了解,第一个字符必须通过初始化向量进行异或,然后通过密钥进行异或,然后输出是加密文本的第一个字符。然后这将与第二个字符进行异或,然后再次与密钥进行异或,等等。

我不太明白 XORing 应该如何工作。

让我们给出翻译 table(仅 space 和 A-Z): /s: 0, A: 1, B: 2, …, Z: 26, 关键:1, Init.vector: 5

使用简单的凯撒,''HELLO'' -> {8,5,12,12,20} -> {9,6,13,13,21} -> ''IFMMP''

但是我如何使用 CBC 进行加密?

如果你能告诉我如何在 Java 中实现它,那将特别有帮助。谢谢!

嗯,我解释你的问题就像你认为你想通过密码的密钥进行异或,这是错误的:

你用之前的密码结果异或。像这样:

// Untested code
// The code below need to be adjusted for it to print meaningful 
// characters in the encrypted string as the xor function produces
// integers outside the range of standard ascii characters 

private void cbcCaesar(){

    int key = 1;
    String message = "java";
    int initialisationVector = 5; // the IV is not super necessarily hidden from an attacker but should be different for each message

    StringBuilder encryptedMessageBuilder = new StringBuilder();

    char[] charArray = message.toCharArray();

    int encryptedLetter = initialisationVector;
    for (int letter : charArray){
        int xorApplied = letter ^ encryptedLetter;
        encryptedLetter = applyCaesarCipher(xorApplied, key);
        encryptedMessageBuilder.append((char) encryptedLetter);
    }

    System.out.println(encryptedMessageBuilder.toString());
}

private int applyCaesarCipher(int xorApplied, int key) {
    return (xorApplied+ key) % 26;
}

将上述代码段转换为可用内容的最简单方法是将字母映射到数字 0-26 并使用它代替字符的 ascii 编码

我觉得这个资源很好https://www.youtube.com/watch?v=0D7OwYp6ZEc