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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:04:07+00:00 2026-05-13T17:04:07+00:00

I decrypt data using PHP with this code: $content=1234; $cp = mcrypt_module_open(‘rijndael-128’, ”, ‘cbc’,

  • 0

I decrypt data using PHP with this code:

$content="1234";
$cp = mcrypt_module_open('rijndael-128', '', 'cbc', '');
$iv = mcrypt_create_iv(16, MCRYPT_RAND);
$key = pack("H*",md5('a'));
mcrypt_generic_init($cp, $key, $iv);
$encrypted = mcrypt_generic($cp, $content);
echo base64_encode($key)."\n";
echo base64_encode($iv)."\n";
echo base64_encode($encrypted)."\n";
mcrypt_generic_deinit($cp);
mcrypt_module_close($cp);

$iv and $encrypted is then saved to file and read in the C# sample app:

var iv=...;
var encrypted=...;
var md5 = new MD5CryptoServiceProvider();
var key = md5.ComputeHash(Encoding.Default.GetBytes("a"));
md5.Clear();

Console.WriteLine(Convert.ToBase64String(key));
Console.WriteLine(Convert.ToBase64String(iv));
Console.WriteLine(Convert.ToBase64String(encrypted));

The output here is exactly the same as the output from PHP, so I can assure there is no encoding error inbetween.

var rd = new RijndaelManaged {
  Key = key,
  IV = iv,
  Mode = CipherMode.CBC,
  KeySize = 128,
  Padding = PaddingMode.Zeros
};

var buffer = new byte[encrypted.Length];
using(var ms = new MemoryStream(buffer)) {
  using(var cs = new CryptoStream(ms, rd.CreateDecryptor(), CryptoStreamMode.Write)) {
    cs.Write(encrypted, 0, encrypted.Length);
    ms.Read(buffer, 0, buffer.Length);
    Console.WriteLine(Encoding.Default.GetString(buffer));
  } 
}
rd.Clear();

The result of the decryption varies on every program start, even with exactly the same input data:

First run:
DMF1ucDxtqgxw5niaXcmYQ== <-Key
GoCeRkrL/EMKNH/BYeLsqQ== <-IV
UBE3DkgbJgj1K/TISugLxA== <-Encrypted
OlOB99yiCYRDoLx+0xxZxQ== <-“Decrypted”

Second run:
DMF1ucDxtqgxw5niaXcmYQ== <-Key
GoCeRkrL/EMKNH/BYeLsqQ== <-IV
UBE3DkgbJgj1K/TISugLxA== <-Encrypted
w5fcY5Fbb9KRgoHfhqAztA== <-“Decrypted”

Key, IV, Encrypted data are identical, but still the decrypted date varies and is always wrong. buffer should contain “1234” or “1234” plus 12 trailing zeros.

I don’t see why the results vary and what is not working, but I have been staring at this darn piece of code for several hours now, and probably miss the obvious error…

Reversing the CryptoStream like this creates identically wrong results:

using(var ms = new MemoryStream(encrypted)) {
  using(var cs = new CryptoStream(ms, rd.CreateDecryptor(), CryptoStreamMode.Read)) {
    cs.Read(buffer, 0, buffer.Length);
    Console.WriteLine(Convert.ToBase64String(buffer));
  }
}

Help?
Thanks!
Alexander

  • 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-13T17:04:07+00:00Added an answer on May 13, 2026 at 5:04 pm

    Well, modifying an old sample of my sins of the past I ended up with this:

    static string Decrypt() {            
      byte[] keyBytes = Convert.FromBase64String("DMF1ucDxtqgxw5niaXcmYQ==");
      byte[] iv = Convert.FromBase64String("GoCeRkrL/EMKNH/BYeLsqQ==");
      byte[] cipherTextBytes = Convert.FromBase64String("UBE3DkgbJgj1K/TISugLxA==");
    
      var symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC, IV = iv, KeySize = 128, Key = keyBytes, Padding = PaddingMode.Zeros};
    
      using (var decryptor = symmetricKey.CreateDecryptor())
      using (var ms = new MemoryStream(cipherTextBytes))
      using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) {
        var plainTextBytes = new byte[cipherTextBytes.Length];
        int decryptedByteCount = cs.Read(plainTextBytes, 0, plainTextBytes.Length);
        return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
      }
    }
    

    which gave “1234” with trailing \0 characters.. Did you just forget to convert the byte[] to a string again? What other difference am I missing?

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

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • 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 You have to implement onclick inside the svg and link… May 15, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer How you have initialized groupPath? It should be the path… May 15, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer Use NSTimer. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { timer = [NSTimer scheduledTimerWithTimeInterval:0.25… May 15, 2026 at 4:09 pm

Trending Tags

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

Top Members

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.