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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:02:27+00:00 2026-05-16T21:02:27+00:00

I have generated on my android application a pair of RSA Keys. I receive

  • 0

I have generated on my android application a pair of RSA Keys.

I receive from a web service
– an AES Key, encrypted with my RSA public key
– a String encoded with the AES key.

So I must do the following:
– decrypt the AES Key
– decrypt the string with the obtained AES Key.

To generate the RSA Keys I did:

 keyGen = KeyPairGenerator.getInstance("RSA");
  keyGen.initialize(size);
  keypair = keyGen.genKeyPair();
  privateKey = keypair.getPrivate();
  publicKey = keypair.getPublic();

On RSA decrypt I use :

public static byte[] decryptRSA( PrivateKey key, byte[] text) throws Exception
      { 
          byte[] dectyptedText = null;

          Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
          cipher.init(Cipher.DECRYPT_MODE, key);
          dectyptedText = cipher.doFinal(text);
          return dectyptedText;
      }

On AES decrypt I use:

public static byte[] decryptAES(byte[] key, byte[] text) throws Exception {   
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");   
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS1Padding");   
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);   
            byte[] decrypted = cipher.doFinal(text);   
            return decrypted;   
        }

So, in my code, to obtain the decrypted AES Key I do

byte[] decryptedAESKey = sm.decryptRSA(key, Base64.decode(ReceivedBase64EncryptedAESKey));
byte[] decryptedString = sm.decryptAES(decryptedAESKey, Base64.decode(ReceivedEncryptedAESString));

On the end I get a null for decryptedString.
What am I doing wrong ?

  • 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-16T21:02:28+00:00Added an answer on May 16, 2026 at 9:02 pm

    Well, the thing is that the key decrypted was 8 byte long and I had to make it 16 byte to be AES 128 bits compatible

    So, I made a method to convert it back

     private static byte[] GetKey(byte[] suggestedKey)
          {
              byte[] kRaw = suggestedKey;
              ArrayList<Byte> kList = new  ArrayList<Byte>();
    
              for (int i = 0; i < 128; i += 8)
              {
                  kList.add(kRaw[(i / 8) % kRaw.length]);
              }
    
              byte[] byteArray = new byte[kList.size()];
              for(int i = 0; i<kList.size(); i++){
                byteArray[i] = kList.get(i);
              }
              return byteArray;
          }
    

    And the rewritten decrypt method:

      public static byte[] decryptAES(byte[] key, byte[] text) throws Exception {   
    
              SecretKeySpec skeySpec = new SecretKeySpec(GetKey(key), "AES");
    
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");  
    
                byte [] iv = new byte[cipher.getBlockSize()];
                for(int i=0;i<iv.length;i++)iv[i] = 0;
                IvParameterSpec ivSpec = new IvParameterSpec(iv);
                cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
    
                byte[] decrypted = cipher.doFinal(text);   
                return decrypted;   
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have generated a Google Maps API key for my Android application. It works
I make Android application which must receive data from the page http://example.com/get.php?id=1 . For
I have to access a existing SOAP webservice from an Android application. I have
I have written an Android Application and the generated .APK file I uploaded in
I have generated the keystore and its inner key. Then I have my signed
i have developed an iphone application where i have generated a terms and condition
In my android application I have created 2 projects. The main project and a
I have a android webservice client application. I am trying to use the java
In my android application , i have three pages A,B,C.All the three pages have
I have created an application in Android that needs to send emails to all

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.