Skip to content
Advertisement

Function from C# to PHP

I need to transform these encryption system developed in C# to PHP. These are the input data. Customer ID, JSON object transformed to string and a private key.

codigoCliente: 1002

datos: {“codigoCliente”:1002,”codigoArticulo”:”30-07483″,”cantidad”:1}

claveSecretaServicio: RFlTfDIwMjBXZWJQYWdlX0V4dGVybmF

I need to get this HASH as result: WGHGEY830J3WeadO1o4NGNLYZ9lY7xvquol5igE+hLU=

This is the call in C#: GetHash(modelo.CodigoCliente.ToString(), JsonSerializer.Serialize(modelo), “WGHGEY830J3WeadO1o4NGNLYZ9lY7xvquol5igE+hLU=”);

The C# functions to transform to PHP are these.

JavaScript

I don’t know C# so I don’t really understand the detailed workings of the functions to change them to PHP.

I have tried this but my HASH result is different.

JavaScript

I’ve gotten a valid 3DES PHP function (thanks to Michael Fehr)

JavaScript

but I still need the other two to achieve the valid HASH. Perhaps if I you read this Michael Fehr.

Your function is correct and I get the same intermediate 3DES value that is achieved with C#: PD8fSM4gz3s=

Thank you in advance.

Advertisement

Answer

The posted Base64 encoded key RFlTfDIwMjBXZWJQYWdlX0V4dGVybmF results Base64 decoded in DYS|2020WebPage_Externa and thus has a length of 23 bytes. This is too short for TripleDES, which requires a 24 bytes key. A plausible key that additionally has the required length of 24 bytes is DYS|2020WebPage_External, which is Base64 encoded RFlTfDIwMjBXZWJQYWdlX0V4dGVybmFs. This is probably a copy/paste issue.

Since the passed data corresponds to a JSON string, a corresponding class, in the following called Modelo, has to be defined, initialized according to the posted sample data and passed as JSON string.

Then the following C# code returns the expected hash:

JavaScript

In the PHP code, in the encrypt_3DES method, the result must not be returned Base64 encoded, but as raw binary data. In addition, a class corresponding to the Modelo class of the C# code must be implemented, initialized according to the posted sample data, and passed as JSON string.

The following PHP code returns the expected hash value corresponding to that of the C# code:

JavaScript

A further note: Apparently the data (datos) are to be hashed with a HMAC and the key used for this (claveOperacion) is derived from a client ID (CodigoCliente) in combination with a password (claveSecretaServicio). To derive the key for the HMAC, an encryption with TripleDES is applied. More contemporary seems to me a derivation with a reliable key derivation function, such as PBKDF2.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement