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 - function pad($string, $blocksize = 32)
-
{
-
$len = strlen($string);
-
$pad = $blocksize - ($len % $blocksize);
-
$string .= str_repeat(chr($pad), $pad);
-
return $string;
-
}
-
function encrypt($string)
-
{
-
$key = base64_decode("/RczyqV95+E4dC/owOzv1tncb5X2n+tzehoxdarJPmc=");
-
$iv = base64_decode("MFo4Fm64Y+58oZ2lOQ1bqT+P9WgdirhG9CkPAubwI1E=");
-
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $this->pad($string), MCRYPT_MODE_CBC, $iv));
-
}
C# -
private static SymmetricAlgorithm GetAlgorithm(SecureString password)
-
{
-
SymmetricAlgorithm algorithm = new RijndaelManaged();
-
algorithm.KeySize = 256;
-
algorithm.BlockSize = 256;
-
algorithm.Mode = CipherMode.CBC;
-
algorithm.Padding = PaddingMode.PKCS7;
-
algorithm.Key = Convert.FromBase64String("/RczyqV95+E4dC/owOzv1tncb5X2n+tzehoxdarJPmc=");
-
algorithm.IV = Convert.FromBase64String("MFo4Fm64Y+58oZ2lOQ1bqT+P9WgdirhG9CkPAubwI1E=");
-
}
-
-
public static string EncryptString(string clearText, string password)
-
{
-
SymmetricAlgorithm algorithm = GetAlgorithm(password);
-
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
-
MemoryStream ms = new MemoryStream();
-
CryptoStream cs = new CryptoStream(ms, algorithm.CreateEncryptor(), CryptoStreamMode.Write);
-
cs.Write(clearBytes, 0, clearBytes.Length);
-
cs.Close();
-
return Convert.ToBase64String(ms.ToArray());
-
}
And my test code: -
var input = "test";
-
-
using (WebClient client = new WebClient())
-
{
-
byte[] response = client.UploadValues("[PHP script which takes $_POST val and returns encrypted byte array]", new NameValueCollection()
-
{
-
{ "test", input },
-
});
-
Console.WriteLine(Encoding.Default.GetString(response));
-
}
-
-
var encrypted = [Relavent Class].EncryptString(input);
-
Console.WriteLine(encrypted);
Which returns basically the two encryptions (which I want to be the same): - QrWFHlhzOsVRTD7wqEyGcnfb1PvMl6ZR8p9ES5SR0Tw=
-
oGX2FLLQJuC0y8KvDKFWxiS3b6XLIU+60EYtNe0PExw=
3 1266
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.
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.
I'm glad you got it working!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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?
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |