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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:36:59+00:00 2026-06-13T09:36:59+00:00

I am currently writing a program in Java that will accept strings from PHP

  • 0

I am currently writing a program in Java that will accept strings from PHP and either encrypt or decrypt them depending on need. The mechanism of encryption is AES-256 and I am using the BouncyCastle API to do it. To ensure that there are fewer problems in transferring the data back and forth, I use Base64 to encode the strings. The problem I am experiencing is that randomly, I cannot decrypt a string-some string can be decrypted ok, others cannot. I found a great article here at stackoverflow I thought could help here.

But I could not really see how it could fit my circumstances (I am not an encryption expert). Here’s my current code. Thanks for your help.

class AES {

private final BlockCipher AESCipher = new AESEngine();

private PaddedBufferedBlockCipher pbbc;
private KeyParameter key;

AES()
{
    init();
}

private void init()
{
    try
    {
        KeyGenerator kg = KeyGenerator.getInstance("AES");
        kg.init(256);
        SecretKey sk = kg.generateKey();
        key=new KeyParameter(sk.getEncoded());
        pbbc=new PaddedBufferedBlockCipher(AESCipher, new PKCS7Padding());
    }
    catch (Exception e)
    {
        //Take care of later
    }
}

private byte[] processing(byte[] input, boolean encrypt)
        throws DataLengthException, InvalidCipherTextException {

    pbbc.init(encrypt, key);

    byte[] output = new byte[pbbc.getOutputSize(input.length)];
    int bytesWrittenOut = pbbc.processBytes(
        input, 0, input.length, output, 0);

    pbbc.doFinal(output, bytesWrittenOut);

    return output;

}

private byte[] _encrypt(byte[] input)
        throws DataLengthException, InvalidCipherTextException {
    return processing(input, true);
}

private byte[] _decrypt(byte[] input)
        throws DataLengthException, InvalidCipherTextException {
    return processing(input, false);
}

public String Encrypt(String input)
{
    try
    {
        byte[] ba = input.getBytes("UTF-8");

        byte[] encr = _encrypt(ba);

        byte[] encryptedByteValue = new Base64().encode(encr);

        String encryptedValue = new String(encryptedByteValue);

        return encryptedValue;//+" and decrypted is "+Decrypt(encryptedValue);
    }
    catch (Exception e)
    {
        return "ENCRYPT_ERROR "+e.getMessage();
    }
}


public String Decrypt(String input)
{
    try
    {
        byte[] decodedValue = new Base64().decode(input.getBytes());

        byte[] retr = _decrypt(decodedValue);

        return new String(retr, "UTF-8").replaceAll("\\u0000", "");
    }
    catch (Exception e)
    {
        return "DECRYPT_ERROR "+e.getMessage();
    }
}
  • 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-13T09:37:00+00:00Added an answer on June 13, 2026 at 9:37 am

    I figured out what the problem is, and it was two fold. This is what I wound up doing:

    1) I was using cURL to communicate strings between Java and PHP and encoding encrypted text as Base64. Since the plus sign is valid in Base64 and not handled by cURL (at least by older versions), I would have mangled strings, thus leading to the error. I switched to hex encoding.

    2) I had to remove carriage return (\r\n) characters from strings that went into the Java layer.

    Hope this helps someone.

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

Sidebar

Related Questions

I'm currently writing a java program that requires some Data to run. The data
I'm currently writing a Java program that can open .exe programs on my PC,
I'm currently writing a program in Java that I would like to use to
I am currently doing a program in Java that will run several tests and
I am currently writing a program in Java that uses a query populated jcombobox.
I am currently a java newbie of sorts writing a program that is, essentially,
I am currently writing a chatroom program in Java. There is a server that
I am currently writing a program using Weka that builds a model (using one
I am writing a program that generates excel reports, currently using the Microsoft.Interop.Excel reference.
I'm building a Java program that will automatically run a hundred or so tests.

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.