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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:06:44+00:00 2026-06-09T11:06:44+00:00

Here is my Python code, I can successfully encrypt and decrypt using M2Crypto: from

  • 0

Here is my Python code, I can successfully encrypt and decrypt using M2Crypto:

from base64 import b64encode, b64decode
import M2Crypto

class AESEncryptionService(object):
    def encrypt(self, key, msg):
        return self.__cipher(key, msg, 1)

    def decrypt(self, key, msg):
        try:
            decrypted = self.__cipher(key, msg, 0)
            return decrypted
        except:
            return False

    def __cipher(self, key, msg, op):
        iv = '\0' * 16
        iv.encode('ascii')
        cipher = M2Crypto.EVP.Cipher(alg='aes_128_cbc', key=key, iv=iv, op=op)
        v = cipher.update(msg)
        v = v + cipher.final()
        del cipher
        return v

enc_service = AESEncryptionService()
cipher = b64encode(enc_service.encrypt("FD496E240E7822552BC0D63C03AB7ABB", "Hello Hello Hello Hello Hello Hello Hello"))

print cipher

plaintext = enc_service.decrypt("FD496E240E7822552BC0D63C03AB7ABB", b64decode(cipher))

print plaintext

Outputs:

oMkdgXOy49VvvbQksxuhBq3YqJWrEw++lZO3ZMYYyo6T7JpK+Ovp+tdm+FrVGPnN
Hello Hello Hello Hello Hello Hello Hello

I have written a simple test script in PHP hoping to get the same results:

<?php

$key = 'FD496E240E7822552BC0D63C03AB7ABB';
$plain = 'Hello Hello Hello Hello Hello Hello Hello';
$iv = '0000000000000000';

$cipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plain, MCRYPT_MODE_CBC, $iv);

$plain = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $cipher, MCRYPT_MODE_CBC, $iv);

echo base64_encode($cipher), '<br>';
echo $plain;

Outputs:

v5QinnNaEkn8myZa31Ikl5DrrtSXrhebDERJz4uqUgWMvbZ3datyCbm9WHPGlAo7
Hello Hello Hello Hello Hello Hello Hello

Can you help me fix the Python code to output the same as PHP? I know PHP’s implementation of AES is correct as I can decrypt it in .NET. The string outputted by M2Crypto Python library cannot be decrypted in .NET.

I think it might have something to do with padding.

M2Crypto.EVP.Cipher has padding parameter. I have tried setting it to 1, 2, 3, 128 but it doesn’t seem to affect the cipher.

  • 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-09T11:06:46+00:00Added an answer on June 9, 2026 at 11:06 am

    Your PHP $iv appears to be a string of ASCII zeros (so, byte 0x30), where in the python iv you appear to be using literal 0x00 bytes.

    EDIT

    You also appear to be passing the ciphertext through b64encode and b64decode. Are you quite sure these are exact inverses? No line breaks, formatting, trailing new lines, etc?

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

Sidebar

Related Questions

Here is some sample Python code: import re some_regex = re.compile(r\s+1\s+) result = some_regex.search(
Here is the my python code using BeautifulSoup. The main issue is with the
I'm trying to run NeHe's tutorial here using Python 2.7.3, but It's throwing the
Python novice here. I am using python2.7.2 on Windows7. I have installed the PyWin32
I have downloaded the two releases of python 2.6.8 from here http://www.python.org/getit/releases/2.6.8/ and I
I am currently working on getting JavaScript to execute successfully from within Python. I
I've been trying to write a python code which can archive the files of
Can someone kindly explain how the following python code is working? This code is
I just successfully shipped my code to the production server. You can check out
What benefit or implications could we get with Python code like this: class some_class(parent_class):

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.