Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:00:45+00:00 2026-06-01T08:00:45+00:00

I am using a Java code that I found that generates a public and

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:00:46+00:00Added an answer on June 1, 2026 at 8:00 am

    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");  
            } 
        }); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Java code that connects to an Oracle database using DriverManager.getConnection(). It
I am writing some Java code that authenticates to Active Directory using SASL GSSAPI.
I want to run Prolog code using Java. I found some engines, but the
I am developing a java code that signs documents using a certificate token. So
I have some Java code that validates XML against an XSD. I am using
I am making UI in android using java code because all UI information comming
I created the Layout design using java code only not from the XML Layout
I want to call shell script on windows environment using java code. I am
I am running a shell command from Java code using ProcessBuilder.start() I need a
In my Java code, I am using Guava's Multimap ( com.google.common.collect.Multimap ) by using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.