473,327 Members | 1,936 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,327 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 1264
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.