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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:39:06+00:00 2026-06-04T05:39:06+00:00

I wrote 2 functions in Java to encrypt and decrypt the message. I chose

  • 0

I wrote 2 functions in Java to encrypt and decrypt the message. I chose AES/CBC/PKCS7Padding as parameter. while encryption, my function can’t add the right padding(as PKCS7Padding, it shouldn’t add 0 to pad my message), so while decryption i can’t remove the 0 at the end of the message.
with code, my problem will be more clair

    public static  byte[] encryptStringToData(String message, String key){
        // transform the message from string to bytes
        byte[] array_to_encrypt, bkey;
        try {
            array_to_encrypt = message.getBytes("UTF8");
            bkey = key.getBytes("UTF8");
        } catch (UnsupportedEncodingException e1) {
            array_to_encrypt = null;
            bkey = null;
            return null;
        }

        bkey = Arrays.copyOf(bkey, 32);
        BlockCipher engine = new AESEngine();
        engine.init(true, new KeyParameter(bkey, 0, 32));
        PKCS7Padding pad = new PKCS7Padding() ;

        BufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),pad); 

        c.init(true, new ParametersWithIV(new KeyParameter(bkey), new byte[16]));

        byte[] encrypted_array = new byte[c.getOutputSize(array_to_encrypt.length)];

        int outputLen = c.processBytes(array_to_encrypt, 0, array_to_encrypt.length, encrypted_array, 0);
        try
        {
            c.doFinal(encrypted_array, outputLen);
        }
        catch (CryptoException ce)
        {
            System.err.println(ce);
            System.exit(1);
        }
        return encrypted_array; 
    }


public static  String decryptDataToString(byte[] message, String key){
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        String decrypted =""; 
        try { 
            byte[] keyBytes = key.getBytes("UTF8"); 

            BlockCipher engine = new AESEngine();
            BufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
            keyBytes = Arrays.copyOf(keyBytes, 32); // use only first 256 bit

            c.init(false, new ParametersWithIV(new KeyParameter(keyBytes), new byte[16]));
            byte[] cipherText = new byte[c.getOutputSize(message.length)];

            int outputLen = c.processBytes(message, 0, message.length, cipherText, 0);
            c.doFinal(cipherText, outputLen);
            decrypted = new String(cipherText,"UTF8");
        }
        catch (CryptoException ce)
        {
            System.err.println(ce);
            System.exit(1);
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace();
        }

        return decrypted;

    }

result (hex output)

clair message :3132333435 size: 5
encrypted message:561dd9f43ec183fe351776a46276991c size: 16
decrypted message:31323334350000000000000000000000 size: 16

  • 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-04T05:39:07+00:00Added an answer on June 4, 2026 at 5:39 am

    You should check How do I get started using BouncyCastle?
    The code for removing extra bytes is:

    if (outputLength == output.length) {
        return output;
    } else {
        byte[] truncatedOutput = new byte[outputLength];
        System.arraycopy(
                output, 0,
                truncatedOutput, 0,
                outputLength
            );
        return truncatedOutput;
    }
    

    and in your code traslates to:

    outputLen += c.doFinal(cipherText, outputLen);
    if (outputLen == cipherText.length) {
        decrypted = new String(cipherText,"UTF8");
    } else {
        byte[] truncatedOutput = new byte[outputLen];
        System.arraycopy(
                cipherText, 0,
                truncatedOutput, 0,
                outputLen 
            );
        decrypted = new String(truncatedOutput,"UTF8");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any maximum size for code in Java? I wrote a function with
I'm trying to encrypt/decrypt files with PBE using AES. I'm using Bouncy Casle library(lightweight
I need to implement callback function in Java using interface. I have wrote the
I have few classes and functions in java code. one of the function is
I am trying to write a recursive function in Java that prints the numbers
I am trying to write a Java equivalent for a function in C#. The
I am a beginner in JavaScript but I wrote two functions that is meant
I wrote a wrapper DLL for some native c++ functions and compiled it in
I wrote function onclick of a element <div class=left_collapsed>Որոնում</div> <div class=container style=display: none;> <ul>
I wrote this function for filling closed loop, pixvali is declared globally to store

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.