2
votes

I am using a Java code that I found that generates a public and a private key via the bouncy castle library. My problem is implementing it into code runnable by my android device. My code does not display the RSA keys like I programmed it to and through most of my troubleshooting I am still unable to make my code do as I ask, although I get no errors. My suspicion is the way I put all of my code into a try/catch block but I am not really sure. Edit: Lower in code

This is the Java class that generates the RSA public and private key. (It works)

public class ClassMain {
    public static void main(String[]args) throws Exception {
        String ST = "Ebenezersawesome";
        byte[] plainText = "ST".getBytes("UTF8");
        // Generating RSA Key
        System.out.println("\nStart generating RSA key");
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(4096);
        KeyPair key = keyGen.generateKeyPair();
        System.out.println("Finish generating RSA key");
        //
        // Creates an RSA Cipher object (specifying the algorithm, mode, and
        // padding).
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        //
        // Print the provider information
        System.out.println("\n" + cipher.getProvider().getInfo());
        System.out.println("\nStart encryption");
        //
        // Initializes the Cipher object.
        cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
        //
        // Encrypt the plaintext using the public key
        byte[] cipherText = cipher.doFinal(plainText);
        System.out.println("Finish encryption: ");
        System.out.println(new String(cipherText, "UTF8"));
        System.out.println("\nStart decryption");
        //
        // Initializes the Cipher object.
        cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
        //
        // Decrypt the ciphertext using the private key
        byte[] newPlainText = cipher.doFinal(cipherText);
        System.out.println("Finish decryption: ");
        System.out.println(new String(newPlainText, "UTF8"));
    }
}

This is my attempt at trying to display the code in an android application. Edit: The code works but for some reason my try/catch stops and does not generate code in tv3.

  TextView tv1;
    TextView tv2;
    TextView tv3;
    Button convert;
    String publicKeyFilename = null;
    String privateKeyFilename = null;
    String ST = "Ebenezersawesome";

    @Override public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("Ebz", "Made it to onCreate");
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        convert = (Button) findViewById(R.id.button1);

        // tv2.setText(ST);
        convert.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Log.d("Ebz", "Made it to onCreate");
                try {
                    byte[] plainText = "ST".getBytes("UTF8");
                    Log.d("Ebz", "made it to Try Block");
                    KeyPairGenerator keyGen = KeyPairGenerator
                            .getInstance("RSA");
                    keyGen.initialize(2048);
                    KeyPair key = keyGen.generateKeyPair();
                    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
                    tv1.setText(cipher.getProvider().getInfo().toString());
                    Log.d("Ebz", "Made it passed tv1");
                    //tv1.setText(ST);
                    cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
                    byte[] cipherText = cipher.doFinal(plainText);
                    tv2.setText(new String(cipherText, "UTF8").toString());
                    Log.d("Ebz", "Made it passed tv2");
                    // tv2.setText(ST);
                    byte[] newPlainText = cipher.doFinal(cipherText);
                     //tv3.setText(new String(newPlainText, "UTF8").toString());
                    // tv3.setText(ST);
                    Log.d("Ebz", "Made it passed tv3");
                } catch (Exception e) {
                    System.out.println("error");
                }
            }
        });
    }
1
Maybe you could use SpongyCastle on Android. Note: 4096 bits .... welcome to the paranoia club ;-)Vincent Cantin
@The Obliviator, much better....you see people are already responding +ve'lyMayank
you said the code that generates RSA keys works, so what doesn't work???Mayank
I cant get the code to run on my device. I've organized the code in my onCreate method and organized my code, but still my keys do not display. I think i put my code in my try block wrong, but i am still playing with it trying to figure out whats up.The Tokenizer
did you tried putting an break point on ` tv3.setText(cipher.getProvider().getInfo().toString());` and maybe couple of other lines and see what's going on? I'll say that's easiest way to figure out what's wrong.Mayank

1 Answers

3
votes

Ok try following code instead yours and see what happens inside your try/catch block.

TextView tv1; 
TextView tv2; 
TextView tv3; 
Button convert; 
String publicKeyFilename = null; 
String privateKeyFilename = null; 
String ST = "Ebenezersawesome"; 

@Override public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Log.d("Ebz", "Made it to onCreate"); 
    tv1 = (TextView) findViewById(R.id.tv1); 
    tv2 = (TextView) findViewById(R.id.tv2); 
    tv3 = (TextView) findViewById(R.id.tv3); 
    convert = (Button) findViewById(R.id.button1); 

    try { 
                byte[]plainText = "ST".getBytes("UTF8"); 
                Log.d("Ebz", "made it to Try Block"); 
                KeyPairGenerator keyGen = 
                    KeyPairGenerator.getInstance("RSA"); 
                keyGen.initialize(4096); 
                KeyPair key = keyGen.generateKeyPair(); 
                Cipher cipher = 
                    Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
                tv3.setText(cipher.getProvider().getInfo().toString()); 
                //tv3.setText(ST); 
                cipher.init(Cipher.ENCRYPT_MODE, key.getPublic()); 
                byte[]cipherText = cipher.doFinal(plainText); 
                tv1.setText(new String(cipherText, "UTF8").toString()); 
                //tv1.setText(ST); 
                byte[]newPlainText = cipher.doFinal(cipherText); 
                tv2.setText(new String(newPlainText, "UTF8").toString()); 
                //tv2.setText(ST); 
            } catch(Exception e) { 
                System.out.println("error"); 
            }
    // tv2.setText(ST); 
    convert.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 
            Log.d("Ebz", "Made it to onCreate");  
        } 
    }); 
}