1 Replies - 2337 Views - Last Post: 20 January 2011 - 02:08 PM Rate Topic: -----

#1 murifagge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-January 11

help on vigenere cipher

Posted 20 January 2011 - 11:10 AM

Please help me on this vigenere ciper, I try to write a program that encrypt and decrype using vigenere cipher, but every time I try running the program it works but my problem is that, the spaces between words are not restore as initially in the plaintext when displaying the ciphertext although I donít want spaces to be included when running the algorithm but I want the spaces to restore when displaying the ciphertext only. Thanks a lot murifagge
I'll give an example to make my problem more clear. If we were given a file that contained only the words "Pop Quiz Today!" the resulting encrypted file should read "Pbi Qhbz Ghdnr!". Spaces, numbers, and punctuation are to not be affected by the key, and as such, the character that was to be next used in the key should be used on the following character. I think that's why I was initially trying to use a scanner so I could just examine the next characters while preserving the white space. Any suggestions or thoughts? Thanks again
below is my source code for consideration

import java.util.*;
public class Viginere
{

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		String p="",c="",k="",l="abcdefghijklmnopqrstuvwxyz";
		//System.out.print("wat d u want/ p 1 to enc or 2 to dec?");
		
		
		//int m=in.nextInt();
		
			System.out.print("give the pl text");
			p=in.nextLine();
			p = p.replaceAll(" ", "");
			System.out.print("give the key");
			k=in.nextLine();
			int ky=k.length();
			int pln=p.length();
		String 	y =k;		//System.out.print("give the pl text");   

								//p=in.nextLine(); 

						//p = p.replaceAll(" ", "");   

		int m = pln%ky;
		
			for( int i=1; i<pln/ky ; i++)
			{
				//if(i>=ky)
					//i=0;
				k=k+y;
				
				
				//x.charAt((x.indexOf(K.charAt(j)) + x.indexOf(p1.charAt(j)))%27);
			}
		k=k+k.substring(0,m);	
			//System.out.println(k);
		for (int j=0; j<pln; j++ ){
			c=c + l.charAt((l.indexOf(k.charAt(j)) + l.indexOf(p.charAt(j)))%26);
		}
		
		c=c.toUpperCase();
		System.out.println("Ciphertext"+c);
		
		// Decryption
		c = c.toLowerCase();
		
		String pl = "";
		
		for (int r=0; r<pln; r++ ){
			pl=pl + l.charAt(((l.indexOf(c.charAt(r)) - l.indexOf(k.charAt(r))) +26 )%26);
		}	
		
		System.out.println("decipher: "+p);
			
		

	}

}


This post has been edited by macosxnerd101: 20 January 2011 - 12:53 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: help on vigenere cipher

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: help on vigenere cipher

Posted 20 January 2011 - 02:08 PM

If you want to preserve the physical layout of the plain text string you do not want to do this:

...
p=in.nextLine();
p = p.replaceAll(" ", "");
...



The replaceAll() method is removing the spaces between words.

You should test each character position in the plain text string to see if it is an alpha character:

...
if (isLetter(p.charAt(i))) {
    do whatever
} 
...



See the isLetter method of Class Character and public char charAt(int index) method of Class String

This post has been edited by n8wxs: 20 January 2011 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1