Andtoid Open Source
RSA Android Java
To encrypt text using RSA, you need to write quite a lot of code. It is not comfortable. I tried to make a separate class for working with RSA.
It turned out like this. But this is the first option. We will improve it.
//=====================================================
//
// This example shows how easy it is to generate RSA 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
//
//=====================================================
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
// Original text (RSA)
public static String testText = "Open Source Java Project Valery Shmelev OFLAMERON";
public static Context Maincontext;
@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
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);
originalTextView.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];
// ============================================================
// ============================================================
// Encode the original text with RSA private key
// ============================================================
RSACode rsacde = new RSACode(); // Class instance RSACode
encodedBytes = rsacde.RSATextEncode(publicKey, privateKey, testText); //Encode text via RSACode.java class
TextView encodedTextView = (TextView)findViewById(R.id.textViewEncoded);
encodedTextView.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
rsacde.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
//--------------------------------------------------------
RSALib rsalib = new RSALib(); // Class instance RSALib
str2 = rsalib.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(); //Write to an X.509 encoded publicKey byte array
privateKeyBytes = privateKey.getEncoded(); //Write to array of privateKey bytes encoded in PKCS#8
str = Base64.encodeToString(publicKeyBytes, Base64.DEFAULT); //Convert Byte Array (Public Key) to String
rsalib.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
rsalib.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
RSACode rsaload = new RSACode(); // Class instance RSACode
str3 = rsaload.Load("key.pub", str3, Maincontext); //Here we read and decode Public Key (RSACode class)
str4 = rsaload.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
//--------------------------------------------------------
RSACode rsacd = new RSACode(); // Class instance RSACode
Key[] KeyMass = new Key[2]; //An array of two keys to return values from a method
KeyMass = rsacd.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);
//--------------------------------------------------------
// 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
RSACode rsadecode = new RSACode(); // Class instance RSACode
decodedBytes = rsadecode.RSATextDecode(KeyMass[0], KeyMass[1], encodedBytes); //Text decoding (publicKey = KeyMass[0], privateKey = KeyMass[1])
TextView decodedTextView = (TextView)findViewById(R.id.textViewDecoded);
decodedTextView.setText("[DECODED]:\n" + new String(decodedBytes) + "\n"); //Show decoded text
} //OnCreate
But this class can save and restore key pairs. Then we will add forwarding (exchange) of keys.
While the keys are stored in files in the sandbox. Then we will add work with storage.
This is a working version with debugging. There are a lot of extra lines.
ReplyDelete