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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:10:03+00:00 2026-06-06T17:10:03+00:00

i m writing one application which encrypt and decrypt data using AES (ECB) mode.

  • 0

i m writing one application which encrypt and decrypt data using AES (ECB) mode.
Encrypted data of blackberry code successfully decrypt by php code.
but problem is that when i get encrypted text from php i m not able to decrypt it with blackberry code..even i m not getting any exception.

here is my code to encrypt and decrypt text.

public static  byte[] encrypt( byte[] keyData, byte[] data )
throws CryptoException, IOException
{
    // Create the AES key to use for encrypting the data.
    // This will create an AES key using as much of the keyData
    // as possible.
    AESKey key = new AESKey( keyData );

    // Now, we want to encrypt the data.
    // First, create the encryptor engine that we use for the actual
    // encrypting of the data.
    AESEncryptorEngine engine = new AESEncryptorEngine( key );

    // Since we cannot guarantee that the data will be of an equal block
    // length we want to use a padding engine (PKCS5 in this case).
    PKCS5FormatterEngine fengine = new PKCS5FormatterEngine( engine );

    // Create a BlockEncryptor to hide the engine details away.
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    BlockEncryptor encryptor = new BlockEncryptor( fengine, output );



    encryptor.write( data );

    encryptor.close();
    output.close();


    return output.toByteArray();
}
public static String AESDecryption(byte[] keyData,byte[] cipherText, int dataLength ) throws CryptoException, IOException {

    // Create the input stream based on the ciphertext
    ByteArrayInputStream in = new ByteArrayInputStream( cipherText, 0, dataLength );

    // Now create the block decryptor and pass in a new instance
    // of an AES decryptor engine with the specified block length
    BlockDecryptor cryptoStream = new BlockDecryptor(new AESDecryptorEngine( new AESKey( keyData )), in );

    byte[] T= new byte[dataLength];
    // Read the decrypted text from the AES decryptor stream and
    // return the actual length read

    int length= cryptoStream.read( T );
  String str= new String(T);


  return str;
}

thanks in advance..

  • 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-06T17:10:05+00:00Added an answer on June 6, 2026 at 5:10 pm

    Here is the method that accepts key and encrypted data (in the form of byte arrays) and returns the decrypted data in the form of byte array:

    public static byte[] decrypt(byte[] keyData, byte[] ciphertext) 
             throws CryptoException, IOException {
    
        // First, create the AESKey again.
        AESKey key = new AESKey(keyData);
    
        // Now, create the decryptor engine.
        AESDecryptorEngine engine = new AESDecryptorEngine(key);
        // Since we cannot guarantee that the data will be of an equal block length
        // we want to use a padding engine (PKCS5 in this case).
        PKCS5UnformatterEngine uengine = new PKCS5UnformatterEngine(engine);
    
        // Create the BlockDecryptor to hide the decryption details away.
        ByteArrayInputStream input = new ByteArrayInputStream(ciphertext);
        BlockDecryptor decryptor = new BlockDecryptor(uengine, input);
    
        // Now, read in the data.
        byte[] temp = new byte[100];
        DataBuffer buffer = new DataBuffer();
    
        for (;;) {
            int bytesRead = decryptor.read(temp);
            buffer.write(temp, 0, bytesRead);
    
            if (bytesRead < 100) {
                // We ran out of data.
                break;
            }
        }
    
        byte[] plaintext = buffer.getArray();
    
        return plaintext;
    }
    

    Note encryption/decryption does not operate with strings directly. It only work with byte arrays. So as a final action you need to convert decrypted byte array to string using String(byte[] bytes) or String(byte[] bytes, String enc) constructor. The second constructor can be useful if your string were encoded with any other than “ISO-8859-1” encoding (which is a default one for BlackBerry).

    UPDATE:

    As it turns out you don’t use PKCS5 padding on server side AND you convert encrypted byte data into a HEX string, then the solution code is:

    // this is to convert HEX to bytes
    public static byte[] convertHexToBytes(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
    
    // this is the same as initial version, but we don't handle PKCS5 padding here
    public static byte[] decrypt(byte[] keyData, byte[] ciphertext)
            throws CryptoException, IOException {
    
        // First, create the AESKey again.
        AESKey key = new AESKey(keyData);
    
        // Now, create the decryptor engine.
        AESDecryptorEngine engine = new AESDecryptorEngine(key);
    
        // Create the BlockDecryptor to hide the decryption details away.
        ByteArrayInputStream input = new ByteArrayInputStream(ciphertext);
        BlockDecryptor decryptor = new BlockDecryptor(engine, input);
    
        // Now, read in the data.
        byte[] temp = new byte[100];
        DataBuffer buffer = new DataBuffer();
    
        for (;;) {
            int bytesRead = decryptor.read(temp);
            buffer.write(temp, 0, bytesRead);
    
            if (bytesRead < 100) {
                // We ran out of data.
                break;
            }
        }
    
        byte[] plaintext = buffer.getArray();
    
        return plaintext;
    }
    
    // and finally :)
    String key =       "1234567890123456";
    String encrypted = "48b983c4f1575280d244b74cf689efe5";
    
    byte[] keyBytes = key.getBytes();
    byte[] encryptedBytes = convertHexToBytes(encrypted);
    
    // displays "nirav bhandari"
    Dialog.inform(new String(decrypt(keyBytes, encryptedBytes)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an application which have two modes; one view mode and one edit
When writing a web application which allows the upload of files, one must be
I am writing a web-application using Play 1.2.3 . One of the feature is
I'm writing a Java application with GUI using Swing. One of the GUI components
I am writing a very simple application which allows one to change the temperature.
I am writing an application which tests email addresses, for one part of this
I'm writing an aggregation application which scrapes data from a couple of web sources
I'm writing an application which aims to centralize all data related to project management
I'm writing an application which needs to host several WCF services. One of the
I'm currently writing a web application which uses forms and PHP $_POST data (so

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.