Advertisement
Kushtrim

VigenereCipherDecoder

Jun 15th, 2013
4,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import javax.swing.*;
  2. /**
  3.  * Vigenere_Cipher decoder
  4.  * Sorry for being to lazy to comment the code, hopefully you can figure it out.
  5.  * @author Kushtrim
  6.  * @version 1.01
  7.  */
  8. public class Vigenere_Cipher
  9.  
  10. {
  11.     public static void main(String args[])
  12.     {
  13.         String input = JOptionPane.showInputDialog("The ciphertext to be decoded :").toLowerCase();
  14.         String shifra = JOptionPane.showInputDialog("The key :").toLowerCase();
  15.         int[] cipher = new int[shifra.length()];
  16.  
  17.  
  18.              
  19.         for ( int i = 0 ; i !=shifra.length(); i++)
  20.         {
  21.             cipher[i] = shifra.charAt(i) - 96;
  22.            // System.out.println(cipher[i]);
  23.         }
  24.  
  25.         String answer = "";
  26.         for (int i = 0 ; i<input.length(); i++)
  27.         {
  28.  
  29.             int index = i%shifra.length();
  30.             char c = decodeLetter(cipher[index] ,input.charAt(i) );
  31.            
  32.               answer +=c;
  33.         }
  34.  
  35.         System.out.print(answer);
  36.     }
  37. // this can be done more neatly by using mod ( % in java)
  38.     private static char decodeLetter( int x , char c)
  39.     {
  40.  
  41.         char r;
  42.         if(c >= 97 && c < (97+26))
  43.         {
  44.  
  45.             int k = (c -97 -x+1);
  46.             if ( k<0)
  47.             {
  48.                 k = 26+k;
  49.             }
  50.  
  51.             r = (char)( k+97);
  52.         }
  53.         else {
  54.             r= '$';
  55.         }
  56.         return r;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement