-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainActivity.java
More file actions
293 lines (245 loc) · 16.1 KB
/
MainActivity.java
File metadata and controls
293 lines (245 loc) · 16.1 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
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
package com.example.rsaeaescrypto;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.security.Key;
//=====================================================
//
// This example shows how easy it is to generate PCA key pairs,
// encrypt text, write text, read text from a file, decode text,
// store encryption keys, and recover encryption keys. If you have
// a special class RSACode.java
// Supports national alphabets (as an example - Czech)
//
// 10.11.2022 - Large Text to Encryption Support - RSACode.java
//
// eFragment = eFragment(eText); // Fragmentation
// splitted = eDEFragment(toDecode); // Defragmentation
//
//=====================================================
public class MainActivity extends AppCompatActivity {
//=====================================================
//
// RSA and Write encoded text to file and Read from file oflameron.txt
// rsaload.Load(FILENAME, str2)
//
//=====================================================
final static String LOG_TAG = "myLogs";
public static String str=" "; //File contents oflameron.txt
public static String str2=" "; //File contents oflameron.txt
public static String str3=" "; //File contents key.txt - public key
public static String str4=" "; //File contents pkey.txt - private key
public static String FILENAME = "oflameron.txt";//File for writing encoded data
public static String Content = "EditText Content";//String variable for text copy
public static Key publicKey = null; //RSA
public static Key privateKey = null; //RSA
public static Key publicKey2 = null; //RSA
public static Key privateKey2 = null; //RSA
public static Key kpprivateKey = null; //RSA restored Private Key
public static Key kppublicKey = null; //RSA restored Public Key
public static byte[] privateKeyBytes = null; //RSA
public static byte[] publicKeyBytes = null; //RSA
public static byte[] encodedBytes = null; //RSA
public static byte[] decodedBytes = null; //RSA
public static byte[] fragmentBytes = null; //RSA
public static byte[] decodefragmentBytes = null; //RSA
// Original text (RSA)
public static String testText = "Open Source Java Project Valery Shmelev OFLAMERON. Česká Republika";
public static String gtestText = "WiKi: The Czech Republic, also known as Czechia, is a landlocked country in Central Europe. Historically known as Bohemia, it is bordered by Austria to the south, Germany to the west, Poland to the northeast, and Slovakia to the southeast. The Czech Republic has a hilly landscape that covers an area of 78,871 square kilometers (30,452 sq mi) with a mostly temperate continental and oceanic climate. The capital and largest city is Prague; other major cities and urban areas include Brno, Ostrava, Plzeň and Liberec.";
public static String etestText; // Text to encrypt - all or blocks of text
public String[] eFragment = new String[100]; // The array contains a large text to encrypt, divided into chunks
public String[] eeFragment = new String[100]; // The array contains a encrypted large text, divided into chunks
public static String EncodeLargeText = ""; // Full Encoded Large Text
public static Context Maincontext;
public ClipboardManager clipboard;
public static String DebugObfuscation = "13663457956709807959674645";
@RequiresApi(api = Build.VERSION_CODES.R)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Maincontext = getApplicationContext(); //To work with context
// ============= Array RESIZE =================================
String[] eLFragment = new String[100];
Log.d("== Array RESIZE ==", "== == 100 == ==" + eLFragment.length);
eLFragment = new String[190];
Log.d("== Array RESIZE ==", "== == 190 == ==" + eLFragment.length);
// ============================================================
// ============================================================
// Write to clipboard for export/import
// ============================================================
ClipboardManager myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData myClip;
String text = "Text Example for ClipBoard";
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
// ============================================================
Log.d(LOG_TAG, "== == Activity NAME == ==" + this.getClass().getSimpleName()); //The name of the current Activity - to access the context
//TextView originalTextView = (TextView) findViewById(R.id.TXTV);
EditText originalEditText = (EditText) findViewById(R.id.TXTV);
originalEditText.setMovementMethod(new ScrollingMovementMethod()); //Scrolling text + android:scrollbars = "vertical" in Activity_main.xml
originalEditText.setText("[ORIGINAL]:\n" + testText + "\n");
// ============================================================
// Generate key pair for 1024-bit RSA encryption and decryption
// ============================================================
RSACode rsagente = new RSACode(); // Class instance RSACode
Key[] KeyPMass = new Key[2]; //An array of two keys to return values from a method
KeyPMass = rsagente.RSAKeyGen(); //GENERATE Key Pair
publicKey = KeyPMass[0];
privateKey = KeyPMass[1];
// ============================================================
// Convert Text to CharCodeString =============================
String cyrtxt = rsagente.String2Code(testText);
Log.d(LOG_TAG, "== ==| Text to CharachterString |== ==" + cyrtxt);
// Convert CharCodeString to Text
String txtcyr = rsagente.Code2String(cyrtxt);
Log.d(LOG_TAG, "== ==| UNICODE CharachterString to TEXT|== ==" + txtcyr);
//=============================================================
// Here Text to Encode - in UNICODE format in txtcyr variable
//
// OBFUSCATION ================================================
String obfustxt = rsagente.ObfuscationD(DebugObfuscation, "5", "7");
rsagente.main();
//=============================================================
// ============================================================
// Encode the original LARGE text with RSA private key
// ============================================================
if (gtestText.length() >= 50) { // If LARGE text lenght > 50 bytes - LONG Text
String eFragment[] = new String[rsagente.eFragment(gtestText).length]; // Array eFragment[] RESIZE
String eeFragment[] = new String[eFragment.length]; // eFragment.lenght = eeFragment.lenght
eFragment = rsagente.eFragment(gtestText); // Fragmentation - eFragment[] Fragment Array
int el = eFragment.length; // Fragment number
int n = 0;
while (n < el) {
if (n == 0) {
EncodeLargeText = Base64.encodeToString(rsagente.RSATextEncode(publicKey, privateKey, eFragment[n]), Base64.DEFAULT);
}
if (n > 0) {
EncodeLargeText = EncodeLargeText + "<oflameron>" + Base64.encodeToString(rsagente.RSATextEncode(publicKey, privateKey, eFragment[n]), Base64.DEFAULT);
}
n++;
}
}
// ============================================================
// Split and Decode LARGE Encoded Text
// ============================================================
eeFragment = rsagente.eDEFragment(EncodeLargeText);
int g = eeFragment.length;
int h = 0;
String RestoreText = ""; // Restored Large Text
for(h = 0; h < g; h++) {
fragmentBytes = Base64.decode(eeFragment[h], Base64.DEFAULT);;
decodefragmentBytes = rsagente.RSATextDecode(publicKey, privateKey,fragmentBytes); //Text decoding (publicKey = KeyMass[0], privateKey = KeyMass[1])
RestoreText = RestoreText + new String(decodefragmentBytes);
}
Log.d("==LARGE Text==", "== ==| RESTORED Source Large Text |== == " + RestoreText); // Full RESTORED Original Text
// ============================================================
encodedBytes = rsagente.RSATextEncode(publicKey, privateKey, testText); //Encode text via RSACode.java class
/////////// encodedBytes = rsagente.RSATextEncode(publicKey, privateKey, testText); //Encode text via RSACode.java class
EditText encodedEditText = (EditText)findViewById(R.id.EditTextEncoded);
encodedEditText.setMovementMethod(new ScrollingMovementMethod()); //Scrolling text + android:scrollbars = "vertical" in Activity_main.xml
encodedEditText.setText("[ENCODED]:\n" + Base64.encodeToString(encodedBytes, Base64.DEFAULT) + "\n");
//--------------------------------------------------------
// Coded Text -> str -> Save to file
//--------------------------------------------------------
str = Base64.encodeToString(encodedBytes, Base64.DEFAULT); //Convert Byte Array to String
rsagente.Save("oflameron.txt",str, Maincontext); //Write Coded Text to file oflameron.txt from str
encodedBytes = null; // This line is optional. For debugging only
//--------------------------------------------------------
// Load Coded Text from file -> str2
//--------------------------------------------------------
//////RSACode rsalib = new RSACode(); // Class instance RSALib
str2 = rsagente.Load(FILENAME, str2, Maincontext); //Here we have erased the ciphertext from the variable encodedBytes. We have already written it to the file oflameron.txt in the module Save();. Now read and decode.
Log.d(LOG_TAG, "== == RSACode.RSA Readed Coded text == ==" + str2);
encodedBytes = Base64.decode(str2, Base64.DEFAULT); //Convert String to Byte Array
//--------------------------------------------------------
//--------------------------------------------------------
// The most important part of encryption/decoding is saving
// and restoring the public and private keys. Otherwise, after
// restarting the application, you will not be able to decrypt
// the encoded text, because new keys will be generated.
//
// Save Keys -> to file
//--------------------------------------------------------
publicKeyBytes = publicKey.getEncoded(); //Записать в массив байт publicKey, закодированный в X.509
privateKeyBytes = privateKey.getEncoded(); //Записать в массив байт privateKey, закодированный в PKCS#8
str = Base64.encodeToString(publicKeyBytes, Base64.DEFAULT); //Convert Byte Array (Public Key) to String
rsagente.Save("key.pub",str, Maincontext); //Write Public Key to file key.txt from str
str = Base64.encodeToString(privateKeyBytes, Base64.DEFAULT); //Convert Byte Array (Private Key) to String
rsagente.Save("pkey.pri",str, Maincontext); //Write Private Key to file pkey.txt from str
Log.d(LOG_TAG, "== == RSA Write Public Key to key.txt == ==" + str);
//encodedBytes = Base64.decode(str, Base64.DEFAULT);
publicKey = null; // This line is optional. For debugging only
privateKey = null; // This line is optional. For debugging only
str3 = rsagente.Load("key.pub", str3, Maincontext); //Here we read and decode Public Key (RSACode class)
str4 = rsagente.Load("pkey.pri", str4, Maincontext); //Here we read and decode Private Key (RSACode class)
//--------------------------------------------------------
// Referring to the special class RSACode.java
// To restore saved keys from files
//--------------------------------------------------------
Key[] KeyMass = new Key[2]; //An array of two keys to return values from a method
KeyMass = rsagente.RSAKeyReGenerate(str3, str4); // We pass to the method str3 and str4 - String from the file. Get the recovered keys as an array
publicKey = KeyMass[0];
privateKey = KeyMass[1];
Log.d(LOG_TAG, "== == RSACode.RSAKeyReGenerate.RSA Publickey and Privatekey Full RESTORED == ==" + publicKey + " "+ privateKey);
ClipBrdWrite(privateKey.toString()); //Write privateKey to ClipBoard
//--------------------------------------------------------
// If you run the application, you will see that the original text is correctly decoded.
// Those. we run the application and immediately encode the text and immediately decode it. Everything is working.
// ============================================================
// Decoding the ciphertext
// ============================================================
// Let's call a method from the class RSACode.java
decodedBytes = rsagente.RSATextDecode(KeyMass[0], KeyMass[1], encodedBytes); //Text decoding (publicKey = KeyMass[0], privateKey = KeyMass[1])
EditText decodedEditText = (EditText)findViewById(R.id.EditTextDecoded);
decodedEditText.setMovementMethod(new ScrollingMovementMethod()); //Scrolling text + android:scrollbars = "vertical" in Activity_main.xml
decodedEditText.setText("[DECODED]:\n" + new String(decodedBytes) + "\n"); //Show decoded text
Button btncpy = (Button)findViewById(R.id.button1);
} //OnCreate
//==========================================================
// Read text (RSA Key) from Android Clipboard
//==========================================================
public void ReadClipBrd() {
String text = "Primer RSA Key"; //RSA Key Text from clipboard
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData primaryClipData = clipboard.getPrimaryClip();
ClipData.Item item = primaryClipData.getItemAt(0);
text = item.getText().toString();
TextView decodedTextView = (TextView)findViewById(R.id.EditTextDecoded);
decodedTextView.setText("== RSA Key == " + text);
//return text; //Return RSA Key Text
}
public void onClickR(View v) { //For Button
ReadClipBrd(); // Read RSA Key from ClipBoard
}
//==========================================================
//==========================================================
// Write text to the Android Clipboard
//==========================================================
public void ClipBrdWrite(String text) {
ClipboardManager myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData myClip;
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
}
public void onClickW(View v) { //For Button
ClipboardManager myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData myClip;
myClip = ClipData.newPlainText("text", Content); // public static String
myClipboard.setPrimaryClip(myClip);
}
//==========================================================
// Crypto RSA (c) by Valery Shmelev https://www.linkedin.com/in/valery-shmelev-479206227/
// GNU GPL. Freelance Programmer
}