RSA and AES Encryption

There are many Android Java programmers who know how to use RSA data encryption. For example, to transfer from a client to a server. But very few programmers know how to protect encryption / decryption keys, securely transfer keys over communication channels and store information in databases in encrypted form.

There are also methods for protecting the private key during storage and transfer from device to device.

Why do it?

The client can make his data that he transmits to the server (for example, during registration) inaccessible to the owners of the server.

At the same time, it is necessary to transfer the private key in order to decrypt the data on the client device. This can be considered a disadvantage of technology. But in some cases, this can be easily corrected.


Freelance

A set of Java classes for working with PCA and AES encryption, databases, for storing and restoring keys, for obfuscating code, keys and data. The classes are written so that programming the encryption process is easy. All Java classes are available in open source, with comments and instructions for use.

// ============================================================

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

// ============================================================

// Encode the original text with RSA private key

// ============================================================

//////RSACode rsacde = new RSACode(); // Class instance RSACode

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

//--------------------------------------------------------

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


This is only a part of the class methods for working with RSA encryption.

Feedback https://drive.google.com/file/d/1EE0iOECbGcJYg7d-qqXcVmJuJLOx_RM0/view?usp=sharing

If you are an Android Java programmer, it will not be difficult to download this little Java project that will show you an email.

Nnational fonts processing


Direct use of class methods for RSA encryption of texts with national alphabets is only possible for the English language. For example, for the Czech language or Polish, this will not work. When decrypting, there will be text distortions.

// Repositories https://github.com/vallshmeleff/

The operation of methods for working with national alphabets (for example, Czech) has been successfully tested. Can be used with different encryption algorithms (for example, RSA or AES). Used to convert to UNICODE and vice versa. The Android Java source code will be placed in the GitHUB repository.


Android Java SMS

A Java code for sending SMS and receiving SMS on an Android smartphone has been written and successfully tested. After the code is correctly formatted, it will be possible to write an SMS messenger with RSA encryption. RSA is a slow encryption algorithm, but for an SMS messenger this is not critical. Such a messenger does not require a centralized server for management and control and is more resistant to data blocking or interception.


Bundesrepublik Deutschland