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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:12:28+00:00 2026-06-02T11:12:28+00:00

I have problem in below code. I have created two methods ENCRYPT and DECRYPT

  • 0

I have problem in below code.
I have created two methods ENCRYPT and DECRYPT for RSA. To get public and private key, I used keyGenerator and KeyPair to generate keys. Our senior told me that KeyPair should be called where us use it, it should not be as declared before the methods.

I tried to create separate method for key generation but when I call that method in both methods( ENCRYPT and DECRYPT ), its going to generate two different keys which does not match hence decryption isn’t working.

I have been told to create constructor and then call it in the method. I don’t know how exactly to call it and how it is going to work.

Please take a look at my code below and help me. Thank you.

public class Encryption

{

static byte[] encrypted;    
public Encryption() throws NoSuchAlgorithmException, NoSuchProviderException
{
    KeyPair keypair;
    KeyPairGenerator keygenerator = KeyPairGenerator.getInstance("RSA");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    keygenerator.initialize(1024, random);
    keypair = keygenerator.generateKeyPair();
}

public String ENCRYPT(String Algorithm, String Data ) throws Exception
{
    String alg = Algorithm;
    String data=Data;
     if(alg.equals("RSA"))
    {   
        stack enc=new stack();
        //Don't know how to call constructor here
        PublicKey publicKey = keypair.getPublic();
        Cipher cipher;
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
         encrypted = cipher.doFinal(data.getBytes());
        System.out.println("Encrypted String -> " + asHex(encrypted));
    }

     return asHex(encrypted);
}
public String DECRYPT(String Algorithm, String Data ) throws Exception
{   
String alg = Algorithm;
String Decrypted="";
if(alg.equals("RSA"))
{   
    //have to call constructor here to get keypair value
    PrivateKey privateKey = keypair.getPrivate();
    Cipher cipher;  
    cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    byte[] dec = cipher.doFinal(encrypted);
    Decrypted=new String(dec);
    System.out.println("Decrypted String[RSA] -> " + Decrypted);

}

return Decrypted.toString();
}
public static String asHex (byte buf[])
{
    StringBuffer strbuf = new StringBuffer(buf.length * 2);
    int i;
    for (i = 0; i < buf.length; i++) 
    {
    if (((int) buf[i] & 0xff) < 0x10)
    strbuf.append("0");
    strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
    }

    return strbuf.toString();
}

}
  • 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-02T11:12:32+00:00Added an answer on June 2, 2026 at 11:12 am

    You want keypair to be a private field:

    private KeyPair keypair;
    

    initialized in the constructor. The initialization is just what you already have:

    keypair = keygenerator.generateKeyPair();
    

    Do not declare it as a local variable in the constructor.

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

Sidebar

Related Questions

I have some problem with my cfml website. I have used the below code
I have a problem sending POST request with VB6. The code below works properly
I have a really weird problem with fgets() in C. Below is the code
I have problem with android 4.0.3. I'm using the method below to get local
Solution to original problem (below) may have been discovered. I commented out <identity> ...
I have a problem with the query below in postgres SELECT u.username,l.description,l.ip,SUBSTRING(l.createdate,0,11) as createdate,l.action
I have a problem with the SQL statement detailed below. The query returns the
i have a problem with a scripts below and How to make multification results
I have a problem for querying records into mysql explanation below The user could
I have a problem when trying to execute this update statement (below) using C#

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.