【问题标题】:Where can I find the Java source code for the Vigenere cipher? [closed]在哪里可以找到 Vigenere 密码的 Java 源代码? [关闭]
【发布时间】:2012-07-06 00:39:44
【问题描述】:

在我的应用程序中,我想实现一些加密。因此我需要 Vigenere 密码的代码。有谁知道我在哪里可以找到 Java 的源代码?

【问题讨论】:

  • AFAIK 这是一个非常简单的密码,为什么不自己实现呢?事实上,您可以检查 Java Cryptography 库是否有实现,无论如何,我不建议在现实世界的应用程序中使用 Vigenere 密码。
  • 您可以在此链接中找到您的答案*.com/questions/10280637/…

标签: java android encryption vigenere


【解决方案1】:

这里是 Vigenere Cipher Code 实现Sample Java Code to Encrypt and Decrypt using Vigenere Cipher 的链接,除此之外我不建议使用 Vigenere Cipher 作为加密。

我推荐jBCrypt

【讨论】:

  • 您发布的链接现已失效。
  • @GeoGriffin 感谢您的指出,我已将链接更新为另一个示例。
  • 链接又死了。
【解决方案2】:

这是 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;
    }
}

【讨论】:

    【解决方案3】:

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

    【讨论】: