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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:58:15+00:00 2026-06-05T01:58:15+00:00

PHP Encrypt Function $privateKey = 1234567812345678; $iv = 1234567812345678; $data = Test string; $encrypted

  • 0

PHP Encrypt Function

$privateKey = "1234567812345678";
$iv = "1234567812345678";
$data = "Test string";

$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);

echo(base64_encode($encrypted));

Result: iz1qFlQJfs6Ycp+gcc2z4w==

When I try to decrypt this result in Java using the function below, all I get back is ì�š@ÔBKxnfÈ~¯Ô’M while I am expecting “Test string”. Any ideas where I am wrong? Thanks

public static String decrypt() throws Exception{
    try{
        String Base64EncodedText = "iz1qFlQJfs6Ycp+gcc2z4w==";
        String decodedText = com.sun.xml.internal.messaging.saaj.util.Base64.base64Decode(Base64EncodedText);
        String key = "1234567812345678";
        String iv = "1234567812345678";

        javax.crypto.spec.SecretKeySpec keyspec = new javax.crypto.spec.SecretKeySpec(key.getBytes(), "AES");
        javax.crypto.spec.IvParameterSpec ivspec = new javax.crypto.spec.IvParameterSpec(iv.getBytes());

        javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(javax.crypto.Cipher.DECRYPT_MODE, keyspec, ivspec);
        byte[] decrypted = cipher.doFinal(decodedText.getBytes());

        String str = new String(decrypted);

        return str;

    }catch(Exception e){
        return null;
    }   
}
  • 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-05T01:58:17+00:00Added an answer on June 5, 2026 at 1:58 am

    EDIT: As of Java 8 Java now includes an acceptable Base64 class, java.util.Base64.


    This line

    String decodedText = com.sun.xml.internal.messaging.saaj.util.Base64.base64Decode(Base64EncodedText);
    

    looks wrong. Instead, use the apache commons codec classes or the Harder base64 class. Also the default padding used by mcrypt, zero padding, is arguably wrong and makes it difficult to use the results in other languages. The users comments section for the mcrypt_encrypt web pages has examples of how to do this.

    Here is small example that uses the apache commons classes to decrypt your string.

    import java.nio.charset.Charset;
    
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    import org.apache.commons.codec.binary.Base64;
    import org.apache.commons.codec.binary.Hex;
    
    public class AESToy3 {
    
        private static final Charset ASCII = Charset.forName("US-ASCII");
    
        public static void main(String[] args) throws Exception {
            String base64Cipher = "iz1qFlQJfs6Ycp+gcc2z4w==";
            byte [] cipherBytes = Base64.decodeBase64(base64Cipher);
            byte [] iv = "1234567812345678".getBytes(ASCII);
            byte [] keyBytes = "1234567812345678".getBytes(ASCII);
    
            SecretKey aesKey = new SecretKeySpec(keyBytes, "AES");
    
            Cipher cipher = Cipher.getInstance("AES/CBC/NOPADDING");
            cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv));
    
            byte[] result = cipher.doFinal(cipherBytes);
            System.out.println(Hex.encodeHexString(result));
        }
    
    }
    

    this produces the following output:

    5465737420737472696e670000000000
    

    which when decoded as ASCII and removing the trailing zeros gives you Test string

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

Sidebar

Related Questions

PHP Function: $privateKey = 1234567812345678; $iv = 1234567812345678; $data = Test string; $encrypted =
so I encrypted a string on my php server. encrypt(http://google.com, happy); function encrypt($str, $key)
in function need key to encrypt string without mcrypt libraly in php function encrypt($str,
I need to encrypt a string using MySQL's AES_ENCRYPT function, then attach that encrypted
I need help converting this into PHP: Public Function Encrypt(ByVal text As String) As
HI, I am encrypting data with following PHP function: $enc_data = mcrypt_encrypt(MCRYPT_3DES, $_key, $_data,
I tried to encrypt data using this function in PHP and decrypt it with
I need a PHP function, AES256_encode($dataToEcrypt) to encrypt the $data into AES-256 and another
I need to obfuscate or encrypt some plain text data in my php 5.2
Following is a php function for masking a string. Can somebody give me the

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.