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
coder04 coder04

2015/2/17

#
This is my text file I would like to make a caesar cipher. Can you tell me what to add?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * Write a description of class TextField here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
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;
     
    /**
     * Creates a new TextField of a default size of 250 x 50 with black letters on white background.
     */
    public TextField() {
        this(250, 50, false, true, Color.white, Color.black, "");
    }
     
    /**
     * Creates a new TextField with a default size of 250 x 50 with black letters on white background and a starting text.
     *
     * @param text
     *      The text that is shown directly.
     */
    public TextField(String text) {
        this(250, 50, false, true, Color.white, Color.black, text);
    }
     
    /**
     * Creates a new TextField with a default size of 250 x 50 with black letters on white background and a starting text.
     *
     * @param text
     *      The text that is shown directly.
     *
     * @param enabled
     *      If you want the textfield to be enabled (able to write in it) this boolean has to be true.
     */
    public TextField(String text, boolean enabled) {
        this(250, 50, false, enabled, Color.white, Color.black, text);
    }
     
    /**
     * Creates a new TextField of the given size with black letters on white background.
     *
     * @param width
     *      The TextFields width (which has to be greater than 50).
     *
     * @param height
     *      The TextFields height (which has to be greater than 20).
     */
    public TextField(int width, int height) throws IllegalArgumentException {
        this(width, height, false, true, Color.white, Color.black, "");
    }
     
    /**
     * Creates a new TextField of the given size with black letters on white background.
     *
     * @param width
     *      The TextFields width (which has to be greater than 50).
     *
     * @param height
     *      The TextFields height (which has to be greater than 20).
     *
     * @param enabled
     *      If you want the textfield to be enabled (able to write in it) this boolean has to be true.
     */
    public TextField(int width, int height, boolean enabled) throws IllegalArgumentException {
        this(width, height, false, enabled, Color.white, Color.black, "");
    }
     
    /**
     * Creates a new TextField of the given size with black letters on white background and a starting text.
     *
     * @param width
     *      The TextFields width (which has to be greater than 50).
     *
     * @param height
     *      The TextFields height (which has to be greater than 20).
     *
     * @param text
     *      The text that is shown directly.
     */
    public TextField(int width, int height, String text) throws IllegalArgumentException {
        this(width, height, false, true, Color.white, Color.black, text);
    }
     
    /**
     * Creates a new TextField of the given size with black letters on white background and a starting text.
     *
     * @param width
     *      The TextFields width (which has to be greater than 50).
     *
     * @param height
     *      The TextFields height (which has to be greater than 20).
     *
     * @param displayKeyNames
     *      If you want to use the textfield for only one key this parameter has to be true.
     *
     * @param text
     *      The text that is shown directly.
     */
    public TextField(int width, int height, boolean displayKeyNames, String text) throws IllegalArgumentException {
        this(width, height, displayKeyNames, true, Color.white, Color.black, text);
    }
     
    /**
     * Creates a new TextField of the given size and the given colors.
     *
     * @param width
     *      The TextFields width (which has to be greater than 50).
     *
     * @param height
     *      The TextFields height (which has to be greater than 20).
     *
     * @param bgColor
     *      The color of the TextFields background.
     *
     * @param textColor
     *      The color of the text.
     */
    public TextField(int width, int height, Color bgColor, Color textColor) throws IllegalArgumentException {
        this(width, height, false, true, bgColor, textColor, "");
    }
     
    /**
     * Creates a new TextField of the given size and the given colors.
     *
     * @param width
     *      The TextFields width (which has to be greater than 50).
     *
     * @param height
     *      The TextFields height (which has to be greater than 20).
     *
     * @param displayKeyNames
     *      If you want to use the textfield for only one key this parameter has to be true.
     *
     * @param bgColor
     *      The color of the TextFields background.
     *
     * @param textColor
     *      The color of the text.
     */
    public TextField(int width, int height, boolean displayKeyNames, Color bgColor, Color textColor) throws IllegalArgumentException {
        this(width, height, false, true, bgColor, textColor, "");
    }
     
    /**
     * Creates a new TextField of the given size, the given colors and a starting text.
     *
     * @param width
     *      The TextFields width (which has to be greater than 50).
     *
     * @param height
     *      The TextFields height (which has to be greater than 20).
     *
     * @param bgColor
     *      The color of the TextFields background.
     *
     * @param textColor
     *      The color of the text.
     */
    public TextField(int width, int height, Color bgColor, Color textColor, String text) throws IllegalArgumentException {
        this(width, height, false, true, bgColor, textColor, text);
    }
     
    /**
     * Creates a new TextField of the given size, the given colors and a starting text.
     *
     * @param width
     *      The TextFields width (which has to be greater than 50).
     *
     * @param height
     *      The TextFields height (which has to be greater than 20).
     *
     * @param displayKeyNames
     *      If you want to use the textfield for only one key this parameter has to be true.
     *
     * @param bgColor
     *      The color of the TextFields background.
     *
     * @param textColor
     *      The color of the text.
     */
    public TextField(int width, int height, boolean displayKeyNames, Color bgColor, Color textColor, String text) throws IllegalArgumentException {
        this(width, height, displayKeyNames, true, bgColor, textColor, text);
    }
     
    /**
     * Creates a new TextField of the given size, the given colors and a starting text.
     *
     * @param width
     *      The TextFields width (which has to be greater than 50).
     *
     * @param height
     *      The TextFields height (which has to be greater than 20).
     *
     * @param displayKeyNames
     *      If you want to use the textfield for only one key this parameter has to be true.
     *
     * @param enabled
     *      If you want the textfield to be enabled (able to write in it) this boolean has to be true.
     *
     * @param bgColor
     *      The color of the TextFields background.
     *
     * @param textColor
     *      The color of the text.
     */
    public TextField(int width, int height, boolean displayKeyNames, boolean enabled, Color bgColor, Color textColor, String text) throws IllegalArgumentException {
        if (width < 50) {
            throw new IllegalArgumentException("The width of the text field has to be greater or equal 50");
        }
        else if (height < 20) {
            throw new IllegalArgumentException("The height of the text field has to be greater or equal 20");
        }
        this.bgColor = bgColor;
        this.textColor = textColor;
        this.text = text;
        this.displayKeyNames = displayKeyNames;
        this.enabled = enabled;
        cursorPosition = text.length();
        getImage().clear();
        getImage().scale(width, height);
        textFontSize = height - 10;
        resetImage();
        displayText();
    }
     
    /**
     * Assigns this field a number.
     * This method is called when the object is added to the world.
     */
    public void addedToWorld(World world) {
        textFieldNumber = getWorld().getObjects(TextField.class).size();
    }
     
    /**
     * Control the TextFields action.
     */
    public void act() {
        if (Greenfoot.mouseClicked(this)) {
            activeTextField = textFieldNumber;
            active = true;
        }
        if (active) {
            if (activeTextField != textFieldNumber) {
                active = false;
                cursorActive = false;
                displayText();
                return;
            }
            if (System.currentTimeMillis() - cursorTime > 750) {
                cursorTime = System.currentTimeMillis();
                cursorActive = !cursorActive;
            }
            input = Greenfoot.getKey();
            if (enabled) {
                if (displayKeyNames) {
                    if (input != null && input != "") {
                        text = input;
                    }
                }
                else {
                    if (input == "backspace" && text.length() != 0 && cursorPosition != 0) {
                        if (cursorPosition == text.length()) {
                            text = text.substring(0, text.length()-1);
                        }
                        else {
                            temp = text.substring(0, cursorPosition - 1);
                            temp += text.substring(cursorPosition, text.length()-1);
                            text = temp;
                            temp = "";
                        }
                        cursorPosition--;
                    }
                    else if (input == "enter") {
                        setActiveTextField();
                    }
                    else if (input == "space") {
                        if (cursorPosition == text.length()) {
                            text += " ";
                        }
                        else {
                            temp = text.substring(0, cursorPosition);
                            temp += " ";
                            temp += text.substring(cursorPosition, text.length() - 1);
                            text = temp;
                            temp = "";
                        }
                        cursorPosition++;
                    }
                    else if (input == "left") {
                        if (cursorPosition > 0) {
                            cursorPosition--;
                            cursorTime = System.currentTimeMillis();
                            cursorActive = true;
                        }
                    }
                    else if (input == "right") {
                        if (cursorPosition < text.length()) {
                            cursorPosition++;
                            cursorTime = System.currentTimeMillis();
                            cursorActive = true;
                        }
                    }
                    else if (input != null && input.length() == 1) {
                        if (cursorPosition == text.length()) {
                            text += input;
                        }
                        else {
                            temp = text.substring(0, cursorPosition);
                            temp += input;
                            temp += text.substring(cursorPosition, text.length() - 1);
                            text = temp;
                            temp = "";
                        }
                        cursorPosition++;
                    }
                }
            }
            else if (input == "enter") {
                setActiveTextField();
            }
            displayText();
        }
        else {
            if (activeTextField == textFieldNumber) {
                active = true;
            }
        }
    }
     
    /**
     * Display the text onto the TextField.
     */
    private void displayText() {
        GreenfootImage textImage = new GreenfootImage(text, textFontSize, textColor, new Color(0, 0, 0, 0));
        GreenfootImage textBeforeCursor = new GreenfootImage(text.substring(0, cursorPosition), textFontSize, textColor, new Color(0, 0, 0, 0));
        resetImage();
        getImage().drawImage(textImage, (textImage.getWidth() > getImage().getWidth() - 10 ? -(textImage.getWidth() - getImage().getWidth()) - 10 : 5), (getImage().getHeight() / 2 - textImage.getHeight() / 2));
        getImage().setColor(textColor);
        if (cursorActive) {
            getImage().fillRect((textBeforeCursor.getWidth() > getImage().getWidth() - 10 ? getImage().getWidth() - 8 : textBeforeCursor.getWidth() + 7), getImage().getHeight() / 2 - textFontSize / 2, 2, textFontSize);
        }
        getImage().setColor(bgColor);
        getImage().fillRect(0, 0, 3, getImage().getHeight()-2);
    }
     
    /**
     * Clears the image of the TextField so that it is empty again.
     */
    private void resetImage() {
        getImage().setColor(bgColor);
        getImage().fill();
        getImage().setColor(Color.black);
        getImage().fillRect(0, getImage().getHeight()-2, getImage().getWidth(), 3);
        getImage().fillRect(getImage().getWidth()-2, 0, 3, getImage().getHeight());
    }
     
    /**
     * Set the active TextField to the given number.
     *
     * @param activeTextField
     *      The number of the TextField that is activated.
     */
    public void setActiveTextField(int activeTextField) {
        this.activeTextField = activeTextField;
    }
    /**
     * Set the active TextField to the next textfield.
     */
    public void setActiveTextField() {
        activeTextField++;
        if (activeTextField > getWorld().getObjects(TextField.class).size()) {
            activeTextField = 1;
        }
    }
     
    /**
     * Check whether this textfield is enabled (if you can change the text in it).
     *
     * @return
     *      Returns true if the text field is enabled.
     */
    public boolean isEnabled() {
        return enabled;
    }
    /**
     * Enable or disenable the textfield.
     *
     * @param enabled
     *      If you want the textfield to be enabled (able to write in it) this boolean has to be true.
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
     
    /**
     * Returns the text of this textfield.
     *
     * @return
     *      The text that is currently displayed in the textfield.
     */
    public String getText() {
        return text;
    }
    /**
     * Set the text of this textfield.
     *
     * @param text
     *      The text that should be displayed in the textfield.
     */
    public void setText(String text) {
        this.text = text;
        cursorPosition = text.length();
    }
     
    /**
     * Returns the backgroundcolor of this TextField.
     *
     * @return
     *      The backgroundcolor of this TextField.
     */
    public Color getBackgroundColor() {
        return bgColor;
    }
    /**
     * Set the backgroundcolor to the given color.
     *
     * @param bgColor
     *      The new backgroundcolor.
     */
    public void setBackgroundColor(Color bgColor) {
        this.bgColor = bgColor;
    }
     
    /**
     * Returns the textcolor of this TextField.
     *
     * @return
     *      The textcolor of this TextField.
     */
    public Color getTextColor() {
        return textColor;
    }
    /**
     * Set the textcolor to the given color.
     *
     * @param textColor
     *      The new textcolor.
     */
    public void setTextColor(Color textColor) {
        this.textColor = textColor;
    }
}
coder04 coder04

2015/2/17

#
help please?
erdelf erdelf

2015/2/17

#
I am not quite sure where you want to add it in this code, but the basics of a caesar cipher is simple, but you have to do it character by character.
1
2
3
char decoded = 'd'; // the character you want to encode
int caesar = 3; // the parameter used for the caesar cipher
char encoded = decoded-('a'+caesar%24)+'a'; // the encoded character
(this should work)
danpost danpost

2015/2/17

#
First, you will need a field to hold the "key" to the cipher. It can be an int field that holds a value between 1 and 25, inclusive. This is the offset value between the real character and the coded character. Then you will need a methods to code/decode the characters. Something like this:
1
2
3
4
5
public char convertChar(char c, int offset)  // offset is key value to code and negative key value to decode
{
    if (char<=64 || (char>90 && char<=96) || char>122) return c; // not an alpha character
    return (char)(32*((int)c)/32)+1+(((int)c-1)%32+offset+26)%26); // converted character
}
I did not test the method, but it should be close (if not accurate).
coder04 coder04

2015/2/17

#
I dont get how i would make this work with text if i put it into this code it will not compile have you got any example for Caesar cipher or a different way i could use?
coder04 coder04

2015/2/17

#
I have got two of the text fields one needs to be where i type and the other needs to change automatically so where do i put this?
1
2
3
4
public char convertChar(char c, int offset)  // offset is key value to code and negative key value to decode
{
    if (char<=64 || (char>90 && char<=96) || char>122) return c; // not an alpha character
    return (char)(32*((int)c)/32)+1+(((int)c-1)%32+offset+26)%26); // converted character
danpost danpost

2015/2/17

#
You could add another method:
1
2
3
4
5
6
public String convertString(String str, int offset)
{
    String converted = "";
    for (int i=0; i<str.length(); i++) converted += convertChar(str.charAt(i), offset);
    return converted;
}
coder04 coder04

2015/2/17

#
Its says .class is expected
1
if (char<=64 || (char>90 && char<=96) || char>122) return c; // not an alpha character
danpost danpost

2015/2/17

#
Actually, these methods are not particular to any world or actor. You should be able to call them from anywhere at anytime. Adding a static 'Cipher' class might be the way to go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static class Cipher
{
    public static char convertChar(char c, int offset)
    {
        if (c<=64 || (c>90 && c<=96) || c>122) return c;
        return (char)(32*((int)c)/32)+1+(((int)c-1)%32+offset+26)%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;
    }
}
Then to convert a string, use;
1
2
3
String codedString = Cipher.converString("any text or string expression", key);
// or
String decodedString = Cipher.convertString(codedString, -key);
You can also use the 'convertChar' method to code/decode individual characters.
danpost danpost

2015/2/17

#
coder04 wrote...
Its says .class is expected
1
if (char<=64 || (char>90 && char<=96) || char>122) return c; // not an alpha character
Oops. 'char' should be 'c'. See my last post.
coder04 coder04

2015/2/17

#
1
return (char)(32*((int)c)/32)+1+(((int)c-1)%32+offset+26)%26);
Apparently when i add it to the textfield class on the code you showed me above the one above this bit needs ; expected
danpost danpost

2015/2/17

#
coder04 wrote...
1
return (char)(32*((int)c)/32)+1+(((int)c-1)%32+offset+26)%26);
Apparently when i add it to the textfield class on the code you showed me above the one above this bit needs ; expected
Looking into it.
danpost danpost

2015/2/17

#
Alright, the final class code follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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-base-1)%26)+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;
    }
}
coder04 coder04

2015/2/17

#
so i put it it into a separate class and delete the act and type this correct?
coder04 coder04

2015/2/17

#
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
There are more replies on the next page.
1
2
3