Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find the Java source code for the Vigenere cipher? [closed]

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?

like image 434
user1420042 Avatar asked Jul 05 '12 15:07

user1420042


2 Answers

This is Vigenere cipher Class, you can use it, just call encrypt and decrypt function : The code is from 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;
    }
}
like image 155
AliSh Avatar answered Sep 28 '22 10:09

AliSh


Here is a link to a Vigenere Cipher Code implementation Sample Java Code to Encrypt and Decrypt using Vigenere Cipher, besides that I cannot recommend to use Vigenere Cipher as encryption.

I recommend jBCrypt.

like image 43
Konrad Reiche Avatar answered Sep 28 '22 10:09

Konrad Reiche