在哪里可以找到维吉尼亚密码的Java源代码?

6

我希望能在我的应用程序中实现一些加密功能,因此我需要维吉尼亚密码的代码。有人知道我可以在哪里找到Java的源代码吗?


2
据我所知,这是一个相当简单的密码,为什么不自己实现呢?实际上,你可以检查一下Java加密库是否有实现,但无论如何,我不建议在现实世界的应用中使用维吉尼亚密码。 - Egor
你可以在以下链接中找到答案: https://dev59.com/22PVa4cB1Zd3GeqP1wQu - Mostafa Elbutch
3个回答

12

这是维吉尼亚密码类,您可以使用它,只需调用加密和解密函数:

代码来自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;
    }
}

2

1
你发布的链接已经失效了。 - GeoGriffin
@GeoGriffin 感谢您指出,我已经更新了链接到另一个示例。 - Konrad Reiche
链接又挂了。 - Omore

1

这篇文章将帮助您。整个解密代码都已提供。您可以使用它来编写加密代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接