6

in my app I wanted to implement some enciphering. Therefore I need the code for the Vigenere cipher. Does anyone know where I can find that source code for Java?

4

3 回答 3

12

这是 Vigenere cipher Class,你可以使用它,只需调用加密和解密函数:代码来自Rosetta Code

public class VigenereCipher {
    public static void main(String[] args) {
        String key = "VIGENERECIPHER";
        String ori = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
        String enc = encrypt(ori, key);
        System.out.println(enc);
        System.out.println(decrypt(enc, key));
    }

    static String encrypt(String text, final String key) {
        String res = "";
        text = text.toUpperCase();
        for (int i = 0, j = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c < 'A' || c > 'Z') continue;
            res += (char)((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
            j = ++j % key.length();
        }
        return res;
    }

    static String decrypt(String text, final String key) {
        String res = "";
        text = text.toUpperCase();
        for (int i = 0, j = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c < 'A' || c > 'Z') continue;
            res += (char)((c - key.charAt(j) + 26) % 26 + 'A');
            j = ++j % key.length();
        }
        return res;
    }
}
于 2012-07-05T15:23:31.047 回答
2

这是一个 Vigenere Cipher Code implementation Sample Java Code to Encrypt and Decrypt using Vigenere Cipher的链接,此外我不建议使用 Vigenere Cipher 作为加密。

我推荐jBCrypt

于 2012-07-05T15:18:49.607 回答
1

这篇文章将为您提供帮助。提供了完整的解密代码。您可以使用它来编写加密代码

于 2014-02-21T16:57:04.420 回答