473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

PHP <--> .NET Encryption

TheServant
1,168 Expert 1GB
Hi all,
I am really struggling with this and I have tried so many things I feel like I'm just chasing my tail.

Trying to have a matching encryption service in my .NET application and a PHP server, but no matter what I do, I can't seem to get these to agree, even when I use the same Key and IV. Any help would be greatly appreciated I you can see the inconsistency!

PHP
Expand|Select|Wrap|Line Numbers
  1.          function pad($string, $blocksize = 32)
  2.         {
  3.             $len = strlen($string);
  4.             $pad = $blocksize - ($len % $blocksize);
  5.             $string .= str_repeat(chr($pad), $pad);
  6.             return $string;
  7.         }
  8.         function encrypt($string)
  9.         {
  10.             $key = base64_decode("/RczyqV95+E4dC/owOzv1tncb5X2n+tzehoxdarJPmc=");
  11.             $iv = base64_decode("MFo4Fm64Y+58oZ2lOQ1bqT+P9WgdirhG9CkPAubwI1E=");
  12.             return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $this->pad($string), MCRYPT_MODE_CBC, $iv));
  13.         }
C#
Expand|Select|Wrap|Line Numbers
  1.         private static SymmetricAlgorithm GetAlgorithm(SecureString password)
  2.         {
  3.             SymmetricAlgorithm algorithm = new RijndaelManaged();
  4.             algorithm.KeySize = 256;
  5.             algorithm.BlockSize = 256;
  6.             algorithm.Mode = CipherMode.CBC;
  7.             algorithm.Padding = PaddingMode.PKCS7;
  8.             algorithm.Key = Convert.FromBase64String("/RczyqV95+E4dC/owOzv1tncb5X2n+tzehoxdarJPmc=");
  9.             algorithm.IV = Convert.FromBase64String("MFo4Fm64Y+58oZ2lOQ1bqT+P9WgdirhG9CkPAubwI1E=");
  10.         }
  11.  
  12.         public static string EncryptString(string clearText, string password)
  13.         {
  14.             SymmetricAlgorithm algorithm = GetAlgorithm(password);
  15.             byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
  16.             MemoryStream ms = new MemoryStream();
  17.             CryptoStream cs = new CryptoStream(ms, algorithm.CreateEncryptor(), CryptoStreamMode.Write);
  18.             cs.Write(clearBytes, 0, clearBytes.Length);
  19.             cs.Close();
  20.             return Convert.ToBase64String(ms.ToArray());
  21.         }
And my test code:
Expand|Select|Wrap|Line Numbers
  1.             var input = "test";
  2.  
  3.             using (WebClient client = new WebClient())
  4.             {
  5.                 byte[] response = client.UploadValues("[PHP script which takes $_POST val and returns encrypted byte array]", new NameValueCollection()
  6.                 {
  7.                 { "test", input },
  8.                 });
  9.                 Console.WriteLine(Encoding.Default.GetString(response));
  10.             }
  11.  
  12.             var encrypted = [Relavent Class].EncryptString(input);
  13.             Console.WriteLine(encrypted);
Which returns basically the two encryptions (which I want to be the same):
Expand|Select|Wrap|Line Numbers
  1. QrWFHlhzOsVRTD7wqEyGcnfb1PvMl6ZR8p9ES5SR0Tw=
  2. oGX2FLLQJuC0y8KvDKFWxiS3b6XLIU+60EYtNe0PExw=
Aug 28 '14 #1
3 1266
Frinavale
9,735 Expert Mod 8TB
You may need to consider using a 3rd party component to do the encryption that you can utilize in your C# code and in your PHP code.

I'm sorry but I don't think you are going to ever going to produce the same output attempting to use two different technologies like this.

It has been a long time since I've done much with encryption but I'm pretty sure that the .NET stuff generates their keys based based on inner workings of it's won code and that is going to be different than the stuff that PHP does.

If you use a 3rd party component I think that you'll have more luck with this.
Sep 16 '14 #2
TheServant
1,168 Expert 1GB
Thanks Frin. I ended up deleting the lot and starting over. I did get it working, and I have a feeling it had something to do with the order I did the base64 encoding. It's spread over several files now, and don't have time to post, but it was not very different to the above.
** I also just noticed that the default blocksize for the PHP pad function did not match that of C#, which would have also prevented it from functioning properly.
Sep 16 '14 #3
Frinavale
9,735 Expert Mod 8TB
I'm glad you got it working!
Sep 17 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Cliff | last post by:
We are trying to connect to 3 different Oracle databases using MS Access as the front-end and ODBC as the connection. The problem that we are having is that 1 of the databases requires a...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
7
by: Alan Silver | last post by:
Hello, I am writing a page where sensitive data is collected (over SSL) and stored in a database. I have been looking at the .NET encryption classes, but am a bit confused as to which is best...
2
by: Sumit Gupta | last post by:
Can anyone please tell me how to encrpt string or any kind of Data. Also the Algorithm of Compression. Any Link tutorial etc. Like : Zip or RAR Formats etc.
9
by: sweety | last post by:
Dear All, How to encrypt a C data file and make binary file and then have to read a bin file at run time and decrypt the file and have to read the data. Any help to achive this pls. Would be...
4
by: pintu | last post by:
Hello everybody.. I hav some confusion regarding asymmetric encryption.As asymmetric encryption it there is one private key and one public key.So any data is encrypted using private key and the...
1
by: =?Utf-8?B?bWljcm9ob2Y=?= | last post by:
Short version: Is there a way to configure (preferably programmatically) the max encryption strength that will be used by the framework when connecting to a particular SSL-protected web service? ...
11
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to...
22
by: j1mb0jay | last post by:
I have had to create a simple string encryption program for coursework, I have completed the task and now have to do a write up on how it could be improved at a later date. If you could look...
19
by: klenwell | last post by:
Another request for comments here. I'd like to accomplish something like the scheme outlined at this page here: http://tinyurl.com/3dtcdr In a nutshell, the form uses javascript to hash...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.