substitution cipher

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 15460 Views - Last Post: 22 November 2012 - 07:46 PM Rate Topic: -----

#16 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2791
  • View blog
  • Posts: 11,822
  • Joined: 20-September 08

Re: substitution cipher

Posted 22 November 2012 - 04:12 AM

We've already given you suggestions - which seem to have been ignored

Quote

System.out.println("Caesar Cipher Encryption\n");
But it ISN'T Caesar encryption ...
Was This Post Helpful? 0
  • +
  • -

#17 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: substitution cipher

Posted 22 November 2012 - 04:46 AM

This is no good at all

           for (int i = 0; i < 26; i++){      
                 char cipherTextChar = cipherText.charAt(i);      
                 char plainTextChar = plainText.charAt(i);      
                 encryptedString = encryptedString.replace(plainTextChar,cipherTextChar);      
            }      


unless you respect standard coding conventions that say "plain text is in lowercase and encoded text in uppercase" because if
"abcd..." is translate by
"gzxi..."
you will firts replace all "a" by "g" and later on all "g" back to "a"
Was This Post Helpful? 0
  • +
  • -

#18 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2791
  • View blog
  • Posts: 11,822
  • Joined: 20-September 08

Re: substitution cipher

Posted 22 November 2012 - 06:10 AM

And the only way you can retain the mixed case which you seem to want is by actually using the Caesar cipher, which as i've already said, you're not doing.
Was This Post Helpful? 0
  • +
  • -

#19 shuffle1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-November 12

Re: substitution cipher

Posted 22 November 2012 - 07:35 AM

so how about like this? i was trying to implement the
char cipherc = to.charAt(from.indexOf(plainC))


but i think i was missing something.

import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;


public class Caesar {

	public static void main(String[] args){
		String from = "abcdefghijklmnopqrstuvwxyz";
		String to   = "qwertyuiopasdfghjklzxcvbnm";

			Scanner console = new Scanner(System.in);
			System.out.print("Input file: ");
			String inputFileName = console.next();
			System.out.print("Output file: ");
			String outputFileName = console.next();
    	       
			try{ 
				FileReader reader = new FileReader("C:/"+inputFileName+".txt");
				Scanner in = new Scanner(reader);
				PrintWriter out = new PrintWriter("C:/"+outputFileName+".txt");
		    	            
					while (in.hasNextLine()){
						String line = in.nextLine();
						String outPutText = "";
						for (int i = 0; i < line.length(); i++){
							char c = to.charAt(from.indexOf(line));
						}
						outPutText += c;
						System.out.println("Plaintext: " + line);
						System.out.println("Ciphertext: " + outPutText);
						out.println(outPutText);         
					}
					System.out.println("Processing file complete");
					out.close();
			}
			catch (IOException exception){ 
				System.out.println("Error processing file:" + exception);
			}
	}
}


Was This Post Helpful? 0
  • +
  • -

#20 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: substitution cipher

Posted 22 November 2012 - 09:35 AM

		while (in.hasNextLine()){
			String line = in.nextLine();
			String outPutText = "";
			for (int i = 0; i < line.length(); i++){
				char c = to.charAt(from.indexOf(line.charAt(i)));
				outPutText += c;
			}
			System.out.println("Plaintext: " + line);
			System.out.println("Ciphertext: " + outPutText);
		}


Was This Post Helpful? 1
  • +
  • -

#21 shuffle1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-November 12

Re: substitution cipher

Posted 22 November 2012 - 07:33 PM

thanks for the help. and i change to
int index = decr.indexOf(line.charAt(i));

if (index >= 0) {
char c = encr.charAt(index);
outPutText += c;
}

for fixing the StringIndexOutOfBoundsException
Was This Post Helpful? 0
  • +
  • -

#22 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: substitution cipher

Posted 22 November 2012 - 07:46 PM

you mean to get ride of the char that are not defined in your arrays ?
It is a way of doing it but then the spaces and punctuation marks won't end up in the cypher array
And please rename your class name..., as mentioned a few times that encoding has nothing to do with Caesar cypher :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2