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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:24:25+00:00 2026-06-08T21:24:25+00:00

I am creating a cryptographically-secure IM application in Java. The first step (after establishing

  • 0

I am creating a cryptographically-secure IM application in Java. The first step (after establishing a connection) is to exchange a public key (RSA-4096) so that the other party can create a symmetric key (AES-256), encrypt it with the other party’s public key, send it over the connection, where the original party can decrypt the symmetric key with their private key and then both parties can exchange messages that are encrypted with the 256-bit key.

My problem is showing up at initializing the KeyPairGenerator, every time I call kpg.initialize(4096,sr) where kpg is the KeyPairGenerator‘s variable’s name, and sr is the SecureRandom I have created, the program crashes and returns a NullPointerException, here is the full code:

CryptoBox.java:

    package crypto;

    import java.security.*;

    public class CryptoBox {
       private static SecureRandom sr = new SecureRandom();
       private static KeyPairGenerator kpg;
       private static KeyPair kp;

       private static Key PubKey;
       private static Key PrivKey;

       @SuppressWarnings("static-access")
       public void init(int keySize){
          try {
               sr.getInstance("SHA1PRNG");
               kpg.getInstance("RSA");
          } catch (NoSuchAlgorithmException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
          }
          try {
               kpg.initialize(keySize,sr); // <-- NullPointerException  
          }catch(Exception e){
               e.printStackTrace();
        return;
          }

          kp = kpg.genKeyPair();
          PubKey = kp.getPublic();
          PrivKey = kp.getPrivate();
       }

    }

Main.java:

package main;

import crypto.CryptoBox;

public class Main {

/**
 * @param args
 */
public static void main(String[] args){
    // TODO Auto-generated method stub
    CryptoBox cb = new CryptoBox();
    cb.init(4096); // <-- NullPointerException
}

}

the full error message is:

java.lang.NullPointerException
at crypto.CryptoBox.init(CryptoBox.java:23)
at main.Main.main(Main.java:15)
  • 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-08T21:24:27+00:00Added an answer on June 8, 2026 at 9:24 pm

    Change sr.getInstance("SHA1PRNG"); to sr = KeyPairGenerator.getInstance("SHA1PRNG");

    The copmiler probably already told you your mistake, but you choose to ignore it. Do yourself a favor and remove @SuppressWarnings("static-access"), too, that’s what compiler errors are good for ;D

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

Sidebar

Related Questions

When creating an EditText in the java portion of the application, how do you
(creating a separate question after comments on this: Javascript redeclared global variable overrides old
Creating a simple RPG game, first time using XNA. Trying to get my character
I creating a web application using JSF,Hibernate,Spring. I have added a filter for checking
creating a filter for a select menu and having a few problems! the first
creating 20 viewcontrollers and calling them one after the other from the mainviewcontroller as
Creating a searchable contacts kind of app. Below is my code public class EmployeeList
Creating an Iphone application, I used to perform INSERT QUERY on different databases. But
Creating an index MySQL After I have added the FULLTEXT index, how do I
Creating a layout in Java as the number of TableLayouts required is not known

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.