0

我已经修改了 Vigenere 密码算法,但是我遇到了问题。如果我加密 a String,然后解密结果,我不会得到我的原始String数据。我的源代码:

public class ViganereEncryptionBase64 {

    private char[] keys = new char[19];

    int sizeKey = 0;
    int cimin = 0;

    final String MVEAs = " !\"#$%&'()=+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~";

    public ViganereEncryptionBase64() {
        String keys = "bangdollaMC08ITATS";
        this.keys = keys.toCharArray();
    }

    public String encrypt(String toEncrypt) {
        char charsToEncrypt[] = toEncrypt.toCharArray();
        char encryptedChars[] = toEncrypt.toCharArray();
        int i = 0;
        while (i < encryptedChars.length) {

            int sube = MVEAs.indexOf(charsToEncrypt[i]) + MVEAs.indexOf(keys[sizeKey]) + cimin;

            sizeKey++;
            if (sizeKey == keys.length)
                sizeKey = 0;

            encryptedChars[i] = MVEAs.charAt(Math.floorMod(sube, 95));
            cimin = MVEAs.indexOf(encryptedChars[i]);
            i += 1;
        }
        return String.valueOf(encryptedChars);
    }

    public String decrypt(String toDecrypt) {
        char charsToDecrypt[] = toDecrypt.toCharArray();
        char decryptedChars[] = toDecrypt.toCharArray();
        int i = 0;
        while (i < charsToDecrypt.length) {
            int sube = MVEAs.indexOf(charsToDecrypt[i]) - MVEAs.indexOf(keys[sizeKey]) - cimin;

            sizeKey++;
            if (sizeKey == keys.length)
                sizeKey = 0;
            decryptedChars[i] = MVEAs.charAt(Math.floorMod(sube, 95));
            cimin = MVEAs.indexOf(charsToDecrypt[i]);
            i++;
        }
        return String.valueOf(decryptedChars);
    }
}

一个说明问题的用例是

public static void main(String[] args) {
    ViganereEncryptionBase64 viganere = new ViganereEncryptionBase64();
    System.out.println("Result encryption: " + viganere.encrypt("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"));
    viganere = new ViganereEncryptionBase64();
    System.out.println("Result description: " + viganere.decrypt("~at,4X~<Cp|\"(>rdnds~1x_\\XTmN5T{irX-9DZv+opkhJLbT[x7Mk/'J&|p&A0qAMR_yh9|H#\\Y91/kKtQI,Su3Ik, p$@IH.c:Ue'Lj25X L#[3f8ql3U]oF"));
}

其中原始输入是"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"

并且加密的结果是"~at,4X~<Cp|\"(>rdnds~1x_\\XTmN5T{irX-9DZv+opkhJLbT[x7Mk/'J&|p&A0qAMR_yh9|H#\\Y91/kKtQI,Su3Ik, p$@IH.c:Ue'Lj25X L#[3f8ql3U]oF"

然后解密的结果是"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">"

这是不同的。

4

1 回答 1

2

所以你的加密和解密工作完美,我运行了你的代码,没有看到任何测试输入和输出之间有任何区别。

如果我们使用不需要转义序列的示例,我认为您可能会对反斜杠\用作转义字符感到困惑:

public static void main(String[] args) {
    viganere = new ViganereEncryptionBase64();
    System.out.println("Result encryption: " + viganere.encrypt("Hello!"));
    // Which is encrypted to "+R.b7("

    viganere = new ViganereEncryptionBase64();
    System.out.println("Result description: " + viganere.decrypt("+R.b7("));
    // Which is decrypted to "Hello!"
}

你可以看到它完美地工作。

在您的示例中\, each 之前有一个",这样 Java 就不会认为您已经结束了您的String,您会注意到它允许您"在 a 中途使用String而不结束它。

所以实际上\你的 中仍然有一个String,看起来没有一个,因为它不会被打印出来System.out.println。所有转义字符都是如此;当打印给用户时,它们将被翻译成标签或新行等

于 2015-07-02T10:16:38.100 回答