Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Vigenere Cipher implementation in java

PROGRAM CODE




public class VigenereCipher
{
    FOR FULL PROGRAM,PROCEDURE and lab manual
CONTACT:
CELL:9789697608
           9566627095
EMAIL:hitechguil@gmail.com 
    }

    public 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;
    }

    public static void main(String[] args)
    {
        String key = "VIGENERECIPHER";
        String message = "welcome to vigener cipher";
        String encryptedMsg = encrypt(message, key);
        System.out.println("String: " + message);
        System.out.println("Encrypted message: " + encryptedMsg);
        System.out.println("Decrypted message: " + decrypt(encryptedMsg, key));
    }
}

OUTPUT:

No comments:

Post a Comment

Flag Counter