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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:45:51+00:00 2026-05-15T20:45:51+00:00

I have to pass some strings from a java servlet to a php script.

  • 0

I have to pass some strings from a java servlet to a php script. What options are there for encrypting the strings? I’d need a method that is implemented for both java and php (like..a caesar cipher…). Is there any standard encryption method I should be able to get a library for both java and php?

I want to encrypt the strings on the java side, pass to the php script, then let the php script decrypt them.

I can’t use https due to the limitations of the provider I’m using.

Thanks

  • 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-15T20:45:51+00:00Added an answer on May 15, 2026 at 8:45 pm

    Hopefully this can get you started. Error handling is missing and the secret key is hard coded. Both of those would need to be addressed for production quality code. From the Java side you can use the Java Cryptography Architecture (JCA):

    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    import javax.xml.bind.DatatypeConverter;
    
    public final void testTesting() throws Exception {
        final String plainText = "plain text";
        final String result = encrypt(plainText);
        System.out.println(result);
    }
    
    public String encrypt(final String plainText) throws Exception {
        final byte[] data = plainText.getBytes("UTF-8");
        final byte[] encoded = encrypt(data);
        final String result = new String(encoded);
        return result;
    }
    
    public byte[] encrypt(final byte[] data) throws Exception {
        // this is just an example key, real code should use a properly generated shared secret
        final byte[] secret = new byte[] {42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
        final SecretKeySpec key = new SecretKeySpec(secret, "AES");
    
        final Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        encryptCipher.init(Cipher.ENCRYPT_MODE, key);
        final byte[] iv = encryptCipher.getIV();
        final byte[] encrypted = encryptCipher.doFinal(data);
    
        final int outputLength = encrypted.length;
        final int ivLength = iv.length;
    
        final byte[] results = new byte[outputLength + ivLength];
        System.arraycopy(iv, 0, results, 0, ivLength);
        System.arraycopy(encrypted, 0, results, ivLength, outputLength);
    
        return DatatypeConverter.printBase64Binary(encoded);
    }
    

    From the PHP side, you are going to need Mcrypt.

    <?php
    $key = base64_decode('KioqKioqKioqKioqKioqKg==');
    $input = base64_decode($_GET['input']);
    $plain_text = substr($input, 16);
    $initialization_vector = substr($input, 0, 16);
    echo pkcs5_unpad(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $plain_text, MCRYPT_MODE_CBC, $initialization_vector));
    
    function pkcs5_unpad($text) {
        $pad = ord($text{strlen($text)-1});
        if ($pad > strlen($text)) {
            return false;
        }
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
            return false;
        }
        return substr($text, 0, -1 * $pad);
    }
    ?>
    

    The function pkcs5_unpad was copied from here since it seems that PHP Mcrypt doesn’t include support for PKCS5 padding. The Java code prefixes the data with the initialization vector used to encrypt it. Subsequently the PHP code splits it into two, the initialization vector and the encrypted data.

    This code uses 128 bit AES (Rijndael) in CBC mode which should be secure enough for most uses. In addition to the simple encryption, I recommend using an HMAC as described here to ensure the data isn’t tampered with. To perform the HMAC in Java, use the Mac class. For PHP, see Mhash.

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

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use GetProductInfo, Windows 7 and Windows Server 2008 R2 have… May 17, 2026 at 1:48 am
  • Editorial Team
    Editorial Team added an answer After you call .validate() on the form, you an do… May 17, 2026 at 1:48 am
  • Editorial Team
    Editorial Team added an answer I think the most flexible approach is the one followed… May 17, 2026 at 1:48 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I need a some stored procedure that called like: search('foo bar') makes a search
I have a function that reads user input from std::cin, and I want to
Looking to pass variables from c# to javascript to use some jquery code. Passing
I have some problem with passing a WebView from the first activity to the
I am writing an android app. I want to pass some data across the
I would like to create an equivalent if this Java method as PL/SQL function
I'm writing a web application (PHP) for my friend and have decided to use
I have a C API that talks to hardware and I want to ultimately
I know the basics of clojure/java interop: calling java from clojure and vice versa.
I'm using DTOs between my business and presentation layers and have some mapping code

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.