3 Replies - 37610 Views - Last Post: 19 January 2011 - 02:43 PM Rate Topic: -----

#1 murifagge  Icon User is offline

  • New D.I.C Head

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

vigenere cipher

Posted 19 January 2011 - 02:24 AM

pls im stock in writing a project in vigenere cipher for encryption and decryction of text only, i wrote the program, its working correctly but in the process of doing the algorithm it takes on space as character and calculate in it and i dont want a space to be included in the calculation, i what the program when running to eliminate every space in the plaintext, so that the space character is not included. pls i need your help.
tanks yours friend
murtala.
below is the sourcecode;

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();
			
			System.out.print("give the key");
			k=in.nextLine();
			int ky=k.length();
			int pln=p.length();
		String 	y =k;
		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);
			
		

	}

}


Edited by Dogstopper: :code:!

Is This A Good Question/Topic? 0
  • +

Replies To: vigenere cipher

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,678
  • Joined: 20-September 08

Re: vigenere cipher

Posted 19 January 2011 - 03:58 AM

Post your code per the instructions in the editor
Was This Post Helpful? 1
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: vigenere cipher

Posted 19 January 2011 - 08:29 AM

use the replaceAll method of String on the plaintext.
replace all " " (spaces) with na empty String "".
like:
System.out.print("give the pl text");  
p=in.nextLine();
p = p.replaceAll(" ", "");  

Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: vigenere cipher

Posted 19 January 2011 - 02:43 PM

The second of my 7 or 8 Secret Code tutorials

http://www.dreaminco...igenere-square/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1