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?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
user1420042
  • 1,689
  • 9
  • 35
  • 53
  • 2
    AFAIK it's a pretty simple cipher, why don't just implement it yourself? In fact you can check if Java Cryptography library has the implementation, anyways, I won't recommend using Vigenere cipher in real world applications. – Egor Jul 05 '12 at 15:14
  • you can find your answer here in this link http://stackoverflow.com/questions/10280637/vigenere-cipher-in-java-for-all-utf-8-characters – Mostafa Elbutch Apr 17 '13 at 16:25

3 Answers3

12

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;
    }
}
curtissv
  • 47
  • 8
AliSh
  • 10,085
  • 5
  • 44
  • 76
2

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.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
1

This post will help you .The entire code for deciphering is provided. You can use this for writing enciphering code

hola
  • 930
  • 1
  • 17
  • 35
cruze
  • 21
  • 1