This site requires JavaScript, please enable it in your browser!
Greenfoot back
coder04
coder04 wrote ...

2015/2/17

Can you help me Encrypt and Decrypt text like Caesar Cipher

1
2
3
danpost danpost

2015/2/17

#
You can use the menubar and select 'Edit>New class...' and enter 'Cipher' for the name. Then open the editor for the class and remove EVERYTHING and replace it with the following:
public abstract class Cipher
{
    public static char convertChar(char c, int offset)
    {
        if (c<=64 || (c>90 && c<=96) || c>122) return c;
        int base = 32*((c-1)/32);
        return (char)(base+1+(((c-1)-base+26+offset)%26));
    }
 
    public static String convertString(String str, int offset)
    {
        String converted = "";
        for (int i=0; i<str.length(); i++) converted += convertChar(str.charAt(i), offset);
        return converted;
    }
}
This is slightly more simplified from my last code post.
coder04 coder04

2015/2/17

#
done that but when i type in one text field the other does not change?
danpost danpost

2015/2/17

#
coder04 wrote...
cool it has now compiled i have two of the textfields 1 and 2 which i have added to the world. I want to type in one and the other on to change. What do I add to the textfield for it to work
I do not believe that is something you want to put in the TextField class. That would be quite difficult to accomplish there when dealing with more than one textfield object. Best is probably to detect changes in the one textfield from the world class and adjust the other textfield when needed.
coder04 coder04

2015/2/17

#
how would i do this?
danpost danpost

2015/2/17

#
There are a couple of ways. Track, in the world class, the current String value of the one textfield and if it changes, adjust the other one. Or, have the world act method call a method in the one textfield that returns a true/false value as to whether its value has been changed since the last call to that method and then adjust the other one when the first one has changed. Either way, you will need to keep references to both textfield objects in the world class so the act method can detect the change in the one and set the change in the other.
coder04 coder04

2015/2/17

#
Shall I put this in the world then
String codedString = Cipher.converString("any text or string expression", key);
String decodedString = Cipher.convertString(codedString, -key);
danpost danpost

2015/2/18

#
coder04 wrote...
Shall I put this in the world then
String codedString = Cipher.converString("any text or string expression", key);
String decodedString = Cipher.convertString(codedString, -key);
No. Those lines only show how the methods are used; but, they are not designed for you scenario, in particular. You would need to call 'getText' on the one TextField object and convert the String returned to 'setText' on the other TextField object.
danpost danpost

2015/2/18

#
The code required in your subclass of world would have these basic statements:
// fields for the TextField objects
private TextField inputTextField;
private TextField codedTextField;
// the code key
private int codeKey = Greenfoot.getRandomNumber(25)+1;
// create new textfield objects, assign them to the fields and add them to the world in the constructor
inputTextField = new TextField(...);
codedTextField = new TextField(...);
addObject(inputTextField, /** wherever */);
addObject(codedTextField, /** wherever */);
// in act method within an 'if' block where the condition is that the text of the inputTextField object was changed
codedTextField.setText(Cipher.convertString(inputTextField.getText(), codeKey));
coder04 coder04

2015/2/18

#
I have added this into the world
    public Textfield_World(){
        super(800, 500, 1);
        getBackground().setColor(Color.blue);
        getBackground().fill();
        getBackground().setColor(Color.red);
        getBackground().setFont(new Font("", Font.BOLD, 25));
        getBackground().drawString("Caesar Cipher", 300, 50);
        addObject(new TextField("Encrypt"), 200, 250);
        addObject(new TextField2("Decrypt"), 600, 250);
        Greenfoot.setSpeed(50);
        Greenfoot.start();
        inputTextField = new TextField(a);
        codedTextField = new TextField(s);
        addObject(inputTextField, TextField);
        addObject(codedTextField, TextField);
    }
    
also I have add the other part of the code in the textfield. what have I done wrong?
public class TextField extends Actor
{
    private boolean active = false;
    private boolean cursorActive = false;
    private boolean displayKeyNames;
    private boolean enabled = true;
    
    private static int activeTextField = 1;
    
    private int textFieldNumber;
    private int textFontSize;
    private int cursorPosition = 0;
    
    private long cursorTime = System.currentTimeMillis();
    
    private String input = "";
    private String text = "";
    private String temp = "";
    
    private Color bgColor = Color.white;
    private Color textColor = Color.black;
    
        // fields for the TextField objects
 private TextField inputTextField;
 private TextField codedTextField;
 // the code key
 private int codeKey = Greenfoot.getRandomNumber(25)+1;
 // in act method within an 'if' block where the condition is that the text of the inputTextField object was changed
 codedTextField.setText(Cipher.convertString(inputTextField.getText(), codeKey));
    
danpost danpost

2015/2/18

#
The main thing you did wrong was to not follow direction very well. The lines you added to your TextField class starting at line 23 should be in your TextField_World class. Also, if you pay attention to the comments within those lines, you will find that certain lines go in certain places in that class. Now, what I did not know was that your encrypted textfield had a separate class which is not necessary. Still, you are now adding FOUR textfield objects into your world where you are only supposed to have TWO (if this even compiles).
coder04 coder04

2015/2/18

#
Thanks I got it to work but when I type something in the encrypt textfield the decrypt text field is quite random. What do I need to change in the caeser cypher class to change the cipher number? world class-
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class Textfield_World here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Textfield_World extends World
{
    
           // fields for the TextField objects
        private TextField inputTextField;
        private TextField codedTextField;
        // the code key
        private int codeKey = Greenfoot.getRandomNumber(25)+1;
    
    public Textfield_World(){
        super(800, 500, 1);
        getBackground().setColor(Color.blue);
        getBackground().fill();
        getBackground().setColor(Color.red);
        getBackground().setFont(new Font("", Font.BOLD, 25));
        getBackground().drawString("Caesar Cipher", 300, 50);
        //addObject(new TextField("Encrypt"), 200, 250);
        //addObject(new TextField2("Decrypt"), 600, 250);
        Greenfoot.setSpeed(50);
        Greenfoot.start();
        
        
        // create new textfield objects, assign them to the fields and add them to the world in the constructor
        inputTextField = new TextField("Encrypt");
        codedTextField = new TextField("Decrypt");
        addObject(inputTextField, 300, 250);
        addObject(codedTextField, 300, 400);
        // in act method within an 'if' block where the condition is that the text of the inputTextField object was changed
        codedTextField.setText(Cipher.convertString(inputTextField.getText(), codeKey));

    }
    


}
cipher class-
public abstract class Cipher
{
    public static char convertChar(char c, int offset)
    {
        if (c<=64 || (c>90 && c<=96) || c>122) return c;
        int base = 32*((c-1)/32);
        return (char)(base+1+(((c-1)-base+26+offset)%26));
    }
  
    public static String convertString(String str, int offset)
    {
        String converted = "";
        for (int i=0; i<str.length(); i++) converted += convertChar(str.charAt(i), offset);
        return converted;
    }
}
danpost danpost

2015/2/18

#
The 'codeKey' field on line 18 of your TextField_World class contains the cipher number. If you want to set it to a specific value that it will always use, you can change what it is assigned to on that line. If you had something else in mind, please explain in more detail -- like what do you want it to change to, when will it be allowed to change, etc.
coder04 coder04

2015/2/20

#
a bit like this caeser cipher example
danpost danpost

2015/2/20

#
Change line 18 of the Textfield_World class to:
private int codeKey;
Now, you probably will need an 'act' method in the class to detect changes in the values of whatever is going to control the 'codeKey' value as well as changes in the 'inputTextField' value. Changes in either should trigger an update on the 'codedTextField' value (which would probably require another method in that class to process).
coder04 coder04

2015/2/20

#
I know what you mean but i dont know what code to use to do that. I changed what you said and added a act method what do I put In it?
  public void act()
   {
    
   }
There are more replies on the next page.
1
2
3