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/20

#
coder04 wrote...
I changed what you said and added a act method what do I put In it?
The 'codedTextField' string needs to update any time there is a change in (1) the 'inputTextField' string; and (2) the 'codeKey' value. So, the act method of the world class should constantly check for changes in those values and, if any change occurs, update the 'codedTextField' string.
coder04 coder04

2015/2/20

#
how would I do that? do i just use the getrandomnumber?
coder04 coder04

2015/2/20

#
what would be the statement i should use
coder04 coder04

2015/2/20

#
I have tried something but I have no idea what I have done wrong
 public void act()
   {
      String codedString = Cipher.converString("hello", a);
      String decodedString = Cipher.convertString(codedString, -key);
   }
danpost danpost

2015/2/20

#
That would only set the local String variables to the coded and uncoded text. That does not do anything as far as looking for a change in one and changing the other when a change is detected. When the act method has completed execution, the local variables are dismissed and the result is that there is never any change (like the act method was not there at all).
danpost danpost

2015/2/21

#
Please read line 38 of the last Textfield_World class code post above. Even unconditionally, the line should work; but, it would not be good programming to set the value of the TextField object repeatedly when there is no change in its value.
danpost danpost

2015/2/21

#
I created a simple test scenario. It has the world class, a textbox class and the cipher class. The 'Cipher' class is exactly as the one provided in this discussion thread. The Textbox class is a simple class consisting of one String field for the current text; one getter and one setter method to deal with the text; and one no-argument constructor that call a single String argument construct using an empty string, ' "" '. The constructor with the String argument creates the textbox image and then calls the 'setText(String)' method which sets the text and applys the text to its image. The world class, 'CipherWorld' has fields for two Textbox objects and and int field for the code key. The constructor assigns the textbox fields and adds them into the world. The act method checks for and processes the following keystrokes: "up" and "down" change the code key value; "escape" clears the text of the first textbox; "backspace" removes the last character of the text of the first textbox (if it is not an empty string); "space" is converted to " "; after the above are checked, if the length of the string holding the keystroke is one, it is appended to the text of the first textbox; After the keystrokes have been processed the second textbox is assigned the coded text of the first textbox. 3 classes; less than 100 lines of undocumented code. Next would be to create an object that can be used to change the code key (instead of having just the "up" and "down" keys change it).
coder04 coder04

2015/2/21

#
Ooh can I please see that. That's exactly what I need mine to be like. Can I please look at the code for that scenario>
danpost danpost

2015/2/21

#
The Textbox class:
import greenfoot.*;

/**
 * creates a simple textbox object
 */
public class Textbox extends Actor
{
    private String text = ""; // the current text
    
    /** calls String argument constructor with an empty string */
    public Textbox()
    {
        this("");
    }
    
    /**
     * creates initial image of textbox and sets initial text
     * @param txt the initial text string value for this textbox
     */
    public Textbox(String txt)
    {
        GreenfootImage image = new GreenfootImage(300, 30);
        image.fill();
        image.setColor(java.awt.Color.white);
        setImage(image);
        setText(txt);
    }

    /**
     * sets text value and adds image of text to textbox image
     * @param the text string value to be assigned to this textbox
     */
    public void setText(String txt)
    {
        text = txt;
        getImage().fillRect(2, 2, 296, 26);
        GreenfootImage tImg = new GreenfootImage(text, 24, null, null);
        int x = tImg.getWidth() < 290 ? 5 : 290-tImg.getWidth();
        getImage().drawImage(tImg, x, 3);
        getImage().fillRect(2, 2, 2, 26);
    }
    
    /**
     * returns the current text string value assigned to this textbox
     * @return the current value of the text string assigned to this textbox
     */
    public String getText()
    {
        return text;
    }
}
And the CipherWorld world class:
import greenfoot.*;

/**
 * a world to demonstrate Ceasar Ciphering
 */
public class CipherWorld extends World
{
    private Textbox textboxA, textboxB; // the text boxes
    private int codeKey; // the cipher key
    
    /** prepares world */
    public CipherWorld()
    {
        super(380, 500, 1);
        addObject(textboxA = new Textbox(), getWidth()/2, 40);
        addObject(textboxB = new Textbox(), getWidth()/2, 80);
    }
    
    /** processes keyboard input and maintains string values of textboxes */
    public void act()
    {
        String key = Greenfoot.getKey();
        if (key != null)
        {
            if ("space".equals(key)) key = " ";
            if ("backspace".equals(key) && textboxA.getText().length() > 0)
            {
                String text = textboxA.getText();
                text = text.substring(0, text.length()-1);
                textboxA.setText(text);
            }
            if ("escape".equals(key)) textboxA.setText("");
            if ("up".equals(key)) codeKey = (codeKey+1)%26;
            if ("down".equals(key)) codeKey = (codeKey+25)%26;
            if (key.length() == 1) textboxA.setText(textboxA.getText()+key);
            textboxB.setText(Cipher.convertString(textboxA.getText(), codeKey));
        }
    }
}
coder04 coder04

2015/2/21

#
Thanks and is the cipher class the same
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/21

#
coder04 wrote...
is the cipher class the same
Like I said:
danpost wrote...
I created a simple test scenario. It has the world class, a textbox class and the cipher class. The 'Cipher' class is exactly as the one provided in this discussion thread.
coder04 coder04

2015/2/21

#
t
You need to login to post a reply.
1
2
3