· 8 years ago · Dec 02, 2017, 04:26 AM
1private String prompt = "Enter message to encode:";
2private String message = "";
3private String encryptionKey = "";
4private String decryptionKey = "";
5private String encryptedMessage = "";
6private String decryptedMessage = "";
7private int progress = 0;
8
9public void setup()
10{
11 size(400, 400);
12 textAlign(CENTER);
13}
14
15public void draw()
16{
17 background(255);
18 noStroke();
19 fill(0);
20
21 text(prompt, width / 2, 20);
22 text(message, width / 2, 40);
23
24 if (progress > 0)
25 {
26 text("Enter a secret key to encode your message:", width / 2, 60);
27 text(encryptionKey, width / 2, 80);
28 }
29
30 if (progress > 1)
31 {
32 text("Your encrypted message:", width / 2 , 100);
33 fill(255, 0, 0);
34 text(encryptedMessage, width / 2, 120);
35
36 fill(0);
37 text("Enter a secret key to decode your message:", width / 2 , 140);
38 text(decryptionKey, width / 2, 160);
39 }
40
41 if (progress > 2)
42 {
43 text("Your decrypted message:", width / 2 , 180);
44 fill(0, 255, 0);
45 text(decryptedMessage, width / 2, 200);
46 }
47}
48
49public void keyPressed()
50{
51 if (keyCode == BACKSPACE && !message.isEmpty())
52 {
53 if (progress == 0)
54 {
55 message = message.substring(0, message.length() - 1);
56 }
57 else if (progress == 1)
58 {
59 encryptionKey = encryptionKey.substring(0, encryptionKey.length() - 1);
60 }
61 else if (progress == 2)
62 {
63 decryptionKey = decryptionKey.substring(0, decryptionKey.length() - 1);
64 }
65 }
66 else if (keyCode == ENTER)
67 {
68 if (progress == 1)
69 {
70 int k = Integer.parseInt(encryptionKey);
71 encryptedMessage = encrypt(message, k);
72 }
73 else if (progress == 2)
74 {
75 int k = Integer.parseInt(decryptionKey);
76 decryptedMessage = decrypt(encryptedMessage, k);
77 }
78
79 progress++;
80 }
81 else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT)
82 {
83 if (progress == 0)
84 {
85 message += key;
86 }
87 else if (progress == 1)
88 {
89 encryptionKey += key;
90 }
91 else if (progress == 2)
92 {
93 decryptionKey += key;
94 }
95 }
96}
97
98public String encrypt(String message, int secretKey)
99{
100 String out = "";
101
102 // TODO: Implement encryption algorithm
103
104 return out;
105}
106
107public String decrypt(String message, int secretKey)
108{
109 String out = "";
110
111 // TODO: Implement decryption algorithm
112
113 return out;
114}