我在哪里可以找到Vigenere密码的Java源代码?

时间:2012-07-05 15:08:07

标签: java android encryption vigenere

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

3 个答案:

答案 0 :(得分:12)

这是Vigenere密码类,你可以使用它,只需调用加密和解密函数: 代码来自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;
    }
}

答案 1 :(得分:2)

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

我建议jBCrypt

答案 2 :(得分:1)

This pos t会帮助你。提供完整的解密代码。您可以使用它来编写加密代码