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

  • Home
  • SEARCH
  • 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 8047299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:03:31+00:00 2026-06-05T06:03:31+00:00

In my program, I’m trying to encrypt some plaintext with RSA using the following

  • 0

In my program, I’m trying to encrypt some plaintext with RSA using the following code:

static String RSAEncrypt(String pubkey, String plain){
    return encrypt(pubkey,plain,"RSA");
}
static String encrypt(String stringKey, String plain, String algo){
    String enc="failed";
    try{
    byte[] byteKey  = new BASE64Decoder().decodeBuffer(stringKey);
    Key key = new SecretKeySpec(byteKey,algo);

    byte[] data = plain.getBytes();
    Cipher c = Cipher.getInstance(algo);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(data);
    enc = new BASE64Encoder().encode(encVal);
    }catch(Exception e){e.printStackTrace();}

    return enc;
}

However, when it runs, it shows the following error:

java.security.InvalidKeyException: No installed provider supports this key: javax.crypto.spec.SecretKeySpec
at javax.crypto.Cipher.chooseProvider(Cipher.java:877)
at javax.crypto.Cipher.init(Cipher.java:1212)
at javax.crypto.Cipher.init(Cipher.java:1152)
at Crypto.encrypt(Crypto.java:37)
at Crypto.RSAEncrypt(Crypto.java:62)

I have tried changing it to RSA/None/PKCS1Padding and RSA/ECB/PKCS1Padding to no avail.. I know that installing BouncyCastle may help but I’d like to avoid it (I’d like to avoid more dependencies and I’ve been having some issues installing it anyway). Thanks in advance for any ideas.

  • 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-05T06:03:32+00:00Added an answer on June 5, 2026 at 6:03 am

    As was said in the comments, SecretKeySpec is for symmetric algorithms only. You mentioned that you got your byte[] containing the key by calling getEncoded.

    There are two possibilities and two resulting formats:

    Encoding of an RSA PrivateKey

    Calling PrivateKey#getEncoded on an instance of an RSA private key will result in a PKCS#8 encoding for private keys, and it can be restored with the help of PKCS8EncodedKeySpec.

    Encoding of an RSA PublicKey

    PublicKey#getEncoded on an RSA public key results in the generic X.509 public key encoding, and can be restored with X509EncodedKeySpec.

    Example Usage

    byte[] data = "test".getBytes("UTF8");
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(512);
    KeyPair keyPair = kpg.genKeyPair();
            
    byte[] pk = keyPair.getPublic().getEncoded();
    X509EncodedKeySpec spec = new X509EncodedKeySpec(pk);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey pubKey = keyFactory.generatePublic(spec);
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    byte[] encrypted = cipher.doFinal(data);
            
    byte[] priv = keyPair.getPrivate().getEncoded();
    PKCS8EncodedKeySpec spec2 = new PKCS8EncodedKeySpec(priv);
    PrivateKey privKey = keyFactory.generatePrivate(spec2);
    cipher.init(Cipher.DECRYPT_MODE, privKey);
    byte[] plain = cipher.doFinal(encrypted);
            
    System.out.println(new String(plain, "UTF8")); //=> "test"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

{ class Program { static void Main(string[] args) { int id = 0, stock
class Program { static void Main(string[] args) { Console.WriteLine(Fib 1: ); Console.ReadLine(); } long
Program returns an error of, expected PWideChar instead of string procedure TForm1.Button1Click(Sender: TObject); var
My program gives this error under gdb: During startup program exited with code 0xc0000135.
class Program { static bool? a = null; static bool b = false; static
program s; type info = record name, surname: string; min, sec: integer; end; arrays
Program: program s; type info = record name, surname: string; min, sek: integer; end;
I program in Java and have been trying to understand exactly what operator overloading
Program : List all C files in the current folder using execlp() system call:
/*Program to merge to arrays using pointers in descending order, when the first array

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.