Click here to Skip to main content
15,115,261 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have gotten des encryption code that client will be using in c#

C#
public string EncryptQueryString(string stringToEncrypt)
        {
            byte[] key = { };
            byte[] IV = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
            try
            {
                key = Encoding.UTF8.GetBytes(KEY);
using (DESCryptoServiceProvider oDESCrypto = new DESCryptoServiceProvider())
                {
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
                    MemoryStream oMemoryStream = new MemoryStream();
                    CryptoStream oCryptoStream = new CryptoStream(oMemoryStream,
                    oDESCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                    oCryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
                    oCryptoStream.FlushFinalBlock();
                    return Convert.ToBase64String(oMemoryStream.ToArray());
                }
            }
            catch
            {
                throw;
            }
        }

        public string DecryptQueryString(string stringToDecrypt)
        {
            byte[] key = { };
            byte[] IV = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
            stringToDecrypt = stringToDecrypt.Replace(" ", "+");
            byte[] inputByteArray = new byte[stringToDecrypt.Length];

            try
            {
                key = Encoding.UTF8.GetBytes(KEY);
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(stringToDecrypt);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                Encoding encoding = Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }
            catch
            {
                throw;
            }
        }


i am trying to get this to work in php - please help me in showiing me what i am doing wrong.

What I have tried:

PHP
echo "<pre>\n";
$key = "K6u8#m2b";
$textToEncrypt = "5512065314089";
$iv = "0112233445566778";

$encrypted = openssl_encrypt($textToEncrypt, 'aes-256-cbc', $key, 0, $iv);
echo "encrypted output=" . $encrypted . "\n";

echo "expected- WfRb+Vugfc1cbJNfXKL6bw=="


output

BASE64(IV)=MDExMjIzMzQ0NTU2Njc3OA==
encrypted output=Pt0MK+1qt0mAyU7irnWzTw==
decrypted output=5512065314089
expected- WfRb+Vugfc1cbJNfXKL6bw==
Posted
Updated 31-May-21 7:57am
v2
Comments
Richard MacCutchan 31-May-21 9:22am
   
"expected- WfRb+Vugfc1cbJNfXKL6bw=="
Why would you expect "5512065314089" to be encrypted and decrypted to "WfRb+Vugfc1cbJNfXKL6bw=="?
Paul van Zyl 31-May-21 12:56pm
   
Serious - its the code to encrypt and then when the encryption is sent back to decrypt back
Richard MacCutchan 31-May-21 15:04pm
   
You have edited the question and removed the part that my comment refers to. But what is left makes no more sense.
Richard Deeming 1-Jun-21 10:01am
   
You C# code is using DES; your PHP code is using AES 256. Of course you're going to get different output!

However, using the key and input text from your PHP code in your C# code does not produce your "expected" output.

You need to update your question to provide a clear and precise description of the problem. At the very least, your expected output for the PHP code needs to match the actual output for your C# code, otherwise we can't help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900