-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
145 lines (131 loc) · 7.03 KB
/
MainActivity.java
File metadata and controls
145 lines (131 loc) · 7.03 KB
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
package com.example.encryption;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import javax.crypto.SecretKey;
public class MainActivity extends AppCompatActivity {
private EditText inputMessage;
private EditText ciphEdt;
private EditText key_dt;
private EditText algorithm;
private Button btnEncrypt;
private Button btnDecrypt;
private TextView tV1;
private SecretKey aesKey;
private PublicKey publicKey;
private PrivateKey privateKey;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Koppla XML-elementer till Java-variabler
inputMessage = findViewById(R.id.inputMessage);
ciphEdt = findViewById(R.id.ciphEdt);
key_dt = findViewById(R.id.key_dt);
algorithm = findViewById(R.id.algorithm);
btnEncrypt = findViewById(R.id.btnencrypt);
btnDecrypt = findViewById(R.id.btndecrypt);
tV1 = findViewById(R.id.tV1);
try {
// Lyssnare för krypteringsknapp
btnEncrypt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String originalText = inputMessage.getText().toString().trim();
String cipherText = ciphEdt.getText().toString().trim();
String keyLengthStr = key_dt.getText().toString().trim();
String selectedAlgorithm = algorithm.getText().toString().trim();
// Kontrollera att användaren har fyllt i alla fält
if (!originalText.isEmpty() && !keyLengthStr.isEmpty() && !selectedAlgorithm.isEmpty()) {
try {
int keyLength = Integer.parseInt(keyLengthStr);
// Kryptering med AES
if (selectedAlgorithm.equalsIgnoreCase("AES")) {
aesKey = EncryptionUtils.generateAESKey(keyLength);
if (!originalText.equals(cipherText)) {
cipherText = EncryptionUtils.encryptAES(originalText, aesKey);
ciphEdt.setText(cipherText);
}
}
// Kryptering med RSA
else if (selectedAlgorithm.equalsIgnoreCase("RSA")) {
if (keyLength == 128 || keyLength == 256) {
KeyPair rsaKeyPair = EncryptionUtils.generateRSAKeyPair(keyLength);
publicKey = rsaKeyPair.getPublic();
privateKey = rsaKeyPair.getPrivate();
cipherText = EncryptionUtils.encryptRSA(originalText, publicKey);
ciphEdt.setText(cipherText);
} else {
Toast.makeText(MainActivity.this, "Invalid RSA Key Length", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Invalid/Incorrect Algorithm selected", Toast.LENGTH_SHORT).show();
}
// Validation/Error check av inputs från användare
tV1.setText("Encrypted Text: " + cipherText);
} catch (NumberFormatException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Invalid Key Length", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Encryption failed", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Fill in all fields", Toast.LENGTH_SHORT).show();
}
}
});
// Lyssnare för dekrypteringsknapp
btnDecrypt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String cipherText = ciphEdt.getText().toString().trim();
String keyLengthStr = key_dt.getText().toString().trim();
String selectedAlgorithm = algorithm.getText().toString().trim();
// Kontrollera att användaren har fyllt i alla fält
if (!cipherText.isEmpty() && !keyLengthStr.isEmpty() && !selectedAlgorithm.isEmpty()) {
try {
int keyLength = Integer.parseInt(keyLengthStr);
// Avkryptering med AES
if (selectedAlgorithm.equalsIgnoreCase("AES")) {
String decryptedText = EncryptionUtils.decryptAES(cipherText, aesKey);
tV1.setText("Decrypted Text: " + decryptedText);
}
// Avkryptering med RSA
else if (selectedAlgorithm.equalsIgnoreCase("RSA")) {
if (keyLength == 128 || keyLength == 256) {
String decryptedText = EncryptionUtils.decryptRSA(cipherText, privateKey);
tV1.setText("Decrypted Text: " + decryptedText);
} else {
Toast.makeText(MainActivity.this, "Invalid RSA Key Length", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Invalid/Incorrect Algorithm selected", Toast.LENGTH_SHORT).show();
}
} catch (NumberFormatException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Invalid Key Length", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Decryption failed", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Fill in all fields", Toast.LENGTH_SHORT).show();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
// Krpytering appen och koden är skriven av Ali Ferzali.