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 7688759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:59:51+00:00 2026-05-31T19:59:51+00:00

I am trying to generate my first public/private key pair for an RSA encryption.

  • 0

I am trying to generate my first public/private key pair for an RSA encryption. This is my first time doing so but through looking at various tutorials and website i’ve decided to do so with the following code. Although my code does not give me errors, it force closes. Everything is posted including my imports, can sombody please help me understand why my code is not generating keys and giving me errors? And yes i did declare it in the AndroidManifest.xml file

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

    public class RSA {
        public static void GenerateKeyPair() {
            try {
                KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
                kpg.initialize(4096);
                KeyPair kp = kpg.genKeyPair();

                KeyFactory fact = KeyFactory.getInstance("RSA");
                RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(),
                        RSAPublicKeySpec.class);
                RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(),
                        RSAPrivateKeySpec.class);

                saveToFile("public.key", pub.getModulus(), pub.getPublicExponent());
                saveToFile("private.key", priv.getModulus(),
                        priv.getPrivateExponent());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }

        public static void saveToFile(String fileName, BigInteger mod,
                BigInteger exp) throws Exception {
            ObjectOutputStream oout = new ObjectOutputStream(
                    new BufferedOutputStream(new FileOutputStream(fileName)));
            try {
                oout.writeObject(mod);
                oout.writeObject(exp);
            } catch (Exception e) {
                throw new Exception("error", e);
            } finally {
                oout.close();
            }
        }
    }


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.BLAH"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".UUIDActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Installation"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".RSA"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>
  • 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-05-31T19:59:52+00:00Added an answer on May 31, 2026 at 7:59 pm

    I don’t know what’s causing your problem (we’d have to see where you use this class to debug that), but I do have an alternative for you, if you can include a 3rd party library. See JSch, which can generate RSA keypairs (e.g. for use in SSH public-key authentication). Documentation: http://epaul.github.com/jsch-documentation/simple.javadoc/

    The method you’re looking for is KeyPair.genKeyPair.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to import an RSA public/private key pair generated with the Win32 Crypto
I am trying to use the Google generated API library for the first time
While trying to generate classes from a xsd, i got this error: java.lang.IllegalArgumentException: Illegal
Trying to make this simple applet - the first part just creates a simple
I'm working my way through 'head first design patterns' and want to use this
I'm trying to use Entity Framework for the first time, and have come a
This is my first post, so hope I haven't missed anything important. I'm doing
I am new to AES encryption but trying to build a solution which: Accepts
Trying to rewrite machine generated my first ever WP7 HelloWorld into something more sophisticated.
Im trying to generate a color gradient using ColdFusion. My current code below works

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.