substitution cipher

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 shuffle1990  Icon User is offline

  • New D.I.C Head

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

substitution cipher

Posted 14 November 2012 - 11:15 PM

Hi, i am new in java. i was thinking of doing a substitution cipher program. i have searched quite a lot of forum. and usually they all are using the key shifting method by assigning the 1st character in the loop. for example normal alphabet sequence "a,b,c,d,e,...z" to shifted alphabet "c,a,b,d,e,...z.". i was thinking about finding a method that can specific the alphabet sequence that i want the encrypt and decrypt because with that the encryption will be vary to others people. for example, "a,b,c,d,e,...,z" to "f,e,a,t,h,...,b". well, is there anyway to do it?

Is This A Good Question/Topic? 0
  • +

Replies To: substitution cipher

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,660
  • Joined: 20-September 08

Re: substitution cipher

Posted 15 November 2012 - 04:29 AM

Quote

well, is there anyway to do it?
Yes, just use Map<Character, Character> and you can map one character to any other

This post has been edited by g00se: 15 November 2012 - 04:30 AM
Reason for edit:: typo

Was This Post Helpful? 1
  • +
  • -

#3 JHol  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 10-February 11

Re: substitution cipher

Posted 15 November 2012 - 04:47 AM

if you are intrested learning cryptation a good place to start is with the Caesar cipher algorithm

My link
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,660
  • Joined: 20-September 08

Re: substitution cipher

Posted 15 November 2012 - 05:06 AM

Quote

a good place to start is with the Caesar cipher algorithm
Not in this case. shuffle1990 has already rejected the functionality used in Caesar
Was This Post Helpful? 3
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: substitution cipher

Posted 15 November 2012 - 08:01 AM

You can use two arrays to fill g00se's Map

    class Ceasar {
       private Map<Character,Character> map;

       public Ceasar() {
          String from = "abcdefghijklmnopqrstuvwxyz";
          String to   = "qwertyuiopasdfghjklzxcvbnm";

          map = new HashMap<Character,Character>();
          for(int i = 0; i < from.length(); ++i)
             map.put(from.charAt(i), to.charAt(i));
      }
}



View PostJHol, on 15 November 2012 - 07:47 AM, said:

if you are intrested learning cryptation a good place to start is with the Caesar cipher algorithm

My link

Nice article by the way.
And the code is here in DIC
http://www.dreaminco...-ceasar-cipher/
as at least 8 other secret codes programs.
Was This Post Helpful? 2
  • +
  • -

#6 shuffle1990  Icon User is offline

  • New D.I.C Head

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

Re: substitution cipher

Posted 19 November 2012 - 07:32 PM

so while encryption is using this
 
map = new HashMap<Character,Character>();
          for(int i = 0; i < from.length(); ++i)
             map.put(from.charAt(i), to.charAt(i));



and decryption will be using this?
 map = new HashMap<Character,Character>();
          for(int i = 0; i < from.length(); ++i)
             map.put(to.charAt(i), from.charAt(i));


Was This Post Helpful? 0
  • +
  • -

#7 shuffle1990  Icon User is offline

  • New D.I.C Head

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

Re: substitution cipher

Posted 19 November 2012 - 07:51 PM

import java.util.HashMap;

public class Caesar {
    private HashMap<Character,Character> encr;
    private HashMap<Character,Character> decr;
    
    public Caesar() {
       String from = "abcdefghijklmnopqrstuvwxyz";
       String to   = "qwertyuiopasdfghjklzxcvbnm";

       encr = new HashMap<Character,Character>();
       for(int i = 0; i < from.length(); ++i)
          encr.put(from.charAt(i), to.charAt(i));
       
       decr = new HashMap<Character,Character>();
       for(int i = 0; i < from.length(); ++i)
          decr.put(to.charAt(i), from.charAt(i));
   }
}




something like this?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: substitution cipher

Posted 19 November 2012 - 08:01 PM

Make it in a single loop
import java.util.HashMap;

public class Caesar {
    private HashMap<Character,Character> encr;
    private HashMap<Character,Character> decr;
    
    public Caesar() {
       String from = "abcdefghijklmnopqrstuvwxyz";
       String to   = "qwertyuiopasdfghjklzxcvbnm";

       encr = new HashMap<Character,Character>();
       decr = new HashMap<Character,Character>();
       for(int i = 0; i < from.length(); ++i) {
          encr.put(from.charAt(i), to.charAt(i));
          decr.put(to.charAt(i), from.charAt(i));
       }
   }


:^:
Was This Post Helpful? 0
  • +
  • -

#9 shuffle1990  Icon User is offline

  • New D.I.C Head

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

Re: substitution cipher

Posted 19 November 2012 - 08:28 PM

hmm, single loop is much better practice for programming?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: substitution cipher

Posted 19 November 2012 - 08:33 PM

don't see why you would have to this twice:

for(int i = 0; i < from.length(); ++i) {

which is:

adding 26 times 1 to i
calling 26 times the method length(| of String from
checking 26 times that i < from.length()
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,660
  • Joined: 20-September 08

Re: substitution cipher

Posted 20 November 2012 - 04:20 AM

A Map in this case isn't really necessary as you can just do
char cipherC = to[from.indexOf(plainC)];

Was This Post Helpful? 1
  • +
  • -

#12 shuffle1990  Icon User is offline

  • New D.I.C Head

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

Re: substitution cipher

Posted 21 November 2012 - 06:59 AM

View Postg00se, on 20 November 2012 - 07:20 PM, said:

A Map in this case isn't really necessary as you can just do
char cipherC = to[from.indexOf(plainC)];


oh? so with that for encrypt would be
char cipherE = to[from.indexOf(plainC)];

while decrypt would be as below?
char cipherD = from[to.indexOf(plainC)];


View Postg00se, on 20 November 2012 - 07:20 PM, said:

A Map in this case isn't really necessary as you can just do
char cipherC = to[from.indexOf(plainC)];


oh? so with that for encrypt would be
char cipherE = to[from.indexOf(plainC)];

while decrypt would be as below?
char cipherD = from[to.indexOf(plainC)];


View Postg00se, on 20 November 2012 - 07:20 PM, said:

A Map in this case isn't really necessary as you can just do
char cipherC = to[from.indexOf(plainC)];

and what is the (plainC) refer to? is it plainText?
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,660
  • Joined: 20-September 08

Re: substitution cipher

Posted 21 November 2012 - 10:03 AM

Quote

while decrypt would be as below?


Not quite. You need to invert everything:
char cipherC = to[from.indexOf(plainC)];

char plainC = from[to.indexOf(cipherC)];

Quote

and what is the (plainC) refer to? is it plainText?
Yes, a single char thereof
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: substitution cipher

Posted 21 November 2012 - 11:00 AM

Not really as they are String you have to use the charAt() method so it is

char cipherC = to.charAt(from.indexOf(plainC));

char plainC = from.charAt(to.indexOf(cipherC));



:)
Was This Post Helpful? 0
  • +
  • -

#15 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 - 02:28 AM

@@ well, i come up with a way to make it without using the hashmap @@ but i encounter some problem @@
which are the input file are consist of both uppercase and lower case alphabet. but the output only able to display uppercase only. and is it possible for me to change ' ' to '#' as well? and i also having some problem in doing the decrypting as well @@ below is the coding that so far i am having now. hope you guys can help me out. @@ and thanks in advance.

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

public class SubstitutionCipher{
    public static final String plainText = "abcdefghijklmnopqrstuvwxyz";
    public static String encrypt(String inputWord, String cipherText){    
    		String encryptedString = inputWord.toLowerCase();
        
            for (int i = 0; i < 26; i++){    
                char cipherTextChar = cipherText.charAt(i);    
                char plainTextChar = plainText.charAt(i);    
                encryptedString = encryptedString.replace(plainTextChar,cipherTextChar);    
            }    
            return encryptedString;    
	}    
	public static void main(String[] args){
		String cipherText = "FEATHRZYXWVUSQPONMLKJIGDCB";
			System.out.println("Caesar Cipher Encryption\n");
			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 encipheredText = encrypt(line, cipherText);
						System.out.println("Plaintext: " + line);
						System.out.println("Ciphertext: " + encipheredText);
						out.println(encipheredText);         
					}
					out.close();
			}
			catch (IOException exception){ 
				System.out.println("Error processing file:" + exception);
			}
	}
}



and for the
String cipherText = "FEATHRZYXWVUSQPONMLKJIGDCB";
this line, it seem that lowercase cant be use @@ any idea to make it lower change to lower, while upper change to upper?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2