4 Replies - 1647 Views - Last Post: 06 October 2012 - 10:31 AM Rate Topic: -----

#1 Carlito242  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-October 12

Vigenere Cipher (Just Alphabet)

Posted 05 October 2012 - 10:32 AM

Ok so the problem us i have to make a vigenere cipher ( Encryption only) in java using only the 26 letters of the alphabet and without using ASCII and basic conditionals (if,for,while) but i cant seem to under stand where im going wrong. The name of the variables are in spanish but i will translate the messages to english.Here is my code:

 

import javax.swing.JOptionPane;
import java.util.*;


public class Vigenere {

  
    public static void main(String[] args) {
        
        String Mensaje = JOptionPane.showInputDialog("Please input message to be encrypted").toLowerCase();
        String Codigo = JOptionPane.showInputDialog("Please input the key to be used").toLowerCase();
        System.out.print(mensajeEncryptar(Codigo, Mensaje));
    
    }
           

  
    
        public static String mensajeEncryptar(String Codigo, String Mensaje) {
    		String ABC="abcdefghijklmnopqrstuvwxyz";
    		String nuevoMensaje = new String();
  
    		char[] menAry = Mensaje.toCharArray(); 
    		for(int i=0; i<menAry.length; i++) { 
    			    char temp= Mensaje.charAt(i);
    			    for (int j=0; j<ABC.length(); j++){
        					if (temp==ABC.charAt(j) ){
        					int temp2=ABC.indexOf(temp);   			
        				    Mensaje.substring(temp2);
        					}
        					}
    			    System.out.print(Mensaje);
        			}
 		
    		return Mensaje;
 		
        }

}




Is This A Good Question/Topic? 0
  • +

Replies To: Vigenere Cipher (Just Alphabet)

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Vigenere Cipher (Just Alphabet)

Posted 05 October 2012 - 11:15 AM

I see a few problems at the moment.

for (int j=0; j<ABC.length(); j++){
     if (temp==ABC.charAt(j) ){
          int temp2=ABC.indexOf(temp);   			
          Mensaje.substring(temp2);
     }
}
    System.out.print(Mensaje);



Right here is the source of your problem. First off you have a for loop looking for what index a char is at.
Then right under that you just use the indexOf method. What is the point of the for loop? You just have to say

int temp2 = ABC.indexOf(temp);



As for your next part. This is just a logical error. Lets say the message I put in is "Zero". This would mean temp2 would be the index of Z in ABC. Z is the 25th index of ABC. However my string is only 4 characters long. I can't make a substring of a 4 character long string at the 25th index. That what's wrong with it. If you need help with the program as a whole don't be afraid to ask :)
Was This Post Helpful? 0
  • +
  • -

#3 Carlito242  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-October 12

Re: Vigenere Cipher (Just Alphabet)

Posted 06 October 2012 - 08:16 AM

View PostKinaces, on 05 October 2012 - 11:15 AM, said:

I see a few problems at the moment.

for (int j=0; j<ABC.length(); j++){
     if (temp==ABC.charAt(j) ){
          int temp2=ABC.indexOf(temp);   			
          Mensaje.substring(temp2);
     }
}
    System.out.print(Mensaje);



Right here is the source of your problem. First off you have a for loop looking for what index a char is at.
Then right under that you just use the indexOf method. What is the point of the for loop? You just have to say

int temp2 = ABC.indexOf(temp);



As for your next part. This is just a logical error. Lets say the message I put in is "Zero". This would mean temp2 would be the index of Z in ABC. Z is the 25th index of ABC. However my string is only 4 characters long. I can't make a substring of a 4 character long string at the 25th index. That what's wrong with it. If you need help with the program as a whole don't be afraid to ask :)


Well the idea of the for loop is that as it goes letter by letter in the message it would compare with its equivalent in the alphabet string thus giving it a value (ex 0-26 i know that if it returns a 2 its a c). I want to do this with both the message and the key then with the values obtained i can use the formula C(i)=[ (M(i)+K(i) ]MOD26

As for the second part, i see now what you mean but how would i go about correcting that. To be honest i just learned about substring so i dont fully know how it works, and yes i do need help with it as a whole.
Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Vigenere Cipher (Just Alphabet)

Posted 06 October 2012 - 10:21 AM

I'll start with how substring works. A String can be thought of as a array of chars.
Each letter has a index. For example.

String message = "Fish";
String key = "Lemo"



Like most things in java, Strings are zero based. Meaning the first letter (i.e. F) will be index 0. The next letter (i.e. i) is index 1, so on and so forth.

Now it is important to remember that the word Fish like in the example would have indexes 0 through 3. The length of the String however is 4.

First off you need to enforce that the message and the key are the SAME size. If you do not then the program will NOT work.

(If you do not know about Maps you can skip this part. This is just showing another possibility to do this program.)
There is a second option to do this program that will make it a lot faster. Use the 26 letters of the alphabet as keys, and their values as their index. (a = 0, b = 1, c = 2, etc.)
This way you do not have to loop through the ABC array for every letter.

This is what you need to do. Lets take our example earlier Fish and Lemo. We first need to find where in ABC does the letter F (first letter of Fish) and the letter L (first letter of lemo) are. Add their positions together. Then find mod 26. Hold this index in another variable. Then find where in ABC is the second letter of Fish and Lemo are. Add them together. Find mod 26. Do this until there are no more letters.
Was This Post Helpful? 1
  • +
  • -

#5 Carlito242  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-October 12

Re: Vigenere Cipher (Just Alphabet)

Posted 06 October 2012 - 10:31 AM

Thank you, i will give it a try and show you what i have.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1