Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- /**
- * Vigenere_Cipher decoder
- * Sorry for being to lazy to comment the code, hopefully you can figure it out.
- * @author Kushtrim
- * @version 1.01
- */
- public class Vigenere_Cipher
- {
- public static void main(String args[])
- {
- String input = JOptionPane.showInputDialog("The ciphertext to be decoded :").toLowerCase();
- String shifra = JOptionPane.showInputDialog("The key :").toLowerCase();
- int[] cipher = new int[shifra.length()];
- for ( int i = 0 ; i !=shifra.length(); i++)
- {
- cipher[i] = shifra.charAt(i) - 96;
- // System.out.println(cipher[i]);
- }
- String answer = "";
- for (int i = 0 ; i<input.length(); i++)
- {
- int index = i%shifra.length();
- char c = decodeLetter(cipher[index] ,input.charAt(i) );
- answer +=c;
- }
- System.out.print(answer);
- }
- // this can be done more neatly by using mod ( % in java)
- private static char decodeLetter( int x , char c)
- {
- char r;
- if(c >= 97 && c < (97+26))
- {
- int k = (c -97 -x+1);
- if ( k<0)
- {
- k = 26+k;
- }
- r = (char)( k+97);
- }
- else {
- r= '$';
- }
- return r;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement