PHP and C# 3DES Encryption -


need convert following function php c# (asp.net)

function encrypt_3des($message, $key){     // se establece un iv por defecto     $bytes = array(0,0,0,0,0,0,0,0); //byte [] iv = {0, 0, 0, 0, 0, 0, 0, 0}     $iv = implode(array_map("chr", $bytes)); //php 4 >= 4.0.2      // se cifra     $ciphertext = mcrypt_encrypt(mcrypt_3des, $key, $message, mcrypt_mode_cbc, $iv); //php 4 >= 4.0.2     return $ciphertext; } 

where $message string encode , $key key

the $key base 64 encoded , decoded before calling function

$key = $this->decodebase64($key); $ciphertext = $this->encrypt_3des($message, $key); 

following c# code used:

key = base64decode(key); ciphertext = encrypt_3des(order, key,true); 

where

 private  string base64decode(string base64encodeddata)     {         byte[] base64encodedbytes = system.convert.frombase64string(base64encodeddata);         return encoding.getencoding(28591).getstring(base64encodedbytes);         // 28591 php compatibility     }   

and

  private string encrypt_3des(string message, string k,bool usehashing)     {         byte[] keyarray;         byte[] toencryptarray = utf8encoding.utf8.getbytes(message);          //if hashing use hashcode regards key         if (usehashing)         {             md5cryptoserviceprovider hashmd5 = new md5cryptoserviceprovider();             keyarray = hashmd5.computehash(encoding.getencoding(28591).getbytes(k));             //always release resources , flush data             // of cryptographic service provide. best practice              hashmd5.clear();         }         else             keyarray = utf8encoding.getencoding(28591).getbytes(k);          tripledescryptoserviceprovider tdes = new tripledescryptoserviceprovider();         //set secret key tripledes algorithm         tdes.key = keyarray;         //mode of operation. there other 4 modes.         //we choose ecb(electronic code book)         tdes.mode = ciphermode.ecb;         //padding mode(if byte added)          tdes.padding = paddingmode.pkcs7;          icryptotransform ctransform = tdes.createencryptor();         //transform specified region of bytes array resultarray         byte[] resultarray =           ctransform.transformfinalblock(toencryptarray, 0,           toencryptarray.length);         //release resources held tripledes encryptor         tdes.clear();         //return encrypted data unreadable string format         return convert.tobase64string(resultarray, 0, resultarray.length);      } 

the results php , c# not same.

i found code works me in spanish web. http://www.resuelvetusproblemas.com/convertir-encriptacion-en-php-3des-en-c/

this function in c# same you've written in php

    public static byte[] tripledesencrypt(string texto, byte[] key)     {         using (tripledescryptoserviceprovider tdes = new tripledescryptoserviceprovider())         {             byte[] iv_0 = { 0, 0, 0, 0, 0, 0, 0, 0 };              byte[] toencryptarray = encoding.ascii.getbytes(texto);                             tdes.iv = iv_0;              //assign secret key             tdes.key = key;              tdes.mode = ciphermode.cbc;              tdes.padding = paddingmode.zeros;              icryptotransform ctransform = tdes.createencryptor();             //transform specified region of bytes array resultarray             byte[] resultarray =               ctransform.transformfinalblock(toencryptarray, 0,               toencryptarray.length);              //clear best practices             tdes.clear();              return resultarray;         }     } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -